UNPKG

1.62 kBJavaScriptView Raw
1#!/usr/bin/env node
2/* eslint-disable no-console */
3
4const _ = require('lodash');
5const fs = require('fs');
6const path = require('path');
7const 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
20function 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
35let template;
36if (argv.template) {
37 template = fs.readFileSync(argv.template, { encoding: 'utf8' });
38}
39
40let data = _.omit(argv, '_ $0 o output t template g glyphmap'.split(' '));
41const glyphMap = extractGlyphMapFromCodepoints(argv._[0]);
42
43let content = JSON.stringify(glyphMap, null, ' ');
44if (template) {
45 const compiled = _.template(template);
46 data = data || {};
47 data.glyphMap = content;
48 content = compiled(data);
49}
50
51if (argv.output) {
52 fs.writeFileSync(argv.output, content);
53} else {
54 console.log(content);
55}
56
57if (argv.glyphmap) {
58 fs.writeFileSync(argv.glyphmap, JSON.stringify(glyphMap, null, ' '));
59}