/
makeArrows.js
77 lines (61 loc) · 1.87 KB
/
makeArrows.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
const path = require('path');
const fs = require('fs');
// generalize so we can use this script in other repos
// so you can call:
// makeArrows <srcPath> <outputPath>
const pathToCombinedTranslationKeys = process.argv[2] || path.join(
__dirname,
'./translationKeys/combined-translation-keys.txt'
);
const pathToArrowsOut = process.argv[3] || path.join(
__dirname,
'../src/locales/xx.js'
);
const wordRE = /^[A-Za-z]+$/;
const maxLineLen = 80;
function makeArrows() {
const lines = fs
.readFileSync(pathToCombinedTranslationKeys, 'utf-8')
.split(/\r?\n/);
const entries = lines
.map(line => {
const key = line.split(/\/\//)[0].trim();
const quoteChar = key.indexOf("'") === -1 ? "'" : '"';
if (key.indexOf('"') !== -1) {
throw new Error('double quotes are not supported, key: ' + key);
}
const maybeQuoteKey = wordRE.test(key)
? key
: quoteChar + key + quoteChar;
const arrowStr = arrowPad(getArrowLen(key));
const quotedVal = quoteChar + arrowStr + key + arrowStr + quoteChar + ',';
const singleLine = ' ' + maybeQuoteKey + ': ' + quotedVal;
if (singleLine.length <= maxLineLen) {
return singleLine;
}
return ' ' + maybeQuoteKey + ':\n ' + quotedVal;
})
.join('\n');
const head = 'export default {';
const tail = '};\n';
fs.writeFileSync(pathToArrowsOut, [head, entries, tail].join('\n'));
console.log('arrows mock translation written to: ' + pathToArrowsOut);
}
// inferred from the arrow file Greg provided
const arrowFactor = 5.7;
function getArrowLen(key) {
return Math.max(1, Math.round(key.length / arrowFactor));
}
function arrowPad(n) {
let out = '';
for (let i = 0; i < n; i++) {
out += '⇚';
}
return out;
}
makeArrows();
process.on('exit', function(code) {
if (code === 1) {
throw new Error('makeArrows failed.');
}
});