|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const util = require('util') |
| 6 | +const common = require('common-prefix') |
| 7 | +const {get} = require('dot-prop') |
| 8 | +const glob = require('globby') |
| 9 | +const makeDir = require('make-dir') |
| 10 | +const debug = require('debug')('@tracespace/cli') |
| 11 | + |
| 12 | +const gerberToSvg = require('gerber-to-svg') |
| 13 | +const pcbStackup = require('pcb-stackup') |
| 14 | +const whatsThatGerber = require('whats-that-gerber') |
| 15 | +const yargs = require('yargs') |
| 16 | + |
| 17 | +const {description, version} = require('../package.json') |
| 18 | +const examples = require('./examples') |
| 19 | +const options = require('./options') |
| 20 | +const resolve = require('./resolve') |
| 21 | + |
| 22 | +const writeFile = util.promisify(fs.writeFile) |
| 23 | +const stackup = util.promisify(pcbStackup) |
| 24 | + |
| 25 | +module.exports = function cli (processArgv, config) { |
| 26 | + const argv = yargs |
| 27 | + .usage('$0 [options] <files...>', `${description}\nv${version}`, yargs => { |
| 28 | + yargs.positional('files', { |
| 29 | + coerce: files => files.map(resolve), |
| 30 | + describe: |
| 31 | + "Filenames, directories, or globs to a PCB's Gerber/drill files", |
| 32 | + type: 'string' |
| 33 | + }) |
| 34 | + |
| 35 | + examples.forEach(e => yargs.example(e.cmd, e.desc)) |
| 36 | + |
| 37 | + yargs.epilog( |
| 38 | + `You may also specify options in the current working directory using a config file in (.tracespacerc, .tracespacerc.json, tracespace.config.js, etc.) or a "tracespace" key in package.json` |
| 39 | + ) |
| 40 | + }) |
| 41 | + .config(config) |
| 42 | + .options(options) |
| 43 | + .version() |
| 44 | + .help() |
| 45 | + .alias({help: 'h', version: 'v'}) |
| 46 | + .fail((error, message, yargs) => { |
| 47 | + throw new Error(error) |
| 48 | + }) |
| 49 | + .parse(processArgv) |
| 50 | + |
| 51 | + debug('argv', argv) |
| 52 | + |
| 53 | + const info = message => !argv.quiet && console.warn(message) |
| 54 | + |
| 55 | + if (config._configFile) info(`Config loaded from ${config._configFile}`) |
| 56 | + |
| 57 | + return glob(argv.files) |
| 58 | + .then(renderFiles) |
| 59 | + .then(writeRenders) |
| 60 | + |
| 61 | + function renderFiles (filenames) { |
| 62 | + const layers = filenames.map(makeLayerFromFilename).filter(_ => _) |
| 63 | + |
| 64 | + if (!layers.length) throw new Error(`No valid Gerber or drill files found`) |
| 65 | + |
| 66 | + return stackup(layers, argv.board) |
| 67 | + } |
| 68 | + |
| 69 | + function makeLayerFromFilename (filename) { |
| 70 | + const basename = path.basename(filename) |
| 71 | + const type = getType(basename) |
| 72 | + |
| 73 | + if (type === 'drw' && !argv.force) { |
| 74 | + info(`Skipping ${basename} (unable to identify type)`) |
| 75 | + return null |
| 76 | + } |
| 77 | + |
| 78 | + info(`Rendering ${basename} as ${whatsThatGerber.getFullName(type)}`) |
| 79 | + |
| 80 | + const gerber = fs.createReadStream(filename) |
| 81 | + const options = getOptions(basename, type) |
| 82 | + |
| 83 | + debug(filename, type, options) |
| 84 | + |
| 85 | + return {type, gerber, options, filename: basename} |
| 86 | + } |
| 87 | + |
| 88 | + function getType (basename) { |
| 89 | + const defaultType = whatsThatGerber(basename) |
| 90 | + |
| 91 | + return get(argv.layer, `${basename}.type`, defaultType) |
| 92 | + } |
| 93 | + |
| 94 | + function getOptions (basename, type) { |
| 95 | + const defaultOptions = type === 'drl' ? argv.drill : argv.gerber |
| 96 | + |
| 97 | + return get(argv.layer, `${basename}.options`, defaultOptions) |
| 98 | + } |
| 99 | + |
| 100 | + function writeRenders (stackup) { |
| 101 | + const name = inferBoardName(stackup) |
| 102 | + const ensureDir = |
| 103 | + argv.out !== options.out.STDOUT ? makeDir(argv.out) : Promise.resolve() |
| 104 | + |
| 105 | + return ensureDir.then(() => |
| 106 | + Promise.all([ |
| 107 | + !argv.noBoard && writeOutput(`${name}.top.svg`, stackup.top.svg), |
| 108 | + !argv.noBoard && writeOutput(`${name}.bottom.svg`, stackup.bottom.svg), |
| 109 | + ...stackup.layers |
| 110 | + .filter(_ => !argv.noLayer) |
| 111 | + .map(layer => |
| 112 | + writeOutput( |
| 113 | + `${layer.filename}.${layer.type}.svg`, |
| 114 | + gerberToSvg.render(layer.converter, layer.options.attributes) |
| 115 | + ) |
| 116 | + ) |
| 117 | + ]) |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + function writeOutput (name, contents) { |
| 122 | + if (argv.out === options.out.STDOUT) return console.log(contents) |
| 123 | + |
| 124 | + const filename = path.join(argv.out, name) |
| 125 | + info(`Writing ${filename}`) |
| 126 | + return writeFile(filename, contents) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +function inferBoardName (stackup) { |
| 131 | + const names = stackup.layers.map(ly => |
| 132 | + path.basename(ly.filename, path.extname(ly.filename)) |
| 133 | + ) |
| 134 | + return common(names) || 'board' |
| 135 | +} |
0 commit comments