UNPKG

1.64 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 yargs = require('yargs');
8
9const { 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
22function 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
37let template;
38if (argv.template) {
39 template = fs.readFileSync(argv.template, { encoding: 'utf8' });
40}
41
42let data = _.omit(argv, '_ $0 o output t template g glyphmap'.split(' '));
43const glyphMap = extractGlyphMapFromCodepoints(argv._[0]);
44
45let content = JSON.stringify(glyphMap, null, ' ');
46if (template) {
47 const compiled = _.template(template);
48 data = data || {};
49 data.glyphMap = content;
50 content = compiled(data);
51}
52
53if (argv.output) {
54 fs.writeFileSync(argv.output, content);
55} else {
56 console.log(content);
57}
58
59if (argv.glyphmap) {
60 fs.writeFileSync(argv.glyphmap, JSON.stringify(glyphMap, null, ' '));
61}