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.

dsv2dsv.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435
  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 defaultOutDelimiter = progname.slice(-3) === "tsv" ? "\t" : ",";
  13. const options = program
  14. .version(JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), "../package.json"))).version)
  15. .usage("[options] [file]")
  16. .option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
  17. .option("-r, --input-delimiter <character>", "input delimiter character", defaultInDelimiter)
  18. .option("-w, --output-delimiter <character>", "output delimiter character", defaultOutDelimiter)
  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. const outFormat = dsvFormat(options.outputDelimiter);
  25. rw.dash.readFile(program.args[0] || "-", (error, text) => {
  26. if (error) throw error;
  27. rw.dash.writeFile("-", iconv.encode(outFormat.format(inFormat.parse(iconv.decode(text, options.inputEncoding))) + EOL, options.outputEncoding), (error) => {
  28. if (error) throw error;
  29. });
  30. });