Template dependency injection

This commit is contained in:
Bán Dénes 2024-04-09 22:38:24 +02:00
parent 1da40bce6f
commit 886c22937d
12 changed files with 39 additions and 8 deletions

View file

@ -6,6 +6,19 @@ const u = require('./utils')
const a = require('./assert') const a = require('./assert')
const kle = require('./kle') 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) => { exports.unpack = async (zip) => {
// main config text (has to be called "config.ext" where ext is one of yaml/json/js) // 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$/)) { for (const fp of fps.file(/.*\.js$/)) {
const name = fp.name.slice('footprints/'.length).split('.')[0] const name = fp.name.slice('footprints/'.length).split('.')[0]
const text = await fp.async('string') 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? // TODO: some sort of footprint validation?
injections.push(['footprint', name, parsed]) injections.push(['footprint', name, parsed])
} }
@ -33,7 +46,7 @@ exports.unpack = async (zip) => {
for (const tpl of tpls.file(/.*\.js$/)) { for (const tpl of tpls.file(/.*\.js$/)) {
const name = tpl.name.slice('templates/'.length).split('.')[0] const name = tpl.name.slice('templates/'.length).split('.')[0]
const text = await tpl.async('string') 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? // TODO: some sort of template validation?
injections.push(['template', name, parsed]) injections.push(['template', name, parsed])
} }

View file

@ -1,3 +1,4 @@
module.exports = { module.exports = {
kicad5: require('./kicad5') kicad5: require('./kicad5'),
kicad8: require('./kicad8')
} }

View file

@ -1,4 +1,5 @@
const m = require('makerjs') const m = require('makerjs')
const version = require('../../package.json').version
module.exports = { module.exports = {
@ -45,7 +46,7 @@ module.exports = {
(kicad_pcb (kicad_pcb
(version 20240108) (version 20240108)
(generator "ergogen") (generator "ergogen")
(generator_version "4.0.5") (generator_version "${version}")
(general (general
(thickness 1.6) (thickness 1.6)
(legacy_teardrops no) (legacy_teardrops no)

View file

@ -1 +1 @@
Could not read config file "test/"! Ambiguous config in bundle!

View file

@ -0,0 +1 @@
node src/cli.js test/cli/bad_template/input --clean

View file

@ -0,0 +1 @@
Unknown dependency "nonexistent_require" among the requirements of injection "bad_template"!

View 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

View file

@ -0,0 +1 @@
const bad = require('nonexistent_require')

View file

@ -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.

Binary file not shown.

View file

@ -1,6 +1,9 @@
const m = require('makerjs')
const version = require('package.json').version
module.exports = { module.exports = {
convert_outline: () => {}, convert_outline: () => {},
body: params => { 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}.`
} }
} }

View file

@ -203,7 +203,7 @@ for (let w of cli_what) {
if (ex === 'should_have_thrown') { if (ex === 'should_have_thrown') {
throw new Error('This command 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) { if (dump) {
fs.writeFileSync(path.join(t, 'error'), actual_error) fs.writeFileSync(path.join(t, 'error'), actual_error)
} }