Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f575f7d

Browse files
committedJan 29, 2025·
Add CLI support for wallet-export with validation command and output options
1 parent 8fa25f5 commit f575f7d

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
 

‎package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"name": "@interop/wallet-export-ts",
33
"description": "A Javascript/Typescript library for exporting Universal Wallet Backup Containers.",
44
"version": "0.1.6",
5+
"bin": {
6+
"wallet-export": "./dist/cli.js"
7+
},
58
"scripts": {
69
"build": "npm run clear && tsc -p tsconfig.json && ./build-dist.sh",
7-
"clear": "rimraf dist/*",
10+
"clear": "npx rimraf dist/*",
811
"lint": "eslint .",
912
"lint:fix": "eslint --fix .",
1013
"prepare": "npm run build",
@@ -63,7 +66,7 @@
6366
"mocha": "^10.2.0",
6467
"nyc": "^15.1.0",
6568
"prettier": "^3.1.0",
66-
"rimraf": "^5.0.5",
69+
"rimraf": "^5.0.10",
6770
"ts-node": "^10.9.1",
6871
"typescript": "^5.2.2"
6972
},

‎src/cli.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
3+
import { parseArgs } from 'node:util'
4+
import fs from 'node:fs'
5+
import { type Readable } from 'node:stream'
6+
import { validateExportStream } from './verify.js'
7+
8+
// Parse command-line arguments
9+
const { values, positionals } = parseArgs({
10+
options: {
11+
output: {
12+
type: 'string',
13+
short: 'o'
14+
}
15+
},
16+
allowPositionals: true
17+
})
18+
19+
const [command, filePath] = positionals
20+
21+
if (command !== 'validate') {
22+
console.error('Usage: wallet-export validate <path-to-export.tsr>')
23+
process.exit(1)
24+
}
25+
26+
// Handle stdin (e.g., `cat file.tar | wallet-export validate /dev/stdin`)
27+
const inputStream: Readable =
28+
filePath === '/dev/stdin' ? process.stdin : fs.createReadStream(filePath)
29+
30+
// Validate the archive
31+
validateExportStream(inputStream)
32+
.then(({ valid, errors }) => {
33+
if (values.output === 'json') {
34+
// Output errors as JSON
35+
console.log(JSON.stringify({ valid, errors }, null, 2))
36+
} else {
37+
// Output errors to stdio
38+
if (valid) {
39+
console.log('✅ Export is valid.')
40+
} else {
41+
console.error('❌ Export is invalid:')
42+
errors.forEach((error) => {
43+
console.error(`- ${error}`)
44+
})
45+
}
46+
}
47+
48+
// Exit with appropriate code
49+
if (!valid) {
50+
process.exit(1)
51+
}
52+
})
53+
.catch((error) => {
54+
console.error('An error occurred:', error)
55+
process.exit(1)
56+
})

0 commit comments

Comments
 (0)
Please sign in to comment.