@jscad/cli 2.3.5 → 3.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,24 @@
1
- const fs = require('fs')
2
- const { isAbsolute, resolve } = require('path')
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import { createRequire } from 'module'
3
4
 
4
- const { deserializers, solidsAsBlob } = require('@jscad/io')
5
+ import { deserialize, getMimeType, serialize } from '@jscad/io'
6
+ import { convertToBlob } from '@jscad/io-utils'
5
7
 
6
- const { rebuildGeometryCli } = require('@jscad/core').evaluation
7
- const { registerAllExtensions } = require('@jscad/core').io
8
+ import { evaluation, io } from '@jscad/core'
9
+
10
+ const { rebuildGeometryCli } = evaluation
11
+ const { registerAllExtensions } = io
8
12
 
9
13
  /**
10
- * generate output data from source
14
+ * Create a promise to convert the given source in inputFormat to the desired outputFormat.
15
+ * The given CLI params are passed into deserializer, main, and serializer.
11
16
  * @param {String} source the original source
12
- * @param {Object} params hash of parameters to pass to main function
13
- * @param {String} options
14
- * @return a Promise with the output data
17
+ * @param {Object} cliParams - parameters as provided on the command line
18
+ * @param {Object} options - options for conversion; inputFormat and outputFormat are required
19
+ * @return {Promise} promise function which can convert the given source
15
20
  */
16
- const generateOutputData = (source, params, options) => {
21
+ export const generateOutputData = (source, cliParams, options) => {
17
22
  const defaults = {
18
23
  outputFile: undefined,
19
24
  outputFormat: 'stl',
@@ -23,38 +28,32 @@ const generateOutputData = (source, params, options) => {
23
28
  generateParts: false
24
29
  }
25
30
  options = Object.assign({}, defaults, options)
26
- const { outputFormat, inputFile, inputFormat, generateParts } = options
27
31
 
28
- options.filename = inputFile // for deserializers
32
+ const { inputFile, inputFormat, outputFormat, generateParts } = options
33
+
34
+ const inputMimeType = getMimeType(inputFormat)
35
+ const outputMimeType = getMimeType(outputFormat)
29
36
 
30
- const inputPath = isAbsolute(inputFile) ? inputFile : resolve(process.cwd(), inputFile)
37
+ const inputPath = path.isAbsolute(inputFile) ? inputFile : path.resolve(process.cwd(), inputFile)
31
38
 
32
39
  // setup support for require-ing files with .jscad, .stl etc extensions
40
+ // HACK create the require function if necessary
41
+ const require = createRequire(import.meta.url)
33
42
  registerAllExtensions(fs, require)
34
43
 
35
44
  return new Promise((resolve, reject) => {
36
- let deserializer = deserializers[inputFormat]
37
- if (!deserializer) {
38
- if (inputFormat === 'jscad' || inputFormat === 'js') {
39
- deserializer = (options, source) => source
40
- } else {
41
- reject(new Error(`unsuported input format ${inputFormat}`))
42
- }
43
- }
44
-
45
45
  // convert any inputs
46
46
  const prevsource = source
47
- const deserializerOptions = Object.assign({}, params, options)
48
- source = deserializer(deserializerOptions, source)
47
+ const deserializerOptions = Object.assign({ output: 'script', filename: inputFile }, cliParams)
48
+ source = deserialize(deserializerOptions, inputMimeType, source)
49
49
  const useFakeFs = (source !== prevsource) // conversion, so use a fake file system when rebuilding
50
50
 
51
- if (outputFormat === 'jscad' || outputFormat === 'js') {
51
+ if (outputMimeType === 'application/javascript') {
52
+ // pass back the source
52
53
  resolve(source)
53
54
  } else {
54
- // } else if ((inputFormat === 'jscad' || inputFormat === 'js') &&
55
- // outputFormat !== 'jscad' && outputFormat !== 'js') {
56
55
  try {
57
- const solids = rebuildGeometryCli({ mainPath: inputPath, parameterValues: params, useFakeFs, source })
56
+ const solids = rebuildGeometryCli({ mainPath: inputPath, parameterValues: cliParams, useFakeFs, source })
58
57
  resolve(solids)
59
58
  } catch (error) {
60
59
  reject(error)
@@ -62,16 +61,25 @@ const generateOutputData = (source, params, options) => {
62
61
  }
63
62
  })
64
63
  .then((solids) => {
65
- const serializerOptions = Object.assign({ format: outputFormat }, params)
66
- if (generateParts) {
67
- const blobs = []
68
- for (let i = 0; i < solids.length; i++) {
69
- blobs.push(solidsAsBlob(solids[i], serializerOptions))
70
- }
71
- return blobs
64
+ if (Array.isArray(solids) && generateParts) {
65
+ return solids.map((s) => convertSolidsToBlob({ mimeType: outputMimeType, cliParams }, [s]))
72
66
  }
73
- return solidsAsBlob(solids, serializerOptions)
67
+ return convertSolidsToBlob({ mimeType: outputMimeType, cliParams }, solids)
74
68
  })
75
69
  }
76
70
 
77
- module.exports = generateOutputData
71
+ /*
72
+ * Convert the given solids to the target mimeType, and return as a blob for writing to file.
73
+ */
74
+ const convertSolidsToBlob = (options, solids) => {
75
+ const { mimeType, cliParams } = options
76
+
77
+ if (mimeType === 'application/javascript') {
78
+ // convert the solids (source code) to blob without conversion
79
+ return convertToBlob({ data: [solids], mimeType })
80
+ } else {
81
+ // convert the solids into the mimeType via serializers
82
+ const serializerOptions = Object.assign({}, cliParams)
83
+ return convertToBlob(serialize(serializerOptions, mimeType, solids))
84
+ }
85
+ }
package/src/parseArgs.js CHANGED
@@ -1,11 +1,15 @@
1
- const fs = require('fs')
1
+ import fs from 'fs'
2
+ import path from 'path'
2
3
 
3
- const { getDesignEntryPoint } = require('@jscad/core').loading.requireDesignUtilsFs
4
- const { supportedInputExtensions, supportedOutputExtensions, supportedOutputFormats } = require('@jscad/io/formats')
4
+ import { loading } from '@jscad/core'
5
5
 
6
- const env = require('./env')
6
+ import { supportedInputExtensions, supportedOutputExtensions, supportedOutputFormats } from '@jscad/io'
7
7
 
8
- const parseArgs = (args) => {
8
+ import { env } from './env.js'
9
+
10
+ const { getDesignEntryPoint } = loading
11
+
12
+ export const parseArgs = (args) => {
9
13
  const inputExtensions = supportedInputExtensions()
10
14
  const outputExtensions = supportedOutputExtensions()
11
15
  const outputFormats = supportedOutputFormats()
@@ -13,7 +17,8 @@ const parseArgs = (args) => {
13
17
  // hint: https://github.com/substack/node-optimist
14
18
  // https://github.com/visionmedia/commander.js
15
19
  if (args.length < 1) {
16
- console.log('USAGE:\n\njscad [-v] <file> [-of <format>] [-o <output>]')
20
+ console.log('USAGE:\n\njscad [-v]\n\n')
21
+ console.log('jscad [-gp] [-z] <file> [-of <format>] [-o <output>]')
17
22
  console.log(`\t<file> :\tinput (Supported types: folder, .${inputExtensions.join(', .')})`)
18
23
  console.log(`\t<output>:\toutput (Supported types: folder, .${outputExtensions.join(', .')})`)
19
24
  console.log(`\t<format>:\t${outputFormats.join(', ')}`)
@@ -78,10 +83,10 @@ const parseArgs = (args) => {
78
83
  console.log('Verify main or index exists')
79
84
  process.exit(1)
80
85
  }
81
- inputFormat = require('path').extname(inputFile).substring(1)
86
+ inputFormat = path.extname(inputFile).substring(1)
82
87
  } else {
83
88
  console.log('ERROR: invalid file name or argument <' + args[i] + '>')
84
- console.log("Type 'openjscad' for a list of supported types")
89
+ console.log("Type 'jscad' for a list of supported types")
85
90
  process.exit(1)
86
91
  }
87
92
  }
@@ -92,6 +97,15 @@ const parseArgs = (args) => {
92
97
  if (!outputFormat && !outputFile) {
93
98
  outputFormat = 'stla'
94
99
  }
100
+ if (!outputFormat && outputFile) {
101
+ outputFormat = path.extname(outputFile).substring(1)
102
+ if (outputFormat === 'stl') outputFormat = 'stla'
103
+ }
104
+ if (!outputFormats.includes(outputFormat)) {
105
+ console.log('ERROR: invalid output format <' + outputFormat + '>')
106
+ console.log("Type 'jscad' for a list of supported types")
107
+ process.exit(1)
108
+ }
95
109
 
96
110
  return {
97
111
  inputFile,
@@ -105,5 +119,3 @@ const parseArgs = (args) => {
105
119
  inputIsDirectory
106
120
  }
107
121
  }
108
-
109
- module.exports = parseArgs
@@ -1,15 +1,11 @@
1
- const fs = require('fs')
1
+ import fs from 'fs'
2
2
 
3
- const writeOutputDataToFile = (outputFile, outputData) => {
3
+ export const writeOutput = (outputFile, outputData) => {
4
4
  fs.writeFile(outputFile, outputData.asBuffer(),
5
5
  (err) => {
6
6
  if (err) {
7
7
  console.log('err', err)
8
- } else {
9
- // console.log('success')
10
8
  }
11
9
  }
12
10
  )
13
11
  }
14
-
15
- module.exports = writeOutputDataToFile
@@ -1,4 +0,0 @@
1
-
2
- const main = () => require('./binary_stl.stl')
3
-
4
- module.exports = { main }