forked from ergogen/ergogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
102 lines (89 loc) · 3 KB
/
io.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
const yaml = require('js-yaml')
const jszip = require('jszip')
const makerjs = require('makerjs')
const u = require('./utils')
const a = require('./assert')
const kle = require('./kle')
exports.unpack = async (zip) => {
// main config text (has to be called "config.ext" where ext is one of yaml/json/js)
const candidates = zip.file(/^config\.(yaml|json|js)$/)
if (candidates.length != 1) {
throw new Error('Ambiguous config in bundle!')
}
const config_text = await candidates[0].async('string')
const injections = []
// bundled footprints
const fps = zip.folder('footprints')
const module_prefix = 'const module = {};\n\n'
const module_suffix = '\n\nreturn module.exports;'
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)()
// TODO: some sort of footprint validation?
injections.push(['footprint', name, parsed])
}
// bundled pcb templates
const tpls = zip.folder('templates')
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)()
// TODO: some sort of template validation?
injections.push(['template', name, parsed])
}
return [config_text, injections]
}
exports.interpret = (raw, logger) => {
let config = raw
let format = 'OBJ'
if (a.type(raw)() == 'string') {
try {
config = yaml.safeLoad(raw)
format = 'YAML'
} catch (yamlex) {
try {
config = new Function(raw)()
a.assert(
a.type(config)() == 'object',
'Input JS Code doesn\'t resolve into an object!'
)
format = 'JS'
} catch (codeex) {
logger('YAML exception:', yamlex)
logger('Code exception:', codeex)
throw new Error('Input is not valid YAML, JSON, or JS Code!')
}
}
}
try {
// assume it's KLE and try to convert it
config = kle.convert(config, logger)
format = 'KLE'
} catch (kleex) {
// nope... nevermind
}
if (a.type(config)() != 'object') {
throw new Error('Input doesn\'t resolve into an object!')
}
if (!Object.keys(config).length) {
throw new Error('Input appears to be empty!')
}
return [config, format]
}
exports.twodee = (model, debug) => {
const assembly = makerjs.model.originate({
models: {
export: u.deepcopy(model)
},
units: 'mm'
})
const result = {
dxf: makerjs.exporter.toDXF(assembly),
}
if (debug) {
result.yaml = assembly
result.svg = makerjs.exporter.toSVG(assembly)
}
return result
}