-
Notifications
You must be signed in to change notification settings - Fork 6
/
extract-source.js
56 lines (39 loc) · 1.21 KB
/
extract-source.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
const fs = require("fs"),
path = require("path")
let exampleScripts = {}
const files = fs.readdirSync(path.join("src", "examples"))
// console.log(files)
for (let f of files) {
if (f === "sketches.ts") continue
// console.log(`Process: ${f}\n\n`)
const filePath = path.join("src", "examples", f)
const source = fs.readFileSync(filePath).toString()
//ha!
const matches = source.match(
/const .*? = \([sp]: SCanvas\) => {(.|[\n])*?(?=(const .*? = \([sp]: SCanvas\) =>)|(const sketches: {))/gim
)
const examples = {}
matches.forEach((m) => {
const name = m.match(/const (.*?)(?= )/)[1]
const code = m
examples[name] = code
})
// console.log(examples)
const names = {}
source
.match(/{ sketch: ([^,]*), name: "([^"]*)" }/gi)
.map((nm) => nm.match(/{ sketch: ([^,]*), name: "([^"]*)" }/i).slice(1, 3))
.forEach(([n, l]) => (names[n] = l))
// console.log(names)
const esForF = {}
Object.keys(names).forEach((name) => {
esForF[names[name]] = examples[name]
})
exampleScripts[f] = esForF
// console.log(`Done: ${f}\n\n`)
}
// console.log(exampleScripts)
fs.writeFileSync(
path.join("src", "data", "source.json"),
JSON.stringify(exampleScripts, null, 2)
)