2014-04-22 23:02:59 +00:00
|
|
|
#!/usr/bin/env node
|
2016-01-16 03:27:02 +00:00
|
|
|
/* js-codepage (C) 2014-present SheetJS -- http://sheetjs.com */
|
2014-04-22 23:02:59 +00:00
|
|
|
/* vim: set ts=2 ft=javascript: */
|
2018-01-18 22:47:47 +00:00
|
|
|
/* eslint-env node */
|
2014-04-22 23:02:59 +00:00
|
|
|
var codepage = require('../');
|
2016-01-16 03:51:05 +00:00
|
|
|
require('exit-on-epipe');
|
2016-10-15 06:09:05 +00:00
|
|
|
var fs = require('fs'), program/*:any*/ = (require('commander')/*:any*/);
|
2014-04-22 23:02:59 +00:00
|
|
|
program
|
2014-07-13 08:30:13 +00:00
|
|
|
.version(codepage.version)
|
2014-04-22 23:02:59 +00:00
|
|
|
.usage('[options] <file>')
|
|
|
|
.option('-f, --from-code <code>', 'codepage of input (default 65001 utf8)')
|
|
|
|
.option('-t, --to-code <code>', 'codepage of output (default 65001 utf8)')
|
|
|
|
.option('-o, --output <file>', 'output file (<file>.<to> if specified)')
|
2014-07-13 08:30:13 +00:00
|
|
|
.option('-B, --bom', 'write BOM (for unicode codepages)')
|
2014-10-19 02:39:39 +00:00
|
|
|
.option('-F, --force', 'force writing to stdout for non-utf8 codepages')
|
2014-04-22 23:02:59 +00:00
|
|
|
.option('-l, --list', 'List supported codepages');
|
|
|
|
|
|
|
|
program.on('--help', function() {
|
|
|
|
console.log(' Codepage descriptions can be found in the README');
|
|
|
|
console.log(' http://oss.sheetjs.com/js-codepage/README.md');
|
|
|
|
console.log(' Support email: dev.codepage@sheetjs.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
program.parse(process.argv);
|
|
|
|
|
2014-07-13 08:30:13 +00:00
|
|
|
if(program.list) {
|
2016-10-15 06:09:05 +00:00
|
|
|
var l/*:Array<number>*/ = [];
|
2018-01-18 22:47:47 +00:00
|
|
|
Object.keys(codepage).forEach(function(x) { if(parseInt(x, 10) == +x) l.push(+x); });
|
|
|
|
Object.keys(codepage.utils.magic).forEach(function(x) { if(parseInt(x, 10) == +x && +x != 16969) l.push(+x); });
|
2016-10-15 06:09:05 +00:00
|
|
|
l.sort(function(a,b) { return a-b; }).forEach(function(x) { console.log(x); });
|
2014-07-13 08:30:13 +00:00
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2014-10-19 02:39:39 +00:00
|
|
|
var fr = +program.fromCode || 65001;
|
|
|
|
var to = +program.toCode || 65001;
|
2014-04-22 23:02:59 +00:00
|
|
|
var f = program.args[0];
|
|
|
|
var o = program.output;
|
2014-09-27 18:05:43 +00:00
|
|
|
|
|
|
|
if(!process.stdin.isTTY) f = f || "-";
|
|
|
|
|
|
|
|
if(f !== "-" && !fs.existsSync(f)) {
|
|
|
|
console.error('codepage: must specify a filename');
|
|
|
|
process.exit(13);
|
2014-07-13 08:30:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:41:27 +00:00
|
|
|
function concat(func) {
|
2018-01-18 22:47:47 +00:00
|
|
|
// $FlowIgnore
|
2017-08-01 23:41:27 +00:00
|
|
|
var writable = require('stream').Writable();
|
|
|
|
var buf = [];
|
2018-01-18 22:47:47 +00:00
|
|
|
writable._write = function(chunk, e, cb) { buf.push(chunk); cb(); };
|
|
|
|
writable._writev = function(chunks, cb) { chunks.forEach(function(c) { buf.push(c.chunk); cb(); }); };
|
|
|
|
writable.on('finish', function() { func(Buffer.concat(buf)); });
|
2017-08-01 23:41:27 +00:00
|
|
|
return writable;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(f === "-") process.stdin.pipe(concat(process_text));
|
2014-09-27 18:05:43 +00:00
|
|
|
else process_text(fs.readFileSync(f));
|
|
|
|
|
2017-07-28 06:14:33 +00:00
|
|
|
function process_text(text/*:Buffer*/) {
|
2018-01-18 22:47:47 +00:00
|
|
|
var dec/*:Buffer*/ = (codepage.utils.decode(fr, text)/*:any*/);
|
2014-09-27 18:05:43 +00:00
|
|
|
|
2016-10-15 06:09:05 +00:00
|
|
|
var bom/*:Array<Buffer>*/ = [];
|
|
|
|
bom[1200] = new Buffer([0xFF, 0xFE]);
|
|
|
|
bom[1201] = new Buffer([0xFE, 0xFF]);
|
|
|
|
bom[12000] = new Buffer([0xFF, 0xFE, 0x00, 0x00]);
|
|
|
|
bom[12001] = new Buffer([0x00, 0x00, 0xFE, 0xFF]);
|
|
|
|
bom[16969] = new Buffer([0x69, 0x69]);
|
|
|
|
bom[65000] = new Buffer([0x2B, 0x2F, 0x76, 0x2B]);
|
|
|
|
bom[65001] = new Buffer([0xEF, 0xBB, 0xBF]);
|
2014-09-27 18:05:43 +00:00
|
|
|
|
2017-04-02 19:34:44 +00:00
|
|
|
var mybom = (program.bom && bom[to] ? bom[to] : "");
|
2017-07-28 06:14:33 +00:00
|
|
|
var out/*:any*/ = to === 65001 ? dec.toString('utf8') : codepage.utils.encode(to, dec);
|
2014-10-19 02:39:39 +00:00
|
|
|
|
|
|
|
/* if output file is specified */
|
|
|
|
if(o) writefile(o, out, mybom);
|
|
|
|
/* utf8 -> print to stdout */
|
|
|
|
else if(to === 65001) logit(out, mybom);
|
|
|
|
/* stdout piped to process -> print */
|
|
|
|
else if(!process.stdout.isTTY) logit(out, mybom);
|
|
|
|
/* forced */
|
|
|
|
else if(program.force) logit(out, mybom);
|
|
|
|
/* input file specified -> write to file */
|
|
|
|
else if(f !== "-") writefile(f + "." + to, out, mybom);
|
2014-09-27 18:05:43 +00:00
|
|
|
else {
|
2014-10-19 02:39:39 +00:00
|
|
|
console.error('codepage: use force (-F, --force) to print ' + to + ' codes');
|
|
|
|
process.exit(14);
|
2014-09-27 18:05:43 +00:00
|
|
|
}
|
2014-07-13 08:30:13 +00:00
|
|
|
}
|
2014-10-19 02:39:39 +00:00
|
|
|
|
2017-07-28 06:14:33 +00:00
|
|
|
function logit(out/*:Buffer*/, bom) {
|
2014-10-19 02:39:39 +00:00
|
|
|
process.stdout.write(bom);
|
|
|
|
process.stdout.write(out);
|
|
|
|
}
|
|
|
|
|
2017-07-28 06:14:33 +00:00
|
|
|
function writefile(o, out/*:Buffer*/, bom) {
|
2014-10-19 02:39:39 +00:00
|
|
|
fs.writeFileSync(o, bom);
|
|
|
|
fs.appendFileSync(o, out);
|
|
|
|
}
|