@jscad/cli 2.2.26 → 3.0.0-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,59 +1,59 @@
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',
20
25
  inputFile: '',
21
26
  version: '',
22
- addMetaData: true
27
+ addMetaData: true,
28
+ generateParts: false
23
29
  }
24
30
  options = Object.assign({}, defaults, options)
25
- const { outputFormat, inputFile, inputFormat } = options
26
31
 
27
- options.filename = inputFile // for deserializers
32
+ const { inputFile, inputFormat, outputFormat, generateParts } = options
33
+
34
+ const inputMimeType = getMimeType(inputFormat)
35
+ const outputMimeType = getMimeType(outputFormat)
28
36
 
29
- const inputPath = isAbsolute(inputFile) ? inputFile : resolve(process.cwd(), inputFile)
37
+ const inputPath = path.isAbsolute(inputFile) ? inputFile : path.resolve(process.cwd(), inputFile)
30
38
 
31
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)
32
42
  registerAllExtensions(fs, require)
33
43
 
34
44
  return new Promise((resolve, reject) => {
35
- let deserializer = deserializers[inputFormat]
36
- if (!deserializer) {
37
- if (inputFormat === 'jscad' || inputFormat === 'js') {
38
- deserializer = (options, source) => source
39
- } else {
40
- reject(new Error(`unsuported input format ${inputFormat}`))
41
- }
42
- }
43
-
44
45
  // convert any inputs
45
46
  const prevsource = source
46
- const deserializerOptions = Object.assign({}, params, options)
47
- source = deserializer(deserializerOptions, source)
47
+ const deserializerOptions = Object.assign({ output: 'script', filename: inputFile }, cliParams)
48
+ source = deserialize(deserializerOptions, inputMimeType, source)
48
49
  const useFakeFs = (source !== prevsource) // conversion, so use a fake file system when rebuilding
49
50
 
50
- if (outputFormat === 'jscad' || outputFormat === 'js') {
51
+ if (outputMimeType === 'application/javascript') {
52
+ // pass back the source
51
53
  resolve(source)
52
54
  } else {
53
- // } else if ((inputFormat === 'jscad' || inputFormat === 'js') &&
54
- // outputFormat !== 'jscad' && outputFormat !== 'js') {
55
55
  try {
56
- const solids = rebuildGeometryCli({ mainPath: inputPath, parameterValues: params, useFakeFs, source })
56
+ const solids = rebuildGeometryCli({ mainPath: inputPath, parameterValues: cliParams, useFakeFs, source })
57
57
  resolve(solids)
58
58
  } catch (error) {
59
59
  reject(error)
@@ -61,9 +61,25 @@ const generateOutputData = (source, params, options) => {
61
61
  }
62
62
  })
63
63
  .then((solids) => {
64
- const serializerOptions = Object.assign({ format: outputFormat }, params)
65
- return solidsAsBlob(solids, serializerOptions)
64
+ if (Array.isArray(solids) && generateParts) {
65
+ return solids.map((s) => convertSolidsToBlob({ mimeType: outputMimeType, cliParams }, [s]))
66
+ }
67
+ return convertSolidsToBlob({ mimeType: outputMimeType, cliParams }, solids)
66
68
  })
67
69
  }
68
70
 
69
- 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(', ')}`)
@@ -24,6 +29,8 @@ const parseArgs = (args) => {
24
29
  let inputFormat
25
30
  let outputFile
26
31
  let outputFormat
32
+ let generateParts = false
33
+ let zip = false
27
34
  const params = {} // parameters to feed the script if applicable
28
35
  let addMetaData = false // wether to add metadata to outputs or not : ie version info, timestamp etc
29
36
  let inputIsDirectory = false // did we pass in a folder or a file ?
@@ -41,6 +48,10 @@ const parseArgs = (args) => {
41
48
  for (let i = 0; i < args.length; i++) {
42
49
  if (args[i] === '-of') { // -of <format>
43
50
  outputFormat = args[++i]
51
+ } else if (args[i] === '-gp') {
52
+ generateParts = true
53
+ } else if (args[i] === '-z') {
54
+ zip = true
44
55
  } else if (args[i].match(/^-o(\S.+)/)) { // -o<output>
45
56
  outputFile = args[i]
46
57
  outputFile = outputFile.replace(/^-o(\S+)$/, '$1')
@@ -72,10 +83,10 @@ const parseArgs = (args) => {
72
83
  console.log('Verify main or index exists')
73
84
  process.exit(1)
74
85
  }
75
- inputFormat = require('path').extname(inputFile).substring(1)
86
+ inputFormat = path.extname(inputFile).substring(1)
76
87
  } else {
77
88
  console.log('ERROR: invalid file name or argument <' + args[i] + '>')
78
- console.log("Type 'openjscad' for a list of supported types")
89
+ console.log("Type 'jscad' for a list of supported types")
79
90
  process.exit(1)
80
91
  }
81
92
  }
@@ -86,16 +97,25 @@ const parseArgs = (args) => {
86
97
  if (!outputFormat && !outputFile) {
87
98
  outputFormat = 'stla'
88
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
+ }
89
109
 
90
110
  return {
91
111
  inputFile,
92
112
  inputFormat,
93
113
  outputFile,
94
114
  outputFormat,
115
+ generateParts,
116
+ zip,
95
117
  params,
96
118
  addMetaData,
97
119
  inputIsDirectory
98
120
  }
99
121
  }
100
-
101
- 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