diff --git a/src/io.js b/src/io.js index be37124..43facca 100644 --- a/src/io.js +++ b/src/io.js @@ -6,6 +6,19 @@ const u = require('./utils') const a = require('./assert') const kle = require('./kle') +const package_json = require('../package.json') + +const fake_require = exports.fake_require = injection => name => { + const dependencies = { + makerjs + } + if (name.endsWith('package.json')) { + return package_json + } else if (dependencies[name]) { + return dependencies[name] + } else throw new Error(`Unknown dependency "${name}" among the requirements of injection "${injection}"!`) +} + exports.unpack = async (zip) => { // main config text (has to be called "config.ext" where ext is one of yaml/json/js) @@ -23,7 +36,7 @@ exports.unpack = async (zip) => { for (const fp of fps.file(/.*\.js$/)) { const name = fp.name.slice('footprints/'.length).split('.')[0] const text = await fp.async('string') - const parsed = new Function(module_prefix + text + module_suffix)() + const parsed = new Function('require', module_prefix + text + module_suffix)(fake_require(name)) // TODO: some sort of footprint validation? injections.push(['footprint', name, parsed]) } @@ -33,7 +46,7 @@ exports.unpack = async (zip) => { for (const tpl of tpls.file(/.*\.js$/)) { const name = tpl.name.slice('templates/'.length).split('.')[0] const text = await tpl.async('string') - const parsed = new Function(module_prefix + text + module_suffix)() + const parsed = new Function('require', module_prefix + text + module_suffix)(fake_require(name)) // TODO: some sort of template validation? injections.push(['template', name, parsed]) } diff --git a/src/templates/index.js b/src/templates/index.js index b13d642..35277a6 100644 --- a/src/templates/index.js +++ b/src/templates/index.js @@ -1,3 +1,4 @@ module.exports = { - kicad5: require('./kicad5') + kicad5: require('./kicad5'), + kicad8: require('./kicad8') } \ No newline at end of file diff --git a/src/templates/kicad8.js b/src/templates/kicad8.js index 1cbd044..dfc2a43 100644 --- a/src/templates/kicad8.js +++ b/src/templates/kicad8.js @@ -1,4 +1,5 @@ const m = require('makerjs') +const version = require('../../package.json').version module.exports = { @@ -45,7 +46,7 @@ module.exports = { (kicad_pcb (version 20240108) (generator "ergogen") - (generator_version "4.0.5") + (generator_version "${version}") (general (thickness 1.6) (legacy_teardrops no) diff --git a/test/cli/bad_bundle/error b/test/cli/bad_bundle/error index 0b991c8..474ed42 100644 --- a/test/cli/bad_bundle/error +++ b/test/cli/bad_bundle/error @@ -1 +1 @@ -Could not read config file "test/"! \ No newline at end of file +Ambiguous config in bundle! \ No newline at end of file diff --git a/test/cli/bad_template/command b/test/cli/bad_template/command new file mode 100644 index 0000000..987e200 --- /dev/null +++ b/test/cli/bad_template/command @@ -0,0 +1 @@ +node src/cli.js test/cli/bad_template/input --clean \ No newline at end of file diff --git a/test/cli/bad_template/error b/test/cli/bad_template/error new file mode 100644 index 0000000..ae2dada --- /dev/null +++ b/test/cli/bad_template/error @@ -0,0 +1 @@ +Unknown dependency "nonexistent_require" among the requirements of injection "bad_template"! \ No newline at end of file diff --git a/test/cli/bad_template/input/config.yaml b/test/cli/bad_template/input/config.yaml new file mode 100644 index 0000000..38039e8 --- /dev/null +++ b/test/cli/bad_template/input/config.yaml @@ -0,0 +1,10 @@ +points.zones.matrix: +outlines: + box: + - what: rectangle + where: true + size: 18 +pcbs: + bad_template: + outlines.edge.outline: box + template: bad_template \ No newline at end of file diff --git a/test/cli/bad_template/input/templates/bad_template.js b/test/cli/bad_template/input/templates/bad_template.js new file mode 100644 index 0000000..7a840b1 --- /dev/null +++ b/test/cli/bad_template/input/templates/bad_template.js @@ -0,0 +1 @@ +const bad = require('nonexistent_require') \ No newline at end of file diff --git a/test/cli/bundle/reference/pcbs/custom_template.kicad_pcb b/test/cli/bundle/reference/pcbs/custom_template.kicad_pcb index 9a1c42b..ba3a662 100644 --- a/test/cli/bundle/reference/pcbs/custom_template.kicad_pcb +++ b/test/cli/bundle/reference/pcbs/custom_template.kicad_pcb @@ -1 +1 @@ -Custom template override. The secret is 42. \ No newline at end of file +Custom template override. The secret is 42. MakerJS is 0.17.0. Ergogen is 4.0.5. \ No newline at end of file diff --git a/test/fixtures/bundle.zip b/test/fixtures/bundle.zip index a4ca4d6..e0ef2e8 100644 Binary files a/test/fixtures/bundle.zip and b/test/fixtures/bundle.zip differ diff --git a/test/fixtures/bundle/templates/custom_template.js b/test/fixtures/bundle/templates/custom_template.js index d3217b8..d70f50a 100644 --- a/test/fixtures/bundle/templates/custom_template.js +++ b/test/fixtures/bundle/templates/custom_template.js @@ -1,6 +1,9 @@ +const m = require('makerjs') +const version = require('package.json').version + module.exports = { convert_outline: () => {}, body: params => { - return `Custom template override. The secret is ${params.custom.secret}.` + return `Custom template override. The secret is ${params.custom.secret}. MakerJS is ${m.version}. Ergogen is ${version}.` } } \ No newline at end of file diff --git a/test/index.js b/test/index.js index 596757e..6d1305f 100644 --- a/test/index.js +++ b/test/index.js @@ -203,7 +203,7 @@ for (let w of cli_what) { if (ex === 'should_have_thrown') { throw new Error('This command should have thrown!') } - const actual_error = ex.stderr.toString().split('\n')[0] + const actual_error = ex.stderr.toString() if (dump) { fs.writeFileSync(path.join(t, 'error'), actual_error) }