Skip to content

Commit 81264a5

Browse files
committed
[#975] Updated examples-gg-latest.js
1 parent c7f94f7 commit 81264a5

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

server/scripts/examples-gg-latest.js

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import rp from 'request-promise';
1+
import axios from 'axios';
22
import Q from 'q';
33
import mongoose from 'mongoose';
44
import objectID from 'bson-objectid';
@@ -106,7 +106,7 @@ const insert = function insert(_mainString, _insString, _pos) {
106106
/* --- data processing --- */
107107
// 1. first get the top level directories P and M
108108
// https://api.github.com/repos/generative-design/Code-Package-p5.js/contents?ref=pre-release
109-
function getCodePackage() {
109+
async function getCodePackage() {
110110
const sketchRootList = [];
111111
const options = {
112112
// url: 'https://api.github.com/repos/generative-design/Code-Package-p5.js/contents',
@@ -118,34 +118,31 @@ function getCodePackage() {
118118
Authorization: `Basic ${Buffer.from(
119119
`${clientId}:${clientSecret}`
120120
).toString('base64')}`
121-
},
122-
json: true
121+
}
123122
};
124123

125-
return rp(options)
126-
.then((res) => {
127-
res.forEach((metadata) => {
128-
if (
129-
metadata.name.endsWith('P') === true ||
130-
metadata.name.endsWith('M') === true
131-
) {
132-
sketchRootList.push(metadata);
133-
}
134-
});
135-
136-
return sketchRootList;
137-
})
138-
.catch((err) => {
139-
throw err;
124+
try {
125+
const { data } = await axios.request(options);
126+
data.forEach((metadata) => {
127+
if (
128+
metadata.name.endsWith('P') === true ||
129+
metadata.name.endsWith('M') === true
130+
) {
131+
sketchRootList.push(metadata);
132+
}
140133
});
134+
return sketchRootList;
135+
} catch (err) {
136+
throw err;
137+
}
141138
}
142139

143140
// 2. get the list of all the top-level sketch directories in P and M
144141
function getSketchDirectories(sketchRootList) {
145142
// console.log(sketchRootList);
146143

147144
return Q.all(
148-
sketchRootList.map((sketches) => {
145+
sketchRootList.map(async (sketches) => {
149146
// console.log(sketches)
150147
const options = {
151148
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`,
@@ -155,19 +152,16 @@ function getSketchDirectories(sketchRootList) {
155152
Authorization: `Basic ${Buffer.from(
156153
`${clientId}:${clientSecret}`
157154
).toString('base64')}`
158-
},
159-
json: true
155+
}
160156
};
161157

162-
return rp(options)
163-
.then((res) => {
164-
const sketchDirs = flatten(res);
165-
166-
return sketchDirs;
167-
})
168-
.catch((err) => {
169-
throw err;
170-
});
158+
try {
159+
const { data } = await axios.request(options);
160+
const sketchDirs = flatten(data);
161+
return sketchDirs;
162+
} catch (err) {
163+
throw err;
164+
}
171165
})
172166
).then((output) => {
173167
const sketchList = [];
@@ -186,7 +180,7 @@ function getSketchDirectories(sketchRootList) {
186180
// 3. For each sketch item in the sketchList, append the tree contents to each item
187181
function appendSketchItemLinks(sketchList) {
188182
return Q.all(
189-
sketchList.map((sketches) => {
183+
sketchList.map(async (sketches) => {
190184
const options = {
191185
// url: `${sketches.url}?client_id=${clientId}&client_secret=${clientSecret}`,
192186
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`,
@@ -196,15 +190,16 @@ function appendSketchItemLinks(sketchList) {
196190
Authorization: `Basic ${Buffer.from(
197191
`${clientId}:${clientSecret}`
198192
).toString('base64')}`
199-
},
200-
json: true
193+
}
201194
};
202195

203-
return rp(options).then((res) => {
204-
sketches.tree = res;
205-
196+
try {
197+
const { data } = await axios.request(options);
198+
sketches.tree = data;
206199
return sketchList;
207-
});
200+
} catch (err) {
201+
throw err;
202+
}
208203
})
209204
);
210205
}
@@ -214,24 +209,24 @@ function getSketchItems(sketchList) {
214209
// const completeSketchPkg = [];
215210

216211
/* eslint-disable */
217-
return Q.all(sketchList[0].map(sketch => Q.all(sketch.tree.map((item) => {
212+
return Q.all(sketchList[0].map(async sketch => Q.all(sketch.tree.map((item) => {
218213
if (item.name === 'data') {
219214
const options = {
220215
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${item.path}${branchRef}`,
221216
method: 'GET',
222217
headers: {
223218
...headers,
224219
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
225-
},
226-
json: true
220+
}
227221
};
228222

229-
return rp(options).then((res) => {
230-
sketch.data = res;
223+
try {
224+
const { data } = axios.request(options);
225+
sketch.data = data;
231226
return sketch;
232-
}).catch((err) => {
227+
} catch (err) {
233228
throw err;
234-
});
229+
}
235230
}
236231
// pass
237232
})))).then(() => sketchList[0]);
@@ -399,7 +394,7 @@ function formatAllSketches(sketchList) {
399394
// get all the sketch data content and download to the newProjects array
400395
function getAllSketchContent(newProjectList) {
401396
/* eslint-disable */
402-
return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map((sketchFile, i) => {
397+
return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map(async (sketchFile, i) => {
403398
/*
404399
sketchFile.name.endsWith(".mp4") !== true &&
405400
sketchFile.name.endsWith(".ogg") !== true &&
@@ -427,12 +422,13 @@ function getAllSketchContent(newProjectList) {
427422
};
428423

429424
// console.log("CONVERT ME!")
430-
return rp(options).then((res) => {
431-
newProject.files[i].content = res;
425+
try {
426+
const { data } = await axios.request(options);
427+
newProject.files[i].content = data;
432428
return newProject;
433-
}).catch((err) => {
429+
} catch (err) {
434430
throw err;
435-
});
431+
}
436432
}
437433
if (newProject.files[i].url) {
438434
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)