Skip to content

Commit 828394d

Browse files
authored
fix(cli): fix rebuild geometry for cli (#583)
* refactor(core): changed rebuildGeometryCli to use webRequire when source is provided, i.e. from conversions * refactor(cli): small change to generateOutputData to clear source when there is NO conversion enabled test of stl to dxf conversion * feat(core): added useFakeFs option to rebuildGeometryCli changed code to derive fake file name and path from given mainPath code cleanup * refactor(cli): changed generateOutputData to determine and pass useFakeFs to rebuildGeometryCli
1 parent 2b5183e commit 828394d

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

packages/cli/cli.conversions.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ test.afterEach.always(t => {
1717
try {
1818
if (t.context.file3Path) fs.unlinkSync(t.context.file3Path)
1919
} catch (err) {}
20+
21+
try {
22+
if (t.context.file4Path) fs.unlinkSync(t.context.file4Path)
23+
} catch (err) {}
2024
})
2125

2226
test.beforeEach(t => {
@@ -102,8 +106,7 @@ test('cli (conversions)', t => {
102106

103107
t.context.file4Path = file4Path
104108

105-
// FIXME FAILS WITH STACK TRACE
106-
// cmd = `node ${cliPath} ${file2Path} -of dxf`
107-
// execSync(cmd, { stdio: [0, 1, 2] })
108-
// t.true(fs.existsSync(file4Path))
109+
cmd = `node ${cliPath} ${file2Path} -of dxf`
110+
execSync(cmd, { stdio: [0, 1, 2] })
111+
t.true(fs.existsSync(file4Path))
109112
})

packages/cli/src/generateOutputData.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const generateOutputData = (source, params, options) => {
2222
addMetaData: true
2323
}
2424
options = Object.assign({}, defaults, options)
25-
const { outputFormat, inputFile, inputFormat, inputIsDirectory } = options
25+
const { outputFormat, inputFile, inputFormat } = options
2626

2727
options.filename = inputFile // for deserializers
2828

@@ -55,15 +55,17 @@ const generateOutputData = (source, params, options) => {
5555
}
5656

5757
// convert any inputs
58+
const prevsource = source
5859
source = conversionTable[inputFormat]({ source, params, options })
60+
const useFakeFs = (source !== prevsource) // conversion, so use a fake file system when rebuilding
5961

6062
if (outputFormat === 'jscad' || outputFormat === 'js') {
6163
resolve(source)
6264
} else {
6365
// } else if ((inputFormat === 'jscad' || inputFormat === 'js') &&
6466
// outputFormat !== 'jscad' && outputFormat !== 'js') {
6567
try {
66-
const solids = rebuildSolids({ mainPath: inputPath, parameterValues: params, inputIsDirectory, source })
68+
const solids = rebuildSolids({ mainPath: inputPath, parameterValues: params, useFakeFs, source })
6769
resolve(solids)
6870
} catch (error) {
6971
reject(error)

packages/core/code-evaluation/rebuildGeometryCli.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,34 @@ const path = require('path')
33
const { toArray } = require('@jscad/array-utils')
44
const requireDesignFromModule = require('../code-loading/requireDesignFromModule')
55
const getAllParameterDefintionsAndValues = require('../parameters/getParameterDefinitionsAndValues')
6-
const transformSources = require('../code-loading/transformSources')
76
const makeWebRequire = require('../code-loading/webRequire')
8-
const makeFakeFs = require('../code-loading/makeFakeFs')
9-
const { registerAllExtensions } = require('../io/registerExtensions')
107

118
const rebuildSolids = (data) => {
129
const defaults = { vtreeMode: false, serialize: false }
13-
const { mainPath, vtreeMode, parameterValues, inputIsDirectory } = Object.assign({}, defaults, data)
10+
let { mainPath, vtreeMode, parameterValues, useFakeFs } = Object.assign({}, defaults, data)
1411
const apiMainPath = vtreeMode ? '../code-loading/vtreeApi' : '@jscad/modeling'
1512
// we need to update the source for our module
1613
let requireFn = require
17-
if (!inputIsDirectory) {
18-
let filesAndFolders = [
14+
15+
// source came from conversion, i.e. not from file system
16+
if (useFakeFs) {
17+
const pathParts = path.parse(mainPath)
18+
const fakeName = `${pathParts.name}.js`
19+
const fakePath = `/${pathParts.name}.js`
20+
const filesAndFolders = [
1921
{
20-
ext: path.extname(mainPath).substring(1),
21-
name: path.basename(mainPath),
22-
fullPath: mainPath,
22+
ext: 'js',
23+
fullPath: fakePath,
24+
name: fakeName,
2325
source: data.source
2426
}
2527
]
26-
filesAndFolders = transformSources({ apiMainPath }, filesAndFolders)
27-
// console.log('filesAndFolders', filesAndFolders)
28+
requireFn = makeWebRequire(filesAndFolders, { apiMainPath })
2829

29-
const fakeFs = makeFakeFs(filesAndFolders)
30-
requireFn = makeWebRequire(filesAndFolders, { apiMainPath, fakeFs })// hasRequire() ? require : makeWebRequire(filesAndFolders, { apiMainPath })
31-
// register all extension formats
32-
registerAllExtensions(fakeFs, requireFn)
30+
mainPath = fakePath // and use the alias as the entry point
3331
}
3432

35-
// console.log('transformed sources', filesAndFolders)
36-
// now check if we need fake require or not
37-
// FIXME: we need to come up with a way to intercept node 'require' calls to be able to apply transformSources on the fly
38-
// since we keep passing the 'mainPath' to the normal require which points to the NON TRANSFORMED source
39-
// const requireFn = makeWebRequire(filesAndFolders, { apiMainPath })// hasRequire() ? require : makeWebRequire(filesAndFolders, { apiMainPath })
40-
// rootModule SHOULD contain a main() entry and optionally a getParameterDefinitions entry
33+
// rootModule should contain exported main and getParameterDefinitions functions
4134
const rootModule = requireDesignFromModule(mainPath, requireFn)
4235
// the design (module tree) has been loaded at this stage
4336
// now we can get our usefull data (definitions and values/defaults)

0 commit comments

Comments
 (0)