UNPKG

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