codepage.njs 3.3 KB

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