forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
66 lines (56 loc) · 2.31 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
// --log_all
// NOTE: this will only run on Node > 6 or needs to be transpiled
// == OpenJSCAD.org CLI interface, written by Rene K. Mueller <[email protected]>, Licensed under MIT License
//
// Description:
// openjscad <file> [-of <format>] [-o <output>]
// e.g.
// openjscad test.jscad
// openjscad test.jscad -o test.stl
// openjscad test.jscad -o test.amf
// openjscad test.jscad -o test.dxf
// openjscad test.scad -o testFromSCAD.jscad
// openjscad test.scad -o test.stl
// openjscad test.stl -o test2.stl # reprocessed: stl -> jscad -> stl
// openjscad test.amf -o test2.jscad
// openjscad test.jscad -of amf
// openjscad test.jscad -of dxf
// openjscad test.jscad -of stl
// openjscad name_plate.jscad --name "Just Me" --title "CEO" -o amf test.amf
//
const fs = require('fs')
const { formats } = require('@jscad/io/formats')
const version = require('./package.json').version
const generateOutputData = require('./src/generateOutputData')
const determineOutputNameAndFormat = require('./src/determineOutputNameAndFormat')
const writeOutput = require('./src/writeOutput')
const parseArgs = require('./src/parseArgs')
// handle arguments (inputs, outputs, etc)
const args = process.argv.splice(2)
let { inputFile, inputFormat, outputFile, outputFormat, params, addMetaData, inputIsDirectory } = parseArgs(args)
// outputs
const output = determineOutputNameAndFormat(outputFormat, outputFile, inputFile)
outputFormat = output.outputFormat
outputFile = output.outputFile
const clicolors = {
red: '\u{1b}[31m',
green: '\u{1b}[32m',
yellow: '\u{1b}[33m',
blue: '\u{1b}[34m',
black: '\u{1b}[0m'
}
console.log(`${clicolors.blue}JSCAD: generating output ${clicolors.red}
from: ${clicolors.green} ${inputFile} ${clicolors.red}
to: ${clicolors.green} ${outputFile} ${clicolors.yellow}(${formats[outputFormat].description}) ${clicolors.black}
`)
// read input data
const src = fs.readFileSync(inputFile, inputFile.match(/\.stl$/i) ? 'binary' : 'UTF8')
// -- convert from JSCAD script into the desired output format
// -- and write it to disk
generateOutputData(src, params, { outputFile, outputFormat, inputFile, inputFormat, version, addMetaData, inputIsDirectory })
.then(outputData => writeOutput(outputFile, outputData))
.catch(error => {
console.error(error)
process.exit(1)
})