mirror of
https://github.com/TECHNOFAB11/ergogen.git
synced 2025-12-12 16:10:04 +01:00
Template dependency injection
This commit is contained in:
parent
1da40bce6f
commit
886c22937d
12 changed files with 39 additions and 8 deletions
17
src/io.js
17
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])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
module.exports = {
|
||||
kicad5: require('./kicad5')
|
||||
kicad5: require('./kicad5'),
|
||||
kicad8: require('./kicad8')
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Could not read config file "test/"!
|
||||
Ambiguous config in bundle!
|
||||
1
test/cli/bad_template/command
Normal file
1
test/cli/bad_template/command
Normal file
|
|
@ -0,0 +1 @@
|
|||
node src/cli.js test/cli/bad_template/input --clean
|
||||
1
test/cli/bad_template/error
Normal file
1
test/cli/bad_template/error
Normal file
|
|
@ -0,0 +1 @@
|
|||
Unknown dependency "nonexistent_require" among the requirements of injection "bad_template"!
|
||||
10
test/cli/bad_template/input/config.yaml
Normal file
10
test/cli/bad_template/input/config.yaml
Normal file
|
|
@ -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
|
||||
1
test/cli/bad_template/input/templates/bad_template.js
Normal file
1
test/cli/bad_template/input/templates/bad_template.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
const bad = require('nonexistent_require')
|
||||
|
|
@ -1 +1 @@
|
|||
Custom template override. The secret is 42.
|
||||
Custom template override. The secret is 42. MakerJS is 0.17.0. Ergogen is 4.0.5.
|
||||
BIN
test/fixtures/bundle.zip
vendored
BIN
test/fixtures/bundle.zip
vendored
Binary file not shown.
|
|
@ -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}.`
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue