From 886c22937d1b6bf64340d4fe3692f64c23c2c3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1n=20D=C3=A9nes?= Date: Tue, 9 Apr 2024 22:38:24 +0200 Subject: [PATCH] Template dependency injection --- src/io.js | 17 +++++++++++++++-- src/templates/index.js | 3 ++- src/templates/kicad8.js | 3 ++- test/cli/bad_bundle/error | 2 +- test/cli/bad_template/command | 1 + test/cli/bad_template/error | 1 + test/cli/bad_template/input/config.yaml | 10 ++++++++++ .../input/templates/bad_template.js | 1 + .../reference/pcbs/custom_template.kicad_pcb | 2 +- test/fixtures/bundle.zip | Bin 1456 -> 1524 bytes .../bundle/templates/custom_template.js | 5 ++++- test/index.js | 2 +- 12 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 test/cli/bad_template/command create mode 100644 test/cli/bad_template/error create mode 100644 test/cli/bad_template/input/config.yaml create mode 100644 test/cli/bad_template/input/templates/bad_template.js 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 a4ca4d68d3aa7a8f0e142b87ce03eb36e033a1fd..e0ef2e80964603220e22dd3010d0c42c0edd08a4 100644 GIT binary patch delta 514 zcmdnM{e_!1z?+#xgnG1-UF zm>DPsmO257ZOEfCRW06c|cUa|?13OHzyVLqm8O*g@t1%>bJ>`5cp^aMRjO zxE^^%1_l`hg~_q3O7*R?MAKlnw1S&~k>v$50|S`w?RV@uWWeM4-Lh5OW2w+7#t8|{ zHxxED{S^pW%`$7pjguK}q3``X7#}{ZKOetsse+)*nud?_WFGJN^sKFB>Z-792jWu- zMfOd-cVl*DV0^1n=Vom@+$IdoV`9Zl$$*bL{GH($n5`?ZL%I?ATv;Ga}%R1 zqkclXc?1Iz;9^ixwY%*bB$ro3Op#+RHSt}0_{Oqt%mLo) z9A+$8AF6>)1vxjsn~_O`86HfNMOpMw!fA2M$Ac0u0W9J`g)Draz(LqHxsTP98x|%&dh$6|1-3)LP+?#I0Q?<~F8}}l 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) }