This repository was archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathupdate-examples-json.js
93 lines (74 loc) · 2.39 KB
/
update-examples-json.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
const fs = require('fs');
const path = require('path');
const recursive = require("recursive-readdir");
const { parse } = require('node-html-parser');
const baseurl = path.resolve(__dirname, "..");
const ghUrl = 'https://ml5js.github.io/ml5-examples';
const weUrl = 'https://editor.p5js.org/ml5/sketches';
// Get all the references
const p5Examples = getReferences(`${baseurl}/p5js`, 'p5js', ghUrl, weUrl);
const plainJsExamples = getReferences(`${baseurl}/javascript`, 'javascript', ghUrl);
const d3Examples = getReferences(`${baseurl}/d3`, 'd3', ghUrl);
appendToSource(p5Examples, plainJsExamples);
appendToSource(p5Examples, d3Examples);
console.log(`Created/update examples index json!!`)
// flatten the array to a json where each example parent is the key
let output = {};
p5Examples.forEach(item => {
output[item.parent] = item.children
});
// write out to file
fs.writeFileSync(`${baseurl}/examples.json`, JSON.stringify(output), 'utf8')
// TODO: functional javascription would be great!
function appendToSource(_source,_arr){
// Merge all of these examples to one file
_arr.forEach(item => {
const {parent, children} = item;
// check if it exists
const match = _source.map( proj => proj.parent ).indexOf(parent);
if(match >= 0){
Object.entries(children).forEach( child => {
const k = child[0];
const v = child[1];
_source[match].children[k] = v;
})
}
})
}
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory();
});
}
function getReferences(_examplesRoot, _rootName, _ghUrl, _weUrl = null){
const dirs = getDirectories(_examplesRoot);
const subdirs = dirs.map(dir => {
let items = getDirectories(`${_examplesRoot}/${dir}`);
const ghItems = items.map(item => {
return {
name: item,
url:`${_ghUrl}/${_rootName}/${dir}/${item}`}
});
if(_weUrl !== null){
const weItems = items.map(item => {
return {
name: item,
url: `${_weUrl}/${item}`
}
});
return {
parent: dir,
children: {
[_rootName]: ghItems,
"p5webeditor": weItems
}}
} else {
return {
parent: dir,
children: {
[_rootName]: ghItems
}}
}
});
return subdirs;
}