generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·74 lines (68 loc) · 2.54 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
import { readJSONFile, writeJSONFile } from '@shgysk8zer0/npm-utils/json';
import { writeYAMLFile } from '@shgysk8zer0/npm-utils/yaml';
import { readCSVFile } from './csv.js';
import { program } from 'commander';
import { extname, basename } from 'node:path';
import { generateSymbols, generateSymbolsFromDirectory, generateSymbol, writeSVG } from './generator.js';
async function init() {
const {
name: NAME,
version: VERSION,
description: DESCRIPTION,
} = await readJSONFile(new URL('./package.json', import.meta.url).pathname);
program
.name(NAME)
.version(VERSION)
.description(DESCRIPTION)
.option('-c, --config [config]', 'JSON or YAML config file')
.option('-d, --directory [directory]', 'path to directory of SVGs')
.option('-e, --encoding [encoding]', 'encoding', 'utf8')
.option('-f, --format [format]', 'output format for migrating from CSV', 'json') // Migrate opt
.option('-l, --list [list]', 'comma separated list of SVGs')
.option('-m, --migrate [migrate]', 'path to deprecated CSV config file') // Migrate opt
.option('-o, --output [output]', 'output file')
.parse(process.argv);
return {
args: program.args,
opts: program.opts(),
};
}
init().then(async ({ opts: { directory, config, encoding, output, list, migrate, format }}) => {
if (typeof directory === 'string') {
await generateSymbolsFromDirectory(directory, { output, encoding });
} else if (typeof config === 'string') {
await generateSymbols(config, { encoding, output });
} else if (typeof list === 'string') {
if (typeof output !== 'string') {
throw new Error('Output is a required options for SVG lists');
} else {
const symbols = await Promise.all(
list.split(',').map(async svg => {
const id = basename(svg).replace(extname(svg), '');
return await generateSymbol(id, svg, { encoding });
})
);
await writeSVG(output, symbols, { encoding });
}
} else if (typeof migrate === 'string') {
if (typeof output !== 'string') {
throw new Error('Output is a required option for migrations.');
} else {
switch(format) {
case 'json':
await readCSVFile(migrate, { encoding })
.then(config => writeJSONFile(output, Object.fromEntries(config), { encoding }));
break;
case 'yaml':
await readCSVFile(migrate, { encoding })
.then(config => writeYAMLFile(output, Object.fromEntries(config), { encoding }));
break;
default:
throw new Error(`Unknown output format: "${format}"`);
}
}
} else {
throw new Error('Either config or directory are required options.');
}
});