Node-Red configuration
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dsv2json.js 1.7KB

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