From e1d0f22609eebfa573c3bccc882735bbf4ad0c4f Mon Sep 17 00:00:00 2001 From: Garrett Luu Date: Tue, 16 Jun 2020 15:28:22 -0700 Subject: [PATCH] codepage-cli refactor * Moved cli to packages folder * Removed bin * Renamed cli folder * Fixed cli issues * Fixed dependency issues * Fixed duplicate script and cli name * Removed cli.js * Moved run function to index.js * Added author and removed fs dependency * Removed cli dependencies * Removed scripts --- bin/codepage.njs | 98 ----------------------- package.json | 27 ++++--- packages/codepage-cli/.gitignore | 1 + packages/codepage-cli/bin/codepage.njs | 7 ++ packages/codepage-cli/index.js | 102 ++++++++++++++++++++++++ packages/codepage-cli/package-lock.json | 39 +++++++++ packages/codepage-cli/package.json | 16 ++++ 7 files changed, 181 insertions(+), 109 deletions(-) delete mode 100755 bin/codepage.njs create mode 100644 packages/codepage-cli/.gitignore create mode 100755 packages/codepage-cli/bin/codepage.njs create mode 100644 packages/codepage-cli/index.js create mode 100644 packages/codepage-cli/package-lock.json create mode 100644 packages/codepage-cli/package.json diff --git a/bin/codepage.njs b/bin/codepage.njs deleted file mode 100755 index d09e1b0..0000000 --- a/bin/codepage.njs +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/env node -/* js-codepage (C) 2014-present SheetJS -- http://sheetjs.com */ -/* vim: set ts=2 ft=javascript: */ -/* eslint-env node */ -var codepage = require('../'); -require('exit-on-epipe'); -var fs = require('fs'), program/*:any*/ = (require('commander')/*:any*/); -program - .version(codepage.version) - .usage('[options] ') - .option('-f, --from-code ', 'codepage of input (default 65001 utf8)') - .option('-t, --to-code ', 'codepage of output (default 65001 utf8)') - .option('-o, --output ', 'output file (. if specified)') - .option('-B, --bom', 'write BOM (for unicode codepages)') - .option('-F, --force', 'force writing to stdout for non-utf8 codepages') - .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); - -if(program.list) { - var l/*:Array*/ = []; - 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); }); - l.sort(function(a,b) { return a-b; }).forEach(function(x) { console.log(x); }); - process.exit(); -} - -var fr = +program.fromCode || 65001; -var to = +program.toCode || 65001; -var f = program.args[0]; -var o = program.output; - -if(!process.stdin.isTTY) f = f || "-"; - -if(f !== "-" && !fs.existsSync(f)) { - console.error('codepage: must specify a filename'); - process.exit(13); -} - -function concat(func) { - // $FlowIgnore - var writable = require('stream').Writable(); - var buf = []; - 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)); }); - return writable; -} - -if(f === "-") process.stdin.pipe(concat(process_text)); -else process_text(fs.readFileSync(f)); - -function process_text(text/*:Buffer*/) { - var dec/*:Buffer*/ = (codepage.utils.decode(fr, text)/*:any*/); - - var bom/*:Array*/ = []; - 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]); - - var mybom = (program.bom && bom[to] ? bom[to] : ""); - var out/*:any*/ = to === 65001 ? dec.toString('utf8') : codepage.utils.encode(to, dec); - - /* 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); - else { - console.error('codepage: use force (-F, --force) to print ' + to + ' codes'); - process.exit(14); - } -} - -function logit(out/*:Buffer*/, bom) { - process.stdout.write(bom); - process.stdout.write(out); -} - -function writefile(o, out/*:Buffer*/, bom) { - fs.writeFileSync(o, bom); - fs.appendFileSync(o, out); -} diff --git a/package.json b/package.json index 5eb99ac..35696b9 100644 --- a/package.json +++ b/package.json @@ -3,19 +3,17 @@ "version": "1.14.0", "author": "SheetJS", "description": "pure-JS library to handle codepages", - "keywords": [ "codepage", "iconv", "convert", "strings" ], - "bin": { - "codepage": "./bin/codepage.njs" - }, + "keywords": [ + "codepage", + "iconv", + "convert", + "strings" + ], "main": "cputils.js", "types": "types", "browser": { "buffer": "false" }, - "dependencies": { - "commander": "~2.14.1", - "exit-on-epipe": "~1.0.1" - }, "devDependencies": { "voc": "~1.1.0", "mocha": "~2.5.3", @@ -26,7 +24,10 @@ "dtslint": "^0.1.2", "typescript": "2.2.0" }, - "repository": { "type":"git", "url":"git://github.com/SheetJS/js-codepage.git"}, + "repository": { + "type": "git", + "url": "git://github.com/SheetJS/js-codepage.git" + }, "scripts": { "pretest": "git submodule init && git submodule update", "test": "make test", @@ -61,7 +62,11 @@ "dist/sbcs.full.js", "dist/cpexcel.full.js" ], - "bugs": { "url": "https://github.com/SheetJS/js-codepage/issues" }, + "bugs": { + "url": "https://github.com/SheetJS/js-codepage/issues" + }, "license": "Apache-2.0", - "engines": { "node": ">=0.8" } + "engines": { + "node": ">=0.8" + } } diff --git a/packages/codepage-cli/.gitignore b/packages/codepage-cli/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/packages/codepage-cli/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/packages/codepage-cli/bin/codepage.njs b/packages/codepage-cli/bin/codepage.njs new file mode 100755 index 0000000..d245a83 --- /dev/null +++ b/packages/codepage-cli/bin/codepage.njs @@ -0,0 +1,7 @@ +#!/usr/bin/env node +/* js-codepage (C) 2014-present SheetJS -- http://sheetjs.com */ +/* vim: set ts=2 ft=javascript: */ +/* eslint-env node */ +var cli = require('../'); + +cli(); \ No newline at end of file diff --git a/packages/codepage-cli/index.js b/packages/codepage-cli/index.js new file mode 100644 index 0000000..582603c --- /dev/null +++ b/packages/codepage-cli/index.js @@ -0,0 +1,102 @@ +#!/usr/bin/env node +/* js-codepage (C) 2014-present SheetJS -- http://sheetjs.com */ +/* vim: set ts=2 ft=javascript: */ +/* eslint-env node */ +var codepage = require('codepage'); +require('exit-on-epipe'); +var fs = require('fs'), program/*:any*/ = (require('commander')/*:any*/); +function run() { + program + .version(codepage.version) + .usage('[options] ') + .option('-f, --from-code ', 'codepage of input (default 65001 utf8)') + .option('-t, --to-code ', 'codepage of output (default 65001 utf8)') + .option('-o, --output ', 'output file (. if specified)') + .option('-B, --bom', 'write BOM (for unicode codepages)') + .option('-F, --force', 'force writing to stdout for non-utf8 codepages') + .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); + + if (program.list) { + var l/*:Array*/ = []; + 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); }); + l.sort(function (a, b) { return a - b; }).forEach(function (x) { console.log(x); }); + process.exit(); + } + + var fr = +program.fromCode || 65001; + var to = +program.toCode || 65001; + var f = program.args[0]; + var o = program.output; + + if (!process.stdin.isTTY) f = f || "-"; + + if (f !== "-" && !fs.existsSync(f)) { + console.error('codepage: must specify a filename'); + process.exit(13); + } + + function concat(func) { + // $FlowIgnore + var writable = require('stream').Writable(); + var buf = []; + 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)); }); + return writable; + } + + if (f === "-") process.stdin.pipe(concat(process_text)); + else process_text(fs.readFileSync(f)); + + function process_text(text/*:Buffer*/) { + var dec/*:Buffer*/ = (codepage.utils.decode(fr, text)/*:any*/); + + var bom/*:Array*/ = []; + 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]); + + var mybom = (program.bom && bom[to] ? bom[to] : ""); + var out/*:any*/ = to === 65001 ? dec.toString('utf8') : codepage.utils.encode(to, dec); + + /* 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); + else { + console.error('codepage: use force (-F, --force) to print ' + to + ' codes'); + process.exit(14); + } + } + + function logit(out/*:Buffer*/, bom) { + process.stdout.write(bom); + process.stdout.write(out); + } + + function writefile(o, out/*:Buffer*/, bom) { + fs.writeFileSync(o, bom); + fs.appendFileSync(o, out); + } +} + +module.exports = run; \ No newline at end of file diff --git a/packages/codepage-cli/package-lock.json b/packages/codepage-cli/package-lock.json new file mode 100644 index 0000000..c83b1a3 --- /dev/null +++ b/packages/codepage-cli/package-lock.json @@ -0,0 +1,39 @@ +{ + "name": "codepage-cli", + "version": "1.0.3", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "codepage": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/codepage/-/codepage-1.14.0.tgz", + "integrity": "sha1-jL4lSBMjVZ19MHVxsP/5HnodL5k=", + "requires": { + "commander": "~2.14.1", + "exit-on-epipe": "~1.0.1" + }, + "dependencies": { + "commander": { + "version": "2.14.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.14.1.tgz", + "integrity": "sha512-+YR16o3rK53SmWHU3rEM3tPAh2rwb1yPcQX5irVn7mb0gXbwuCCrnkbV5+PBfETdfg1vui07nM6PCG1zndcjQw==" + } + } + }, + "commander": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz", + "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==" + }, + "exit-on-epipe": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/exit-on-epipe/-/exit-on-epipe-1.0.1.tgz", + "integrity": "sha512-h2z5mrROTxce56S+pnvAV890uu7ls7f1kEvVGJbw1OlFH3/mlJ5bkXu0KRyW94v37zzHPiUd55iLn3DA7TjWpw==" + }, + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" + } + } +} diff --git a/packages/codepage-cli/package.json b/packages/codepage-cli/package.json new file mode 100644 index 0000000..a38c2ed --- /dev/null +++ b/packages/codepage-cli/package.json @@ -0,0 +1,16 @@ +{ + "name": "codepage-cli", + "version": "1.0.4", + "description": "CLI for js-codepage", + "main": "index.js", + "bin": { + "codepage-cli": "./bin/codepage.njs" + }, + "author": "Garrett Luu", + "license": "Apache-2.0", + "dependencies": { + "codepage": "^1.14.0", + "commander": "^5.1.0", + "exit-on-epipe": "^1.0.1" + } +}