-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
136 lines (115 loc) · 3.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require("fs");
const path = require("path");
const objectID = require("bson-objectid");
const core = require("@actions/core");
require("dotenv").config();
const apiService = require("./lib/api");
const P5_USERNAME = core.getInput("p5-username") || process.env.P5_USERNAME;
const P5_PASSWORD = core.getInput("p5-password") || process.env.P5_PASSWORD;
const SKETCHES_FOLDER = path.join(
process.cwd(),
core.getInput("sketch-folder") || process.env.SKETCHES_FOLDER || "sketches"
);
const SKETCH_INFO_FILE = path.join(process.cwd(), "sketches.json");
const COLLECTION_NAME =
core.getInput("collection-name") ||
process.env.COLLECTION_NAME ||
"My Sketches";
(async () => {
if (!P5_USERNAME || !P5_PASSWORD) {
console.error(
"No username or password provided. Please set the P5_USERNAME and P5_PASSWORD environment variables."
);
process.exit(1);
}
if (!fs.existsSync(SKETCHES_FOLDER)) {
console.error(
`The "${SKETCHES_FOLDER}" folder does not exist. Please check is "SKETCHES_FOLDER" set correctly.`
);
process.exit(1);
}
// Login
await apiService.login(P5_USERNAME, P5_PASSWORD);
// Get or create the collection
const collectionId = await apiService.getOrCreateCollection(COLLECTION_NAME);
// Load existing sketch information
const sketchesInfo = fs.existsSync(SKETCH_INFO_FILE)
? JSON.parse(fs.readFileSync(SKETCH_INFO_FILE, "utf8"))
: [];
// Get the list of sketch directories
const sketches = fs
.readdirSync(SKETCHES_FOLDER)
.filter((file) =>
fs.statSync(path.join(SKETCHES_FOLDER, file)).isDirectory()
);
for (const sketchName of sketches) {
const sketchPath = path.join(SKETCHES_FOLDER, sketchName);
// Read all files in the sketch folder
const files = fs.readdirSync(sketchPath).filter((file) => {
const fileExtension = path.extname(file).toLowerCase();
return (
fs.statSync(path.join(sketchPath, file)).isFile() &&
[".html", ".css", ".js", ".json"].includes(fileExtension)
);
});
// Build the files object for the API request
const filesData = [];
const children = [];
files.forEach((fileName) => {
const filePath = path.join(sketchPath, fileName);
const content = fs.readFileSync(filePath, "utf8");
const id = objectID().toHexString();
children.push(id);
filesData.push({
id,
_id: id,
name: fileName,
content: content,
fileType: "file",
isSelectedFile: fileName === "sketch.js",
children: [],
});
});
const id = objectID().toHexString();
filesData.unshift({
id,
_id: id,
name: "root",
content: "",
fileType: "folder",
children,
});
const existingSketch = sketchesInfo.find(
(item) => item.name === sketchName
);
if (existingSketch) {
// Update the Sketch
await apiService.updateSketch(existingSketch.id, sketchName, filesData);
} else {
// Create the sketch
const sketch = await apiService.createSketch(sketchName, filesData);
sketchesInfo.push({
id: sketch.id,
name: sketchName,
});
if (sketch && sketch.id) {
// Add the sketch to the collection
await apiService.addSketchToCollection(
collectionId,
sketch.id,
sketchName
);
}
}
}
// Save updated sketch information to JSON file
try {
fs.writeFileSync(
SKETCH_INFO_FILE,
JSON.stringify(sketchesInfo, null, 2),
"utf8"
);
} catch (error) {
console.error("Error writing to file:", error);
}
})();