Skip to content

Commit ff3c114

Browse files
authored
Refactored CLI to be a separate package (#24)
* 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 * Bumped codepage-cli version
1 parent 62fd61e commit ff3c114

File tree

7 files changed

+181
-109
lines changed

7 files changed

+181
-109
lines changed

bin/codepage.njs

Lines changed: 0 additions & 98 deletions
This file was deleted.

package.json

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33
"version": "1.14.0",
44
"author": "SheetJS",
55
"description": "pure-JS library to handle codepages",
6-
"keywords": [ "codepage", "iconv", "convert", "strings" ],
7-
"bin": {
8-
"codepage": "./bin/codepage.njs"
9-
},
6+
"keywords": [
7+
"codepage",
8+
"iconv",
9+
"convert",
10+
"strings"
11+
],
1012
"main": "cputils.js",
1113
"types": "types",
1214
"browser": {
1315
"buffer": "false"
1416
},
15-
"dependencies": {
16-
"commander": "~2.14.1",
17-
"exit-on-epipe": "~1.0.1"
18-
},
1917
"devDependencies": {
2018
"voc": "~1.1.0",
2119
"mocha": "~2.5.3",
@@ -26,7 +24,10 @@
2624
"dtslint": "^0.1.2",
2725
"typescript": "2.2.0"
2826
},
29-
"repository": { "type":"git", "url":"git://github.com/SheetJS/js-codepage.git"},
27+
"repository": {
28+
"type": "git",
29+
"url": "git://github.com/SheetJS/js-codepage.git"
30+
},
3031
"scripts": {
3132
"pretest": "git submodule init && git submodule update",
3233
"test": "make test",
@@ -61,7 +62,11 @@
6162
"dist/sbcs.full.js",
6263
"dist/cpexcel.full.js"
6364
],
64-
"bugs": { "url": "https://github.com/SheetJS/js-codepage/issues" },
65+
"bugs": {
66+
"url": "https://github.com/SheetJS/js-codepage/issues"
67+
},
6568
"license": "Apache-2.0",
66-
"engines": { "node": ">=0.8" }
69+
"engines": {
70+
"node": ">=0.8"
71+
}
6772
}

packages/codepage-cli/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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 cli = require('../');
6+
7+
cli();

packages/codepage-cli/index.js

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

packages/codepage-cli/package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/codepage-cli/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "codepage-cli",
3+
"version": "1.0.5",
4+
"description": "CLI for js-codepage",
5+
"main": "index.js",
6+
"bin": {
7+
"codepage-cli": "./bin/codepage.njs"
8+
},
9+
"author": "Garrett Luu",
10+
"license": "Apache-2.0",
11+
"dependencies": {
12+
"codepage": "^1.14.0",
13+
"commander": "^5.1.0",
14+
"exit-on-epipe": "^1.0.1"
15+
}
16+
}

0 commit comments

Comments
 (0)
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