ergogen/src/cli.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-06-26 21:00:10 +02:00
#!/usr/bin/env node
2020-06-28 22:35:53 +02:00
// libs
2020-06-07 13:40:26 +02:00
const fs = require('fs-extra')
const path = require('path')
const yaml = require('js-yaml')
2020-05-28 22:18:41 +02:00
const yargs = require('yargs')
2020-06-07 13:40:26 +02:00
2020-06-28 22:35:53 +02:00
// internals
const io = require('./io')
2021-01-01 00:50:04 +01:00
const ergogen = require('./ergogen')
2020-05-30 15:17:53 +02:00
2020-06-28 22:35:53 +02:00
// command line args
2020-06-26 21:00:10 +02:00
2020-05-28 22:18:41 +02:00
const args = yargs
2020-06-07 13:40:26 +02:00
.option('config', {
alias: 'c',
demandOption: true,
2020-06-28 22:35:53 +02:00
describe: 'Config yaml/json file',
2020-06-07 13:40:26 +02:00
type: 'string'
})
.option('output', {
alias: 'o',
default: path.resolve('output'),
describe: 'Output folder',
type: 'string'
})
2020-05-30 15:17:53 +02:00
.option('debug', {
2020-06-28 22:35:53 +02:00
alias: 'd',
2020-05-28 22:18:41 +02:00
default: false,
2020-06-28 22:35:53 +02:00
describe: 'Debug mode',
2020-05-30 15:17:53 +02:00
type: 'boolean'
})
2020-07-12 23:23:30 +02:00
.option('clean', {
default: false,
describe: 'Clean output dir before parsing',
type: 'boolean'
})
2020-06-07 13:40:26 +02:00
.argv
2020-07-12 23:23:30 +02:00
if (args.clean) fs.removeSync(args.o)
2020-06-26 21:00:10 +02:00
fs.mkdirpSync(args.o)
2020-05-28 22:18:41 +02:00
2021-01-01 00:50:04 +01:00
// config parsing
2020-08-11 22:28:23 +02:00
let config_text
try {
config_text = fs.readFileSync(args.c).toString()
} catch (err) {
throw new Error(`Could not read file "${args.c}": ${err}`)
}
const is_yaml = args.c.endsWith('.yaml') || args.c.endsWith('.yml')
const config_parser = is_yaml ? yaml.load : JSON.parse
2020-06-28 22:35:53 +02:00
let config
try {
2020-08-11 22:28:23 +02:00
config = config_parser(config_text)
2020-06-28 22:35:53 +02:00
} catch (err) {
2020-08-11 22:28:23 +02:00
throw new Error(`Malformed input within "${args.c}": ${err}`)
2020-06-28 22:35:53 +02:00
}
2021-01-01 00:50:04 +01:00
// processing
const results = ergogen.process(config, args.debug, s => console.log(s))
// output
console.log('Writing output to disk...')
2020-05-30 15:17:53 +02:00
if (args.debug) {
io.dump_model(results.demo, path.join(args.o, 'points/demo'), args.debug)
fs.writeJSONSync(path.join(args.o, 'points/data.json'), results.points, {spaces: 4})
2020-05-31 22:59:51 +02:00
}
2020-06-07 13:40:26 +02:00
2021-01-01 00:50:04 +01:00
for (const [name, outline] of Object.entries(results.outlines)) {
2020-07-17 23:20:49 +02:00
io.dump_model(outline, path.join(args.o, `outlines/${name}`), args.debug)
2020-07-04 23:23:14 +02:00
}
2020-06-28 22:35:53 +02:00
2021-01-01 21:46:01 +01:00
for (const [name, _case] of Object.entries(results.cases)) {
const file = path.join(args.o, `cases/${name}.jscad`)
fs.mkdirpSync(path.dirname(file))
fs.writeFileSync(file, _case)
}
for (const [name, pcb] of Object.entries(results.pcbs)) {
const file = path.join(args.o, `pcbs/${name}.kicad_pcb`)
fs.mkdirpSync(path.dirname(file))
fs.writeFileSync(file, pcb)
}
2020-08-11 20:00:35 +02:00
2021-01-01 00:50:04 +01:00
if (args.debug) {
fs.writeJSONSync(path.join(args.o, 'results.json'), results, {spaces: 4})
2020-08-11 20:00:35 +02:00
}
2020-06-28 22:35:53 +02:00
// goodbye
2020-06-16 22:24:46 +02:00
console.log('Done.')