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