This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.js
74 lines (65 loc) · 1.61 KB
/
build.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
let { writeFileSync, readFileSync, readdirSync, unlinkSync } = require('fs')
let { join, basename } = require('path')
readdirSync(__dirname)
.filter(file => /^[a-z]{2}.js$/.test(file) || file === 'index.js')
.forEach(file => {
unlinkSync(join(__dirname, file))
})
let langs = readdirSync(__dirname)
.filter(file => /^([a-z]{2}|dvorak|colemak).json$/.test(file))
.map(file => basename(file, '.json'))
.sort()
let preferredOrder = '.exports=func'
function sortHash(mappingTuple) {
let index = preferredOrder.indexOf(mappingTuple[0])
if (index > -1) {
return '\u0000' + String.fromCharCode(index)
}
return '\u0020' + mappingTuple[0]
}
function sortFn(a, b) {
a = sortHash(a)
b = sortHash(b)
if (a < b) {
return -1
} else if (a > b) {
return 1
} else {
return 0
}
}
function toJs(map) {
let tuples = []
for (let key in map) {
if (key === map[key]) {
continue
}
tuples.push([key, map[key]])
}
tuples.sort(sortFn)
let keys = tuples.map(tuple => tuple[0]).join('')
let values = tuples.map(tuple => tuple[1]).join('')
return (
"module.exports = require('./convert')(\n" +
' ' +
JSON.stringify(keys) +
',\n' +
' ' +
JSON.stringify(values) +
'\n' +
')\n'
)
}
for (let lang of langs) {
let mapping = JSON.parse(
readFileSync(join(__dirname, lang + '.json')).toString('utf-8')
)
writeFileSync(join(__dirname, lang + '.js'), toJs(mapping))
}
writeFileSync(
join(__dirname, 'index.js'),
'module.exports = {\n' +
langs.map(name => ' ' + name + ": require('./" + name + "')").join(',\n') +
'\n' +
'}\n'
)