@cto.af/chex-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/chex.js +51 -0
- package/package.json +30 -0
package/bin/chex.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
|
|
4
|
+
import {Command, Option} from 'commander';
|
|
5
|
+
import {Buffer} from 'buffer';
|
|
6
|
+
import fs from 'fs/promises';
|
|
7
|
+
import {hexDump} from '@cto.af/chex';
|
|
8
|
+
|
|
9
|
+
const program = new Command();
|
|
10
|
+
program
|
|
11
|
+
.argument('[file...]', 'file names to read or "-" for stdin')
|
|
12
|
+
.option('-c,--colors', 'force colors on even if stdout is not a tty')
|
|
13
|
+
.option('-d,--dots', 'use dots for more control characters, instead of Unicode representations')
|
|
14
|
+
.addOption(new Option(
|
|
15
|
+
'-s,--strings <encoding>',
|
|
16
|
+
'try to decode strings in this encoding'
|
|
17
|
+
).choices(['utf8', 'utf-8']))
|
|
18
|
+
.parse();
|
|
19
|
+
|
|
20
|
+
const opts = program.opts();
|
|
21
|
+
const {args} = program;
|
|
22
|
+
if (args.length === 0) {
|
|
23
|
+
args.push('-');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function readStdin() {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const bufs = [];
|
|
29
|
+
process.stdin.on('data', d => bufs.push(d));
|
|
30
|
+
process.stdin.on('end', () => resolve(new Uint8Array(Buffer.concat(bufs))));
|
|
31
|
+
process.stdin.on('error', reject);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function main(argv) {
|
|
36
|
+
for (const fn of argv) {
|
|
37
|
+
if (argv.length > 1) {
|
|
38
|
+
console.log(fn);
|
|
39
|
+
}
|
|
40
|
+
const buf = fn === '-' ?
|
|
41
|
+
await readStdin() :
|
|
42
|
+
new Uint8Array(await fs.readFile(fn));
|
|
43
|
+
console.log(hexDump(buf, {
|
|
44
|
+
colors: opts.colors || process.stdout.isTTY,
|
|
45
|
+
dots: opts.dots,
|
|
46
|
+
decode: opts.strings,
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
main(args).catch(console.error);
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cto.af/chex-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "hexdump -C as a small JS library",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"chex": "bin/chex.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/cto-af/chex.git",
|
|
13
|
+
"directory": "pkg/chex-cli"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"hexdump",
|
|
17
|
+
"hex",
|
|
18
|
+
"unicode",
|
|
19
|
+
"cli"
|
|
20
|
+
],
|
|
21
|
+
"author": "Joe Hildebrand <joe-github@cursive.net>",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@cto.af/chex": "workspace:*",
|
|
25
|
+
"commander": "12.1.0"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
}
|
|
30
|
+
}
|