-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathuploadExamples.js
371 lines (344 loc) · 10.4 KB
/
uploadExamples.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const fs = require("fs");
const dotenv = require("dotenv");
const objectID = require("bson-objectid");
const readline = require("node:readline/promises");
const { stdin: input, stdout: output } = require("node:process");
const ml5Version = require("../package.json").version;
dotenv.config();
const P5_URL = "https://editor.p5js.org";
const TEXT_FILE_REGEX =
/.+\.(json|txt|csv|tsv|vert|frag|js|css|html|htm|jsx|xml|stl|mtl)$/i;
let sessionID = "";
const manualUploadList = [];
let manualUploadFlag = false;
/**
* Get the session ID for the p5 web editor.
*
* @returns {Object} The response status code and the session ID
*/
async function getP5SessionID() {
// The credentials for p5 web editor.
const credentials = {
email: process.env.P5_USERNAME,
password: process.env.P5_PASSWORD,
};
const res = await fetch(P5_URL + "/editor/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credentials),
});
let sid;
try {
sid = res.headers.get("set-cookie").split(";")[0].split("=")[1];
} catch (e) {}
return { status: res.status, sid };
}
/**
* upload a new sketch on the p5 web editor.
*
* @param {Object} sketch - The sketch object.
*/
async function uploadSketch(sketch) {
const res = await fetch(P5_URL + "/editor/projects", {
method: "POST",
headers: {
"Content-Type": "application/json",
Cookie: `sessionId=${sessionID}`,
},
body: JSON.stringify(sketch),
});
return res.status;
}
/**
* update a sketch on the p5 web editor.
* The updated sketch will have the same URL as the original sketch.
*
* @param {Object} sketch - The sketch object.
*/
async function updateSketch(oldSketch, sketch) {
sketch.id = oldSketch.id;
const res = await fetch(P5_URL + "/editor/projects/" + oldSketch.id, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Cookie: `sessionId=${sessionID}`,
},
body: JSON.stringify(sketch),
});
return res.status;
}
/**
* Upload a binary file to the p5 web editor.
* @param {string} filePath - The file path.
* @returns
*
* @todo: This function does not work, non-text files need to be uploaded manually for now
*/
async function uploadFile(filePath) {
const file = fs.readFileSync(filePath);
const res = await fetch(P5_URL + "/editor/S3/sign", {
method: "POST",
headers: {
"Content-Type": "application/octect-stream",
Cookie: `sessionId=${sessionID}`,
},
body: file,
});
const data = await res.text();
console.log(data);
//const data = await res.json();
//console.log(data);
//return data;
}
/**
* Get the existing files on the p5 web editor.
* @returns {Object} The response status code and the data
*/
getExistingSketches = async () => {
const res = await fetch(
P5_URL + `/editor/${process.env.P5_USERNAME}/projects`,
{
method: "GET",
headers: {
Cookie: `sessionId=${sessionID}`,
},
}
);
const data = await res.json();
return { status: res.status, data };
};
/**
* Delete a file from the p5 web editor.
* @param {Object} file - The file object.
* @returns {number} The status code of the response.
*/
deleteSketch = async (file) => {
const res = await fetch(P5_URL + `/editor/projects/${file.id}`, {
method: "DELETE",
headers: {
Cookie: `sessionId=${sessionID}`,
},
});
return res.status;
};
/**
* Get the file path of each item in a given directory.
*
* @param {string} dir - The directory path.
* @returns - An array of file paths.
*/
function getFilepaths(dir) {
let files = [];
fs.readdirSync(dir).forEach((file) => {
files.push(`${dir}/${file}`);
});
return files;
}
/**
* Check if a file is a text file.
* @param {string} filename
* @returns {boolean} True if the file is a text file, false otherwise.
*/
function isTextFile(filename) {
return TEXT_FILE_REGEX.test(filename);
}
/**
* Create a p5 file object from a file path.
*
* @param {string} fileID - The file ID.
* @param {string} filePath - The file path.
* @returns {Object} The file object.
*/
function createFileObject(fileID, filePath) {
const filename = filePath.split("/").pop();
let fileContent = "";
if (isTextFile(filename)) {
fileContent = fs.readFileSync(filePath, "utf8");
fileContent = replaceWithCdnURL(fileContent);
} else {
//TODO: handle upload non-text files
manualUploadList.push(filePath);
manualUploadFlag = true;
return null;
}
return {
name: filename,
id: fileID,
_id: fileID,
fileType: "file",
content: fileContent,
};
}
/**
* Create a p5 folder object.
* @param {s} folderID - The folder ID.
* @param {string} folderPath - The folder path. If not provided, the folder will be root.
* @returns {Object} The folder object.
*/
function createFolderObject(folderID, folderPath) {
const folderName = folderPath ? folderPath.split("/").pop() : "root";
return {
name: folderName,
id: folderID,
_id: folderID,
fileType: "folder",
children: [],
content: "",
};
}
/**
* Replace the local file path with the ml5.js CDN URL.
* @param {string} content - The content of the file.
* @returns {string} The content with the CDN URL.
*/
function replaceWithCdnURL(content) {
const version = ml5Version.split(".")[0];
return content.replace(
"../../dist/ml5.js",
`https://unpkg.com/ml5@${version}/dist/ml5.min.js`
);
}
/**
* Recursively add files and folders to the sketch object.
* @param {*} currentPath - The current directory path
* @param {*} fileArray - The array storing file and folder objects in the sketch.
* @param {*} childrenArray - The array storing the children ids of the folder.
*/
async function recursiveAddFiles(currentPath, fileArray, childrenArray) {
const filepaths = getFilepaths(currentPath);
filepaths.forEach((filePath) => {
const fileID = objectID().toString();
if (fs.lstatSync(filePath).isDirectory()) {
const folder = createFolderObject(fileID, filePath);
fileArray.push(folder);
childrenArray.push(fileID);
recursiveAddFiles(filePath, fileArray, folder.children);
} else {
const fileObject = createFileObject(fileID, filePath);
if (fileObject) {
fileArray.push(fileObject);
childrenArray.push(fileID);
}
}
});
}
/**
* Create a p5 sketch object from a given sketch path.
* @param {*} sketchDirPath
* @returns {Object} The sketch object.
*/
function createSketchObject(sketchDirPath) {
const rootID = objectID().toString();
const sketchName = sketchDirPath.split("/").pop();
const sketch = {
name: sketchName,
files: [createFolderObject(rootID)],
};
recursiveAddFiles(sketchDirPath, sketch.files, sketch.files[0].children);
// Set the sketch.js file as the default selected file.
for (const file of sketch.files) {
if (file.name == "sketch.js") {
file.isSelectedFile = true;
break;
}
}
return sketch;
}
/**
* Entry point of the script.
* This script uploads each sketch in the examples to the server.
*/
async function main() {
// Confirm that the user wants to upload the examples.
const rl = readline.createInterface({ input, output });
const response = await rl.question(
"Running this script will update example sketches on the p5 Web Editor, please check out the section 'Update p5 Web Editor Sketches' in CONTRIBUTING.md for more detail before proceeding!\n\nEnter 'I understand' to proceed:\n"
);
if (response.toLocaleLowerCase() != "i understand") {
rl.close();
console.log("The script was cancelled.");
process.exit(0);
} else {
rl.close();
console.log();
}
// Get the session ID.
console.log("Obtaining session ID...");
const sessionRes = await getP5SessionID();
if (sessionRes.status == 401) {
console.log("🔴 Please check your p5 credentials in the .env file.");
process.exit(1);
} else if (sessionRes.status != 200) {
console.log(
"🔴 Failed to get session ID. The server returned status code: " +
sessionRes.status
);
process.exit(1);
}
console.log("🟢 Successfully obtained session ID.");
sessionID = sessionRes.sid;
console.log();
// Get the sketches on the web editor server and on the local machine.
const getSketchesRes = await getExistingSketches();
if (getSketchesRes.status != 200) {
console.log(
"🔴 Failed to get existing files. The server returned status code: " +
getFilesRes.status
);
process.exit(1);
}
const oldSketches = getSketchesRes.data;
const sketchPaths = getFilepaths("examples");
// Update existing sketches and upload new sketches
console.log("Uploading sketches...");
for (const sketchPath of sketchPaths) {
const oldSketch = oldSketches.find(
(sketch) =>
sketch.name.toLowerCase() === sketchPath.split("/").pop().toLowerCase()
);
// If the sketch name already exists on the web editor, update the sketch.
if (oldSketch) {
const status = await updateSketch(
oldSketch,
createSketchObject(sketchPath)
);
if (status != 200) {
console.log("🔴 Failed to update sketch: " + oldSketch.name);
console.log("The server returned status code: " + status);
} else {
if (manualUploadFlag) {
console.log("🟡 Partially updated sketch: " + oldSketch.name);
for (const file of manualUploadList) {
console.log(" - Please manually update: " + file);
}
manualUploadList.splice(0, manualUploadList.length);
manualUploadFlag = false;
} else {
console.log("🟢 Successfully updated sketch: " + oldSketch.name);
}
}
}
// If the sketch name does not exist on the web editor, upload the sketch.
else {
const status = await uploadSketch(createSketchObject(sketchPath));
if (status != 200) {
console.log("🔴 Failed to create new sketch: " + sketchPath);
console.log(" - The server returned status code: " + status);
} else {
if (manualUploadFlag) {
console.log("🟡 Partially created new sketch: " + sketchPath);
for (const file of manualUploadList) {
console.log(" - Please manually upload: " + file);
}
manualUploadList.splice(0, manualUploadList.length);
manualUploadFlag = false;
} else {
console.log("🟢 Successfully created new sketch: " + sketchPath);
}
}
}
}
}
main();