UNPKG

1.42 kBJavaScriptView Raw
1#!/usr/bin/env node
2const commander = require('commander');
3const d3Dsv = require('d3-dsv');
4const fs = require('fs');
5const path = require('path');
6const pkg = require('../package.json');
7
8function resolve(pathname) {
9 return path.resolve(process.cwd(), pathname);
10}
11
12commander.version(pkg.version);
13
14commander.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
22commander.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
30commander.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
43commander.parse(process.argv);
44
45if (process.argv.length === 2) {
46 commander.outputHelp();
47}