forked from ergogen/ergogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·126 lines (102 loc) · 2.78 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs-extra')
const path = require('path')
const yaml = require('js-yaml')
const yargs = require('yargs')
const ergogen = require('./ergogen')
const pkg = require('../package.json')
// command line args
const args = yargs
.option('output', {
alias: 'o',
default: path.resolve('output'),
describe: 'Output folder',
type: 'string'
})
.option('debug', {
alias: 'd',
default: false,
describe: 'Debug mode',
type: 'boolean'
})
.option('clean', {
default: false,
describe: 'Clean output dir before parsing',
type: 'boolean'
})
.argv
// config reading
const config_file = args._[0]
if (!config_file) {
console.error('Usage: ergogen <config_file> [options]')
process.exit(1)
}
let config_text
try {
config_text = fs.readFileSync(config_file).toString()
} catch (err) {
console.error(`Could not read config file "${config_file}": ${err}`)
process.exit(2)
}
const title_suffix = args.debug ? ' (Debug Mode)' : ''
console.log(`Ergogen v${pkg.version} CLI${title_suffix}`)
console.log()
;(async () => {
// processing
let results
try {
results = await ergogen.process(config_text, args.debug, s => console.log(s))
} catch (err) {
console.error(err)
process.exit(3)
}
// helpers
const single = (data, rel) => {
if (!data) return
const abs = path.join(args.o, rel)
fs.mkdirpSync(path.dirname(abs))
if (abs.endsWith('.yaml')) {
fs.writeFileSync(abs, yaml.dump(data, {indent: 4}))
} else {
fs.writeFileSync(abs, data)
}
}
const composite = (data, rel) => {
if (!data) return
const abs = path.join(args.o, rel)
if (data.yaml) {
fs.mkdirpSync(path.dirname(abs))
fs.writeFileSync(abs + '.yaml', yaml.dump(data.yaml, {indent: 4}))
}
for (const format of ['svg', 'dxf', 'jscad', 'stl']) {
if (data[format]) {
fs.mkdirpSync(path.dirname(abs))
fs.writeFileSync(abs + '.' + format, data[format])
}
}
}
// output
if (args.clean) {
console.log('Cleaning output folder...')
fs.removeSync(args.o)
}
console.log('Writing output to disk...')
fs.mkdirpSync(args.o)
single(results.raw, 'source/raw.txt')
single(results.canonical, 'source/canonical.yaml')
single(results.units, 'points/units.yaml')
single(results.points, 'points/points.yaml')
composite(results.demo, 'points/demo')
for (const [name, outline] of Object.entries(results.outlines)) {
composite(outline, `outlines/${name}`)
}
for (const [name, _case] of Object.entries(results.cases)) {
composite(_case, `cases/${name}`)
}
for (const [name, pcb] of Object.entries(results.pcbs)) {
single(pcb, `pcbs/${name}.kicad_pcb`)
}
// goodbye
console.log('Done.')
console.log()
})()