codepage.njs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env node
  2. /* js-codepage (C) 2014 SheetJS -- http://sheetjs.com */
  3. /* vim: set ts=2 ft=javascript: */
  4. var codepage = require('../');
  5. var fs = require('fs'), program = require('commander');
  6. program
  7. .version(codepage.version)
  8. .usage('[options] <file>')
  9. .option('-f, --from-code <code>', 'codepage of input (default 65001 utf8)')
  10. .option('-t, --to-code <code>', 'codepage of output (default 65001 utf8)')
  11. .option('-o, --output <file>', 'output file (<file>.<to> if specified)')
  12. .option('-B, --bom', 'write BOM (for unicode codepages)')
  13. .option('-F, --force', 'force writing to stdout for non-utf8 codepages')
  14. .option('-l, --list', 'List supported codepages');
  15. program.on('--help', function() {
  16. console.log(' Codepage descriptions can be found in the README');
  17. console.log(' http://oss.sheetjs.com/js-codepage/README.md');
  18. console.log(' Support email: dev.codepage@sheetjs.com');
  19. });
  20. program.parse(process.argv);
  21. if(program.list) {
  22. Object.keys(codepage).forEach(function(x) { if(+x == x) console.log(x); });
  23. process.exit();
  24. }
  25. var fr = +program.fromCode || 65001;
  26. var to = +program.toCode || 65001;
  27. var f = program.args[0];
  28. var o = program.output;
  29. if(!process.stdin.isTTY) f = f || "-";
  30. if(f !== "-" && !fs.existsSync(f)) {
  31. console.error('codepage: must specify a filename');
  32. process.exit(13);
  33. }
  34. if(f === "-") process.stdin.pipe(require('concat-stream')(process_text));
  35. else process_text(fs.readFileSync(f));
  36. function process_text(text) {
  37. var dec = codepage.utils.decode(fr, text);
  38. var bom = {
  39. 1200: new Buffer([0xFF, 0xFE]),
  40. 1201: new Buffer([0xFE, 0xFF]),
  41. 12000: new Buffer([0xFF, 0xFE, 0x00, 0x00]),
  42. 12001: new Buffer([0x00, 0x00, 0xFE, 0xFF]),
  43. 16969: new Buffer([0x69, 0x69]),
  44. 65000: new Buffer([0x2B, 0x2F, 0x76, 0x2B]),
  45. 65001: new Buffer([0xEF, 0xBB, 0xBF])
  46. }
  47. var mybom = (program.bom && bom[fr] ? bom[fr] : "");
  48. var out = to === 65001 ? dec.toString('utf8') : codepage.utils.encode(to, dec);
  49. /* if output file is specified */
  50. if(o) writefile(o, out, mybom);
  51. /* utf8 -> print to stdout */
  52. else if(to === 65001) logit(out, mybom);
  53. /* stdout piped to process -> print */
  54. else if(!process.stdout.isTTY) logit(out, mybom);
  55. /* forced */
  56. else if(program.force) logit(out, mybom);
  57. /* input file specified -> write to file */
  58. else if(f !== "-") writefile(f + "." + to, out, mybom);
  59. else {
  60. console.error('codepage: use force (-F, --force) to print ' + to + ' codes');
  61. process.exit(14);
  62. }
  63. }
  64. function logit(out, bom) {
  65. process.stdout.write(bom);
  66. process.stdout.write(out);
  67. }
  68. function writefile(o, out, bom) {
  69. fs.writeFileSync(o, bom);
  70. fs.appendFileSync(o, out);
  71. }