forked from ergogen/ergogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathergogen.js
115 lines (102 loc) · 3.51 KB
/
ergogen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const u = require('./utils')
const io = require('./io')
const prepare = require('./prepare')
const units_lib = require('./units')
const points_lib = require('./points')
const outlines_lib = require('./outlines')
const cases_lib = require('./cases')
const pcbs_lib = require('./pcbs')
const version = require('../package.json').version
const process = async (raw, debug=false, logger=()=>{}) => {
const prefix = 'Interpreting format: '
let empty = true
let [config, format] = io.interpret(raw, logger)
let suffix = format
// KLE conversion warrants automaticly engaging debug mode
// as, usually, we're only interested in the points anyway
if (format == 'KLE') {
suffix = `${format} (Auto-debug)`
debug = true
}
logger(prefix + suffix)
logger('Preprocessing input...')
config = prepare.unnest(config)
config = prepare.inherit(config)
config = prepare.parameterize(config)
const results = {}
if (debug) {
results.raw = raw
results.canonical = u.deepcopy(config)
}
if (config.meta && config.meta.engine) {
logger('Checking compatibility...')
const engine = u.semver(config.meta.engine, 'config.meta.engine')
if (!u.satisfies(version, engine)) {
throw new Error(`Current ergogen version (${version}) doesn\'t satisfy config's engine requirement (${config.meta.engine})!`)
}
}
logger('Calculating variables...')
const units = units_lib.parse(config)
if (debug) {
results.units = units
}
logger('Parsing points...')
if (!config.points) {
throw new Error('Input does not contain a points clause!')
}
const points = points_lib.parse(config.points, units)
if (!Object.keys(points).length) {
throw new Error('Input does not contain any points!')
}
if (debug) {
results.points = points
results.demo = io.twodee(points_lib.visualize(points, units), debug)
}
logger('Generating outlines...')
const outlines = outlines_lib.parse(config.outlines || {}, points, units)
results.outlines = {}
for (const [name, outline] of Object.entries(outlines)) {
if (!debug && name.startsWith('_')) continue
results.outlines[name] = io.twodee(outline, debug)
empty = false
}
logger('Modeling cases...')
const cases = cases_lib.parse(config.cases || {}, outlines, units)
results.cases = {}
for (const [case_name, case_script] of Object.entries(cases)) {
if (!debug && case_name.startsWith('_')) continue
results.cases[case_name] = {jscad: case_script}
empty = false
}
logger('Scaffolding PCBs...')
const pcbs = pcbs_lib.parse(config, points, outlines, units)
results.pcbs = {}
for (const [pcb_name, pcb_text] of Object.entries(pcbs)) {
if (!debug && pcb_name.startsWith('_')) continue
results.pcbs[pcb_name] = pcb_text
empty = false
}
if (!debug && empty) {
logger('Output would be empty, rerunning in debug mode...')
return process(raw, true, () => {})
}
return results
}
const inject = (type, name, value) => {
if (value === undefined) {
value = name
name = type
type = 'footprint'
}
switch (type) {
case 'footprint':
return pcbs_lib.inject_footprint(name, value)
default:
throw new Error(`Unknown injection type "${type}" with name "${name}" and value "${value}"!`)
}
}
module.exports = {
version,
process,
inject
}