Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

json2dsv.js 1.5KB

123456789101112131415161718192021222324252627282930313233343536
  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 defaultOutDelimiter = progname.slice(-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("-w, --output-delimiter <character>", "output delimiter character", defaultOutDelimiter)
  17. .option("-n, --newline-delimited", "accept newline-delimited JSON")
  18. .option("--input-encoding <encoding>", "input character encoding; defaults to “utf8”", "utf8")
  19. .option("--output-encoding <encoding>", "output character encoding; defaults to “utf8”", "utf8")
  20. .parse(process.argv)
  21. .opts();
  22. const outFormat = dsvFormat(options.outputDelimiter);
  23. rw.dash.readFile(program.args[0] || "-", (error, text) => {
  24. if (error) throw error;
  25. text = iconv.decode(text, options.inputEncoding);
  26. rw.dash.writeFile(options.out, iconv.encode(outFormat.format(options.newlineDelimited
  27. ? text.trim().split(/\r?\n/g).map((line) => JSON.parse(line))
  28. : JSON.parse(text)) + EOL, options.outputEncoding), (error) => {
  29. if (error) throw error;
  30. });
  31. });