1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | const _ = require('lodash');
|
5 | const fs = require('fs');
|
6 | const path = require('path');
|
7 | const argv = require('yargs')
|
8 | .usage(
|
9 | 'Usage: $0 [options] path/to/codepoints \nFor default template please provide --componentName and --fontFamily'
|
10 | )
|
11 | .demand(1)
|
12 | .default('t', path.resolve(__dirname, '..', 'templates/bundled-icon-set.tpl'))
|
13 | .describe('t', 'Template in lodash format')
|
14 | .alias('t', 'template')
|
15 | .describe('o', 'Save output to file, defaults to STDOUT')
|
16 | .alias('o', 'output')
|
17 | .describe('g', 'Save glyphmap JSON to file')
|
18 | .alias('g', 'glyphmap').argv;
|
19 |
|
20 | function extractGlyphMapFromCodepoints(fileName) {
|
21 | const codepoints = fs
|
22 | .readFileSync(fileName, { encoding: 'utf8' })
|
23 | .split('\n');
|
24 | const glyphMap = {};
|
25 | codepoints.forEach(point => {
|
26 | const parts = point.split(' ');
|
27 | if (parts.length === 2) {
|
28 | glyphMap[parts[0].replace(/_/g, '-')] = parseInt(parts[1], 16);
|
29 | }
|
30 | });
|
31 |
|
32 | return glyphMap;
|
33 | }
|
34 |
|
35 | let template;
|
36 | if (argv.template) {
|
37 | template = fs.readFileSync(argv.template, { encoding: 'utf8' });
|
38 | }
|
39 |
|
40 | let data = _.omit(argv, '_ $0 o output t template g glyphmap'.split(' '));
|
41 | const glyphMap = extractGlyphMapFromCodepoints(argv._[0]);
|
42 |
|
43 | let content = JSON.stringify(glyphMap, null, ' ');
|
44 | if (template) {
|
45 | const compiled = _.template(template);
|
46 | data = data || {};
|
47 | data.glyphMap = content;
|
48 | content = compiled(data);
|
49 | }
|
50 |
|
51 | if (argv.output) {
|
52 | fs.writeFileSync(argv.output, content);
|
53 | } else {
|
54 | console.log(content);
|
55 | }
|
56 |
|
57 | if (argv.glyphmap) {
|
58 | fs.writeFileSync(argv.glyphmap, JSON.stringify(glyphMap, null, ' '));
|
59 | }
|