1 | #!/usr/bin/env node
|
2 | const commander = require('commander');
|
3 | const d3Dsv = require('d3-dsv');
|
4 | const fs = require('fs');
|
5 | const path = require('path');
|
6 | const pkg = require('../package.json');
|
7 |
|
8 | function resolve(pathname) {
|
9 | return path.resolve(process.cwd(), pathname);
|
10 | }
|
11 |
|
12 | commander.version(pkg.version);
|
13 |
|
14 | commander.command('csv2json <filename>')
|
15 | .description('convert csv file to json')
|
16 | .action(filename => {
|
17 | const content = fs.readFileSync(resolve(filename), 'utf8');
|
18 | const converted = JSON.stringify(d3Dsv.csvParse(content), null, 2);
|
19 | process.stdout.write(converted);
|
20 | });
|
21 |
|
22 | commander.command('tsv2json <filename>')
|
23 | .description('convert tsv file to json')
|
24 | .action(filename => {
|
25 | const content = fs.readFileSync(resolve(filename), 'utf8');
|
26 | const converted = JSON.stringify(d3Dsv.tsvParse(content), null, 2);
|
27 | process.stdout.write(converted);
|
28 | });
|
29 |
|
30 | commander.command('compress-json <file>')
|
31 | .description('convert tsv file to json')
|
32 | .option('--override', 'override the origin file')
|
33 | .action((filename, options) => {
|
34 | const content = fs.readFileSync(resolve(filename), 'utf8');
|
35 | const converted = JSON.stringify(JSON.parse(content));
|
36 | if (options.override) {
|
37 | fs.writeFileSync(resolve(filename), converted, 'utf8');
|
38 | } else {
|
39 | process.stdout.write(converted);
|
40 | }
|
41 | });
|
42 |
|
43 | commander.parse(process.argv);
|
44 |
|
45 | if (process.argv.length === 2) {
|
46 | commander.outputHelp();
|
47 | }
|