ergogen/src/cli.js

90 lines
2.2 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
2020-06-26 21:00:10 +02:00
const u = require('./utils')
2020-06-28 22:35:53 +02:00
const io = require('./io')
2020-06-26 21:00:10 +02:00
const points_lib = require('./points')
2020-07-17 23:20:49 +02:00
const outlines_lib = require('./outlines')
const pcbs_lib = require('./pcbs')
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
2020-06-26 21:00:10 +02:00
const config_parser = args.c.endsWith('.yaml') ? yaml.load : JSON.parse
2020-06-28 22:35:53 +02:00
let config
try {
config = config_parser(fs.readFileSync(args.c).toString())
} catch (err) {
throw new Error(`Malformed input "${args.c}": ${err}`)
}
// points
2020-05-30 15:17:53 +02:00
2020-06-28 22:35:53 +02:00
console.log('Parsing points...')
2020-06-26 21:00:10 +02:00
const points = points_lib.parse(config.points)
2020-05-30 15:17:53 +02:00
if (args.debug) {
2020-06-28 22:35:53 +02:00
const rect = u.rect(18, 18, [-9, -9])
const points_demo = points_lib.position(points, rect)
2020-07-04 23:23:14 +02:00
io.dump_model(points_demo, path.join(args.o, 'points/points_demo'), args.debug)
fs.writeJSONSync(path.join(args.o, 'points/points.json'), points, {spaces: 4})
2020-05-31 22:59:51 +02:00
}
2020-06-07 13:40:26 +02:00
2020-06-28 22:35:53 +02:00
// outlines
2020-07-04 23:23:14 +02:00
console.log('Generating outlines...')
2020-07-17 23:20:49 +02:00
const outlines = outlines_lib.parse(config.outlines, points)
2020-07-04 23:23:14 +02:00
for (const [name, outline] of Object.entries(outlines)) {
if (!args.debug && name.startsWith('_')) continue
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
2020-07-17 23:20:49 +02:00
// pcbs
2020-07-11 20:30:20 +02:00
2020-07-17 23:20:49 +02:00
console.log('Scaffolding PCBs...')
const pcbs = pcbs_lib.parse(config.pcbs, points, outlines)
for (const [pcb_name, pcb_text] of Object.entries(pcbs)) {
const pcb_file = path.join(args.o, `pcbs/${pcb_name}.kicad_pcb`)
fs.mkdirpSync(path.dirname(pcb_file))
fs.writeFileSync(pcb_file, pcb_text)
}
2020-07-11 20:30:20 +02:00
2020-06-28 22:35:53 +02:00
// goodbye
2020-06-16 22:24:46 +02:00
console.log('Done.')