Skip to content

Refactored CLI to be a separate package #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 0 additions & 98 deletions bin/codepage.njs

This file was deleted.

27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions packages/codepage-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
7 changes: 7 additions & 0 deletions packages/codepage-cli/bin/codepage.njs
Original file line number Diff line number Diff line change
@@ -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();
102 changes: 102 additions & 0 deletions packages/codepage-cli/index.js
Original file line number Diff line number Diff line change
@@ -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] <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)')
.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<number>*/ = [];
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<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]);

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;
39 changes: 39 additions & 0 deletions packages/codepage-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions packages/codepage-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "codepage-cli",
"version": "1.0.5",
"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"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy