CLI and output restructure, SVG/DXF/STL integration
This commit is contained in:
parent
c49881c2b4
commit
42a3e2de55
7 changed files with 730 additions and 154 deletions
113
src/cli.js
Normal file → Executable file
113
src/cli.js
Normal file → Executable file
|
@ -1,26 +1,15 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// libs
|
||||
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const yaml = require('js-yaml')
|
||||
const yargs = require('yargs')
|
||||
|
||||
// internals
|
||||
|
||||
const io = require('./io')
|
||||
const ergogen = require('./ergogen')
|
||||
const pkg = require('../package.json')
|
||||
|
||||
// command line args
|
||||
|
||||
const args = yargs
|
||||
.option('config', {
|
||||
alias: 'c',
|
||||
demandOption: true,
|
||||
describe: 'Config yaml/json file',
|
||||
type: 'string'
|
||||
})
|
||||
.option('output', {
|
||||
alias: 'o',
|
||||
default: path.resolve('output'),
|
||||
|
@ -40,60 +29,100 @@ const args = yargs
|
|||
})
|
||||
.argv
|
||||
|
||||
if (args.clean) fs.removeSync(args.o)
|
||||
fs.mkdirpSync(args.o)
|
||||
// config reading
|
||||
|
||||
// config parsing
|
||||
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(args.c).toString()
|
||||
config_text = fs.readFileSync(config_file).toString()
|
||||
} catch (err) {
|
||||
throw new Error(`Could not read file "${args.c}": ${err}`)
|
||||
console.error(`Could not read config file "${config_file}": ${err}`)
|
||||
process.exit(2)
|
||||
}
|
||||
|
||||
const is_yaml = args.c.endsWith('.yaml') || args.c.endsWith('.yml')
|
||||
const config_parser = is_yaml ? yaml.load : JSON.parse
|
||||
let config
|
||||
try {
|
||||
config = config_parser(config_text)
|
||||
} catch (err) {
|
||||
throw new Error(`Malformed input within "${args.c}": ${err}`)
|
||||
}
|
||||
const title_suffix = args.debug ? ' (Debug Mode)' : ''
|
||||
console.log(`Ergogen v${pkg.version} CLI${title_suffix}`)
|
||||
console.log()
|
||||
|
||||
;(async () => {
|
||||
|
||||
// processing
|
||||
|
||||
const results = ergogen.process(config, args.debug, s => console.log(s))
|
||||
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('.json')) {
|
||||
fs.writeJSONSync(abs, data, {spaces: 4})
|
||||
} else 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.json) {
|
||||
fs.mkdirpSync(path.dirname(abs))
|
||||
fs.writeJSONSync(abs + '.json', data.json, {spaces: 4})
|
||||
}
|
||||
for (const format of ['svg', 'dxf', 'jscad', 'stl']) {
|
||||
if (data[format]) {
|
||||
fs.mkdirpSync(path.dirname(abs))
|
||||
fs.writeFileSync(abs + '.' + format, data[format])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// output
|
||||
|
||||
console.log('Writing output to disk...')
|
||||
|
||||
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})
|
||||
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, 'raw.txt')
|
||||
single(results.canonical, 'canonical.yaml')
|
||||
single(results.units, 'units.json')
|
||||
|
||||
single(results.points, 'points/points.json')
|
||||
composite(results.demo, 'points/demo')
|
||||
|
||||
for (const [name, outline] of Object.entries(results.outlines)) {
|
||||
io.dump_model(outline, path.join(args.o, `outlines/${name}`), args.debug)
|
||||
composite(outline, `outlines/${name}`)
|
||||
}
|
||||
|
||||
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)
|
||||
composite(_case, `cases/${name}`)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if (args.debug) {
|
||||
fs.writeJSONSync(path.join(args.o, 'results.json'), results, {spaces: 4})
|
||||
single(pcb, `pcbs/${name}.kicad_pcb`)
|
||||
}
|
||||
|
||||
// goodbye
|
||||
|
||||
console.log('Done.')
|
||||
console.log()
|
||||
|
||||
})()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue