1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | const fs = require('fs');
|
5 | const path = require('path');
|
6 | const yargs = require('yargs');
|
7 | const { omit } = require('../lib/object-utils');
|
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 JS template string 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 | const 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 templateVariables = { glyphMap: content, ...data };
|
48 | content = template.replace(
|
49 | /\${([^}]*)}/g,
|
50 | (_, key) => templateVariables[key]
|
51 | );
|
52 | }
|
53 |
|
54 | if (argv.output) {
|
55 | fs.writeFileSync(argv.output, content);
|
56 | } else {
|
57 | console.log(content);
|
58 | }
|
59 |
|
60 | if (argv.glyphmap) {
|
61 | fs.writeFileSync(argv.glyphmap, JSON.stringify(glyphMap, null, ' '));
|
62 | }
|