forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostInstall.js
More file actions
73 lines (65 loc) · 2.42 KB
/
postInstall.js
File metadata and controls
73 lines (65 loc) · 2.42 KB
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
// this copies the examples folder from the @jscad/examples repository locally
const path = require('path')
const examplesSrc = path.resolve('./node_modules/@jscad/examples')
const copydir = require('copy-dir')
const fs = require('fs')
const DocBlock = require('docblock')
const docBlock = new DocBlock()
const ignoreExamples = { Imports: 1, Projects: 1 }
const copyAndProcessExamples = (examplesSrc) => {
const examples = { 'Creating Shapes': [], 'Manipulating Shapes': [], Colors: [], Parameters: [], Other: [] }
const examplesDist = 'examples'
if (fs.existsSync(examplesSrc)) {
console.log('Copying Examples...', examplesSrc)
copydir.sync(examplesSrc, examplesDist, { mode: false })
processExamplesInDirectory(examplesDist, examples)
sortExamples(examples)
fs.writeFile('examples/examples.json', JSON.stringify(examples), (err) => {
if (err) return console.log(err)
})
} else {
return console.log('Examples directory does not exist: ' + examplesSrc)
}
}
const processExamplesInDirectory = (dir, examples) => {
const files = fs.readdirSync(dir)
files.forEach((fileName) => {
const filePath = path.join(dir, fileName)
if (fs.lstatSync(filePath).isDirectory()) {
processExamplesInDirectory(filePath, examples)
} else if (filePath.endsWith('.js')) {
processExamplesFile(filePath, examples)
}
})
}
const processExamplesFile = (filePath, examples) => {
const result = docBlock.parse(fs.readFileSync(filePath), 'js')
if (result.length) {
const category = result[0].tags.category
if (category in ignoreExamples) {
console.log(`Ignoring example ${filePath}`)
return
}
const title = result[0].title
const description = result[0].description
const sort = parseInt(result[0].tags.skillLevel)
if (!(category in examples)) {
const categories = Object.keys(examples)
console.error(`ERR: Example ${filePath} did not have a valid category set. Valid categories are ${categories}`)
return
}
examples[category].push({ title, filePath, sort, description })
}
}
const sortExamples = (examples) => {
for (const category in examples) {
if (!Object.prototype.hasOwnProperty.call(examples, category)) continue
examples[category] = examples[category].sort((a, b) => {
if (a.sort === b.sort) {
return a.title > b.title ? 1 : -1
}
return a.sort - b.sort
})
}
}
copyAndProcessExamples(examplesSrc)