-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
57 lines (49 loc) · 1.73 KB
/
cli.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
const filterKey = (k, i, row) => {
const dv = { w: 1, h:1, x: 0, y: 0 }
if (typeof k === "string") {
if (typeof row[i-1] === 'object') {
const obj = row[i-1]
return [obj.w || 1, obj.h || 1, obj.x || 0, obj.y || 0]
} else {
return [1, 1, 0, 0]
}
}
return null
}
const filterRow = (r) => r.map(filterKey).filter(x => x)
const filterDesc = (kle) => kle.map(r => r instanceof Array && r).filter(x => x)
const fs = require('fs');
const path = require('path');
// Non-blocking example with fs.readFile
// const args = process.argv.splice(2);]
const {
_: [cliType, filePath],
angle = 6,
padding = 4,
top = 8,
reinforce = 3.5,
middle = 6,
bottom = 0,
reinforceScrew = 1
} = require('minimist')(process.argv.slice(2));
if (filePath) {
console.log('Using ' + filePath)
console.log('Generating ' + cliType)
const fileName = path.basename(filePath, '.json');
const kle = fs.readFileSync(filePath, 'utf-8');
const layout = JSON.stringify(filterDesc(JSON.parse(kle)).map(filterRow));
if (padding < 4) {
console.log('Warning: The minimum padding is 4, reset to 4!')
}
let result = ''
result += 'use <../utils.scad>;' + '\n'
result += `layout = ${layout};` + '\n'
if (cliType === 'case') result += `case(layout,${angle},${Math.max(padding, 4)},${top},${reinforce},${middle},${bottom}, ${reinforceScrew});` + '\n'
if (cliType === 'plate') result += 'plate(layout);' + '\n'
fs.writeFile(`scad/result/${fileName}_${cliType}.scad`, result, function (err) {
if (err) return console.log(`Error: Can not reslove ${filePath}.`);
console.log(`Done: Please check ./scad/result/${fileName}_${cliType}.scad`)
});
} else {
console.log('Missing required arguments: keyboard_layout_editor.json')
}