ergogen/src/cli.js

127 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-06-26 21:00:10 +02:00
#!/usr/bin/env node
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')
2021-01-01 00:50:04 +01:00
const ergogen = require('./ergogen')
const pkg = require('../package.json')
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('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
// config reading
2020-05-28 22:18:41 +02:00
const config_file = args._[0]
if (!config_file) {
console.error('Usage: ergogen <config_file> [options]')
process.exit(1)
}
2021-01-01 00:50:04 +01:00
2020-08-11 22:28:23 +02:00
let config_text
try {
config_text = fs.readFileSync(config_file).toString()
2020-08-11 22:28:23 +02:00
} catch (err) {
console.error(`Could not read config file "${config_file}": ${err}`)
process.exit(2)
2020-08-11 22:28:23 +02:00
}
const title_suffix = args.debug ? ' (Debug Mode)' : ''
console.log(`Ergogen v${pkg.version} CLI${title_suffix}`)
console.log()
;(async () => {
// processing
let results
2020-06-28 22:35:53 +02:00
try {
results = await ergogen.process(config_text, args.debug, s => console.log(s))
2020-06-28 22:35:53 +02:00
} catch (err) {
console.error(err)
process.exit(3)
2020-06-28 22:35:53 +02:00
}
// 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)
}
}
2021-01-01 00:50:04 +01:00
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])
}
}
}
2021-01-01 00:50:04 +01:00
// output
if (args.clean) {
console.log('Cleaning output folder...')
fs.removeSync(args.o)
}
2021-01-01 00:50:04 +01:00
console.log('Writing output to disk...')
fs.mkdirpSync(args.o)
2020-05-30 15:17:53 +02:00
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')
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)) {
composite(outline, `outlines/${name}`)
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)) {
composite(_case, `cases/${name}`)
2021-01-01 21:46:01 +01:00
}
for (const [name, pcb] of Object.entries(results.pcbs)) {
single(pcb, `pcbs/${name}.kicad_pcb`)
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.')
console.log()
})()