Skip to content

Commit aa51e20

Browse files
authored
Merge pull request #1762 from bomanimc/bomani.update-examples-upload
Update the ml5.js example upload script to pull examples from ml5-library@main
2 parents 1b4320b + 6645a70 commit aa51e20

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ EXAMPLE_USER_PASSWORD=hellop5js
1010
GG_EXAMPLES_USERNAME=generativedesign
1111
GG_EXAMPLES_EMAIL=benedikt.gross@generative-gestaltung.de
1212
GG_EXAMPLES_PASS=generativedesign
13-
ML5_EXAMPLES_USERNAME=ml5
14-
ML5_EXAMPLES_EMAIL=examples@ml5js.org
15-
ML5_EXAMPLES_PASS=helloml5
13+
ML5_LIBRARY_USERNAME=ml5
14+
ML5_LIBRARY_EMAIL=examples@ml5js.org
15+
ML5_LIBRARY_PASS=helloml5
1616
GITHUB_ID=<your-github-client-id>
1717
GITHUB_SECRET=<your-github-client-secret>
1818
GOOGLE_ID=<your-google-client-id> (use google+ api)

server/scripts/examples-ml5.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import rp from 'request-promise';
33
import Q from 'q';
44
import { ok } from 'assert';
55

6-
// TODO: Change branchName if necessary
7-
const branchName = 'release';
6+
const branchName = 'main';
87
const branchRef = `?ref=${branchName}`;
9-
const baseUrl = 'https://api.github.com/repos/ml5js/ml5-examples/contents';
8+
const baseUrl = 'https://api.github.com/repos/ml5js/ml5-library/contents';
109
const clientId = process.env.GITHUB_ID;
1110
const clientSecret = process.env.GITHUB_SECRET;
12-
const editorUsername = process.env.ML5_EXAMPLES_USERNAME;
11+
const editorUsername = process.env.ML5_LIBRARY_USERNAME;
1312
const personalAccessToken = process.env.EDITOR_API_ACCESS_TOKEN;
1413
const editorApiUrl = process.env.EDITOR_API_URL;
1514
const headers = {
@@ -18,7 +17,7 @@ const headers = {
1817

1918
ok(clientId, 'GITHUB_ID is required');
2019
ok(clientSecret, 'GITHUB_SECRET is required');
21-
ok(editorUsername, 'ML5_EXAMPLES_USERNAME is required');
20+
ok(editorUsername, 'ML5_LIBRARY_USERNAME is required');
2221
ok(personalAccessToken, 'EDITOR_API_ACCESS_TOKEN is required');
2322
ok(editorApiUrl, 'EDITOR_API_URL is required');
2423

@@ -79,17 +78,24 @@ async function fetchFileContent(item) {
7978
options.url !== ''
8079
) {
8180
file.content = await rp(options);
81+
8282
// NOTE: remove the URL property if there's content
8383
// Otherwise the p5 editor will try to pull from that url
8484
if (file.content !== null) delete file.url;
85+
86+
// Replace localhost references with references to the currently published version.
87+
file.content = file.content.replace(
88+
/http:\/\/localhost(:[0-9]+)\/ml5.js/g,
89+
'https://unpkg.com/ml5@latest/dist/ml5.min.js'
90+
);
8591
}
8692

8793
return file;
8894
// if it is NOT an html or js file
8995
}
9096

9197
if (file.url) {
92-
const cdnRef = `https://cdn.jsdelivr.net/gh/ml5js/ml5-examples@${branchName}${
98+
const cdnRef = `https://cdn.jsdelivr.net/gh/ml5js/ml5-library@${branchName}${
9399
file.url.split(branchName)[1]
94100
}`;
95101
file.url = cdnRef;
@@ -104,7 +110,7 @@ async function fetchFileContent(item) {
104110
async function getCategories() {
105111
try {
106112
const options = Object.assign({}, githubRequestOptions);
107-
options.url = `${options.url}/p5js${branchRef}`;
113+
options.url = `${options.url}/examples/p5js${branchRef}`;
108114
const results = await rp(options);
109115

110116
return results;
@@ -125,7 +131,7 @@ async function getCategoryExamples(sketchRootList) {
125131
// let options = Object.assign({url: `${requestOptions.url}/${categories.path}${branchRef}`}, requestOptions)
126132
const options = Object.assign({}, githubRequestOptions);
127133
options.url = `${options.url}${categories.path}${branchRef}`;
128-
// console.log(options)
134+
129135
const sketchDirs = await rp(options);
130136

131137
try {
@@ -158,7 +164,7 @@ async function traverseSketchTree(parentObject) {
158164
if (parentObject.type !== 'dir') {
159165
return output;
160166
}
161-
// let options = `https://api.github.com/repos/ml5js/ml5-examples/contents/${sketches.path}${branchRef}`
167+
// let options = `https://api.github.com/repos/ml5js/ml5-library/contents/examples/p5js/${sketches.path}${branchRef}`
162168
const options = Object.assign({}, githubRequestOptions);
163169
options.url = `${options.url}${parentObject.path}${branchRef}`;
164170

@@ -353,7 +359,7 @@ async function createProjectsInP5User(filledProjectList, user) {
353359

354360
/**
355361
* MAKE
356-
* Get all the sketches from the ml5-examples repo
362+
* Get all the sketches from the ml5-library repo
357363
* Get the p5 examples
358364
* Dive down into each sketch and get all the files
359365
* Format the sketch files to be save to the db
@@ -378,7 +384,7 @@ async function make() {
378384

379385
/**
380386
* TEST - same as make except reads from file for testing purposes
381-
* Get all the sketches from the ml5-examples repo
387+
* Get all the sketches from the ml5-library repo
382388
* Get the p5 examples
383389
* Dive down into each sketch and get all the files
384390
* Format the sketch files to be save to the db
@@ -414,7 +420,7 @@ async function test() {
414420
*/
415421

416422
if (process.env.NODE_ENV === 'development') {
417-
// test()
423+
// test();
418424
make(); // replace with test() if you don't want to run all the fetch functions over and over
419425
} else {
420426
make();

0 commit comments

Comments
 (0)