Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli option to output files as individual part files or .zip of parts #1263

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ Examples:

```jscad mydesign.js -of amf # -- convert mydesign.js into mydesign.amf```

For multi-part models, you can pass the parts flag `-p` to output each part as a separate, numbered file:

```jscad mydesign.js -p # -- convert mydesign.js into mydesign-part-1-of-2.stl and mydesign-part-2-of-2.stl```

If you pass `-p` you may also pass the zip flag `-z` to zip multi-part files into one .zip file:

```jscad mydesign.js -p -z # -- convert mydesign.js into mydesign.zip which contains: mydesign-part-1-of-2.stl and mydesign-part-2-of-2.stl```

The '-o' option can be used to control where the output will be placed.
While, the '-of' option can be used to control the format of the output.

Expand Down
46 changes: 39 additions & 7 deletions packages/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// jscad name_plate.jscad --name "Just Me" --title "CEO" -o amf test.amf
//
const fs = require('fs')
const JSZip = require('jszip')

const { formats } = require('@jscad/io/formats')

Expand All @@ -33,7 +34,7 @@ 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)
let { inputFile, inputFormat, outputFile, outputFormat, parts, zip, params, addMetaData, inputIsDirectory } = parseArgs(args)

// outputs
const output = determineOutputNameAndFormat(outputFormat, outputFile, inputFile)
Expand All @@ -48,18 +49,49 @@ const clicolors = {
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}
`)
const logFileOutput = (outputFile) => {
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))
generateOutputData(src, params, { outputFile, outputFormat, inputFile, inputFormat, parts, version, addMetaData, inputIsDirectory })
.then((outputData) => {
if (outputData instanceof Array) {
if (zip) {
const zip = new JSZip()
for (let i = 0; i < outputData.length; i++) {
const filename = outputFile.replace(/\.(\w+)$/, `-part-${i + 1}-of-${outputData.length}.$1`)
zip.file(filename, outputData[i].asBuffer())
}
zip.generateAsync({ type: 'nodebuffer' }).then((content) => {
const zipFilename = outputFile.replace(/\.(\w+)$/, '.zip')
fs.writeFile(zipFilename, content, (err) => {
if (err) {
console.log('err', err)
} else {
logFileOutput(zipFilename)
}
})
})
} else {
for (let i = 0; i < outputData.length; i++) {
const filename = outputFile.replace(/\.(\w+)$/, `-part-${i + 1}-of-${outputData.length}.$1`)
logFileOutput(filename)
writeOutput(filename, outputData[i])
}
}
} else {
logFileOutput(outputFile)
writeOutput(outputFile, outputData)
}
})
.catch((error) => {
console.error(error)
process.exit(1)
Expand Down
63 changes: 61 additions & 2 deletions packages/cli/cli.parameters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test.afterEach.always((t) => {
try {
if (t.context.outputPath) fs.unlinkSync(t.context.outputPath)
} catch (err) {}
try {
if (t.context.outputPath2) fs.unlinkSync(t.context.outputPath2)
} catch (err) {}
try {
if (t.context.outputPath3) fs.unlinkSync(t.context.outputPath3)
} catch (err) {}

try {
if (t.context.folderPath) fs.rmdirSync(t.context.folderPath, { recursive: false })
Expand All @@ -32,7 +38,7 @@ test.beforeEach((t) => {

// create a simple JSCAD script for input
// the script should produce ALL geometry types
const createJscad = (id) => {
const createJscad = (id, multipart = false) => {
const jscadScript = `// test script ${id}
const { primitives } = require('@jscad/modeling')

Expand All @@ -51,7 +57,7 @@ const main = (params) => {
let ageom2 = primitives.ellipse()
let ageom3 = primitives.ellipsoid()

return [apath2, ageom2, ageom3]
${multipart ? `return [ageom3, ageom3, ageom3]` : `return [apath2, ageom2, ageom3]`}
}

module.exports = { main, getParameterDefinitions }
Expand Down Expand Up @@ -207,3 +213,56 @@ test('cli (single input file, invalid jscad)', (t) => {
execSync(cmd, { stdio: [0, 1, 2] })
})
})


test('cli (single input file, multiple output files)', (t) => {
tsdexter marked this conversation as resolved.
Show resolved Hide resolved
const testID = 7

const inputPath = createJscad(testID, true)
t.true(fs.existsSync(inputPath))

t.context.inputPath = inputPath

const outputName = (partNum) => `./test${testID}-part-${partNum}-of-3.stl`
const outputPath1 = path.resolve(__dirname, outputName(1))
const outputPath2 = path.resolve(__dirname, outputName(2))
const outputPath3 = path.resolve(__dirname, outputName(3))
t.false(fs.existsSync(outputPath1))
t.false(fs.existsSync(outputPath2))
t.false(fs.existsSync(outputPath3))

t.context.outputPath = outputPath1
t.context.outputPath2 = outputPath2
t.context.outputPath3 = outputPath3

const cliPath = t.context.cliPath

const cmd = `node ${cliPath} ${inputPath} -p`
execSync(cmd, { stdio: [0,1,2] })
t.true(fs.existsSync(outputPath1))
t.true(fs.existsSync(outputPath2))
t.true(fs.existsSync(outputPath3))
})

test('cli (single input file, zipped output file)', (t) => {
const testID = 8

const inputPath = createJscad(testID, true)
t.true(fs.existsSync(inputPath))

t.context.inputPath = inputPath


const outputName = `./test${testID}.zip`
const outputPath = path.resolve(__dirname, outputName)

t.false(fs.existsSync(outputPath))

t.context.outputPath = outputPath

const cliPath = t.context.cliPath

const cmd = `node ${cliPath} ${inputPath} -p -z`
execSync(cmd, { stdio: [0,1,2] })
t.true(fs.existsSync(outputPath))
})
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"@jscad/array-utils": "2.1.4",
"@jscad/core": "2.6.6",
"@jscad/io": "2.4.5",
"@jscad/modeling": "2.11.1"
"@jscad/modeling": "2.11.1",
"jszip": "^3.10.1"
},
"devDependencies": {
"ava": "3.15.0",
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/generateOutputData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const generateOutputData = (source, params, options) => {
addMetaData: true
tsdexter marked this conversation as resolved.
Show resolved Hide resolved
}
options = Object.assign({}, defaults, options)
const { outputFormat, inputFile, inputFormat } = options
const { outputFormat, inputFile, inputFormat, parts } = options

options.filename = inputFile // for deserializers

Expand Down Expand Up @@ -62,6 +62,13 @@ const generateOutputData = (source, params, options) => {
})
.then((solids) => {
const serializerOptions = Object.assign({ format: outputFormat }, params)
if (parts) {
let blobs = []
for (let i = 0; i < solids.length; i++) {
blobs.push(solidsAsBlob(solids[i], serializerOptions))
}
return blobs
}
return solidsAsBlob(solids, serializerOptions)
})
}
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/parseArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const parseArgs = (args) => {
let inputFormat
let outputFile
let outputFormat
let parts = false
let zip = false
const params = {} // parameters to feed the script if applicable
let addMetaData = false // wether to add metadata to outputs or not : ie version info, timestamp etc
let inputIsDirectory = false // did we pass in a folder or a file ?
Expand All @@ -41,6 +43,10 @@ const parseArgs = (args) => {
for (let i = 0; i < args.length; i++) {
if (args[i] === '-of') { // -of <format>
outputFormat = args[++i]
} else if (args[i] === '-p') {
parts = true
} else if (args[i] === '-z') {
zip = true
} else if (args[i].match(/^-o(\S.+)/)) { // -o<output>
outputFile = args[i]
outputFile = outputFile.replace(/^-o(\S+)$/, '$1')
Expand Down Expand Up @@ -92,6 +98,8 @@ const parseArgs = (args) => {
inputFormat,
outputFile,
outputFormat,
parts,
zip,
params,
addMetaData,
inputIsDirectory
Expand Down