-
Notifications
You must be signed in to change notification settings - Fork 103
/
updateExamples.js
155 lines (135 loc) · 4.25 KB
/
updateExamples.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
const fs = require('fs-extra');
const path = require('path');
const glob = require('fast-glob');
const inquirer = require('inquirer');
const { exampleSlug } = require('../src/utils/paths');
const from = path.join(__dirname, '..', '..', 'processing-examples');
const to = path.join(__dirname, '..', 'content', 'examples');
const staticFolder = path.join(__dirname, '..', 'static');
/**
This script updates the Processing code for the examples
**/
const updateExamples = async () => {
if (!examplesRepoExists()) {
console.error(
'To run this script, you must have the processing-examples repo next to the processing-website repo on your computer.'
);
return;
}
// Find examples in both the examples repo and this repo
const fromExamples = findExamples(from);
const toExamples = findExamples(to);
const missingFrom = diffExamples(toExamples, fromExamples);
const missingTo = diffExamples(fromExamples, toExamples);
const portExamples = fromExamples.filter(
(example) => !missingTo.includes(example)
);
if (missingTo.length > 0) {
console.log(
'Examples from processing-examples that are not in the website:'
);
console.log('(These need to be manually added to the website)');
missingTo.map((example) => console.log(`- ${example.path}`));
console.log('');
}
if (missingFrom.length > 0) {
console.log(
'Examples from the website that are not in processing-examples:'
);
console.log('(These should probably be deleted from the website)');
missingFrom.map((example) => console.log(`- ${example.path}`));
console.log('');
}
if (portExamples.length > 0) {
console.log(
'Examples that are in both repos and will be updated with the script:'
);
portExamples.map((example) => console.log(`- ${example.path}`));
console.log('');
}
const answers = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: 'Do you wish to update the content stated above?'
}
]);
if (!answers.confirm) {
return;
}
// Loop through and move over all files within the examples
for (let i = 0; i < portExamples.length; i++) {
const example = portExamples[i];
// Copy all .pde files to the example folder
const pdes = glob.sync('*.pde', {
cwd: path.join(from, example.dirname)
});
for (let i = 0; i < pdes.length; i++) {
const pde = pdes[i];
fs.copySync(
path.join(from, example.dirname, pde),
path.join(to, example.dirname, pde)
);
}
// Copy all data files to the static/livesketch/SLUG folder
const dataFiles = glob.sync('data/**/*.*', {
cwd: path.join(from, example.dirname)
});
for (let i = 0; i < dataFiles.length; i++) {
const dataFile = dataFiles[i];
fs.copySync(
path.join(from, example.dirname, dataFile),
path.join(
staticFolder,
'livesketch',
exampleSlug(example.name),
path.relative('data', dataFile)
)
);
}
}
console.log('Examples updated!');
};
/**
Finds all main example files that are of format CATEGORY/SUBCATEGORY/EXAMPLE/EXAMPLE.pde
**/
const findExamples = (folder) => {
const files = glob.sync('**/*.pde', { cwd: folder });
const examples = [];
files.forEach((file) => {
const split = file.split(path.sep);
const basename = path.basename(file, '.pde');
// If the file is in the right nested folder structure
// and the file is named the same as the parent.
if (split.length === 4 && split[2] === basename) {
examples.push({
category: split[0],
subcategory: split[1],
path: file,
name: basename,
dirname: path.dirname(file)
});
}
});
return examples;
};
/**
Finds all the examples from examples1 missing from examples2
**/
const diffExamples = (examples1, examples2) => {
const missing = [];
loop1: for (let i = 0; i < examples1.length; i++) {
for (let j = 0; j < examples2.length; j++) {
if (examples1[i].path === examples2[j].path) {
continue loop1;
}
}
missing.push(examples1[i]);
}
return missing;
};
/**
Checks whether the processing-contributions repo is next to this repo
**/
const examplesRepoExists = (keywords) => fs.existsSync(from);
updateExamples();