Skip to content

Commit 9833239

Browse files
committed
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
1 parent 62fd61e commit 9833239

File tree

8 files changed

+188
-103
lines changed

8 files changed

+188
-103
lines changed

bin/codepage.njs

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

cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
var cli = require('codepage-cli');

package.json

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
"version": "1.14.0",
44
"author": "SheetJS",
55
"description": "pure-JS library to handle codepages",
6-
"keywords": [ "codepage", "iconv", "convert", "strings" ],
6+
"keywords": [
7+
"codepage",
8+
"iconv",
9+
"convert",
10+
"strings"
11+
],
712
"bin": {
8-
"codepage": "./bin/codepage.njs"
13+
"codepage-cli": "./cli.js"
914
},
1015
"main": "cputils.js",
1116
"types": "types",
1217
"browser": {
1318
"buffer": "false"
1419
},
1520
"dependencies": {
21+
"codepage-cli": "^1.0.4",
1622
"commander": "~2.14.1",
1723
"exit-on-epipe": "~1.0.1"
1824
},
@@ -26,7 +32,10 @@
2632
"dtslint": "^0.1.2",
2733
"typescript": "2.2.0"
2834
},
29-
"repository": { "type":"git", "url":"git://github.com/SheetJS/js-codepage.git"},
35+
"repository": {
36+
"type": "git",
37+
"url": "git://github.com/SheetJS/js-codepage.git"
38+
},
3039
"scripts": {
3140
"pretest": "git submodule init && git submodule update",
3241
"test": "make test",
@@ -61,7 +70,11 @@
6170
"dist/sbcs.full.js",
6271
"dist/cpexcel.full.js"
6372
],
64-
"bugs": { "url": "https://github.com/SheetJS/js-codepage/issues" },
73+
"bugs": {
74+
"url": "https://github.com/SheetJS/js-codepage/issues"
75+
},
6576
"license": "Apache-2.0",
66-
"engines": { "node": ">=0.8" }
77+
"engines": {
78+
"node": ">=0.8"
79+
}
6780
}

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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
run();
103+
104+
module.exports = run;

packages/codepage-cli/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var cli = require('./bin/codepage.njs');
2+
3+
module.exports = cli;

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "codepage-cli",
3+
"version": "1.0.4",
4+
"description": "CLI for js-codepage",
5+
"main": "index.js",
6+
"bin": {
7+
"codepage-cli": "./bin/codepage.njs"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"codepage": "^1.14.0",
16+
"commander": "^5.1.0",
17+
"exit-on-epipe": "^1.0.1",
18+
"fs": "0.0.1-security"
19+
}
20+
}

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