| [e4c61dd] | 1 | #!/usr/bin/env node
|
|---|
| 2 |
|
|---|
| 3 | import {EOL} from "os";
|
|---|
| 4 | import {basename, dirname, resolve} from "path";
|
|---|
| 5 | import {readFileSync} from "fs";
|
|---|
| 6 | import {fileURLToPath} from "url";
|
|---|
| 7 | import rw from "rw";
|
|---|
| 8 | import {program} from "commander";
|
|---|
| 9 | import iconv from "iconv-lite";
|
|---|
| 10 | import {dsvFormat} from "../src/index.js";
|
|---|
| 11 |
|
|---|
| 12 | const progname = basename(process.argv[1]);
|
|---|
| 13 | const defaultOutDelimiter = progname.slice(-3) === "tsv" ? "\t" : ",";
|
|---|
| 14 |
|
|---|
| 15 | const options = program
|
|---|
| 16 | .version(JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), "../package.json"))).version)
|
|---|
| 17 | .usage("[options] [file]")
|
|---|
| 18 | .option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
|
|---|
| 19 | .option("-w, --output-delimiter <character>", "output delimiter character", defaultOutDelimiter)
|
|---|
| 20 | .option("-n, --newline-delimited", "accept newline-delimited JSON")
|
|---|
| 21 | .option("--input-encoding <encoding>", "input character encoding; defaults to “utf8”", "utf8")
|
|---|
| 22 | .option("--output-encoding <encoding>", "output character encoding; defaults to “utf8”", "utf8")
|
|---|
| 23 | .parse(process.argv)
|
|---|
| 24 | .opts();
|
|---|
| 25 |
|
|---|
| 26 | const outFormat = dsvFormat(options.outputDelimiter);
|
|---|
| 27 |
|
|---|
| 28 | rw.dash.readFile(program.args[0] || "-", (error, text) => {
|
|---|
| 29 | if (error) throw error;
|
|---|
| 30 | text = iconv.decode(text, options.inputEncoding);
|
|---|
| 31 | rw.dash.writeFile(options.out, iconv.encode(outFormat.format(options.newlineDelimited
|
|---|
| 32 | ? text.trim().split(/\r?\n/g).map((line) => JSON.parse(line))
|
|---|
| 33 | : JSON.parse(text)) + EOL, options.outputEncoding), (error) => {
|
|---|
| 34 | if (error) throw error;
|
|---|
| 35 | });
|
|---|
| 36 | });
|
|---|