Skip to content

Commit

Permalink
Add CLI support for wallet-export with validation command and output …
Browse files Browse the repository at this point in the history
…options
  • Loading branch information
0marSalah committed Jan 29, 2025
1 parent 8fa25f5 commit f575f7d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"name": "@interop/wallet-export-ts",
"description": "A Javascript/Typescript library for exporting Universal Wallet Backup Containers.",
"version": "0.1.6",
"bin": {
"wallet-export": "./dist/cli.js"
},
"scripts": {
"build": "npm run clear && tsc -p tsconfig.json && ./build-dist.sh",
"clear": "rimraf dist/*",
"clear": "npx rimraf dist/*",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"prepare": "npm run build",
Expand Down Expand Up @@ -63,7 +66,7 @@
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"prettier": "^3.1.0",
"rimraf": "^5.0.5",
"rimraf": "^5.0.10",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
Expand Down
56 changes: 56 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node

import { parseArgs } from 'node:util'
import fs from 'node:fs'
import { type Readable } from 'node:stream'
import { validateExportStream } from './verify.js'

// Parse command-line arguments
const { values, positionals } = parseArgs({
options: {
output: {
type: 'string',
short: 'o'
}
},
allowPositionals: true
})

const [command, filePath] = positionals

if (command !== 'validate') {
console.error('Usage: wallet-export validate <path-to-export.tsr>')
process.exit(1)
}

// Handle stdin (e.g., `cat file.tar | wallet-export validate /dev/stdin`)
const inputStream: Readable =
filePath === '/dev/stdin' ? process.stdin : fs.createReadStream(filePath)

// Validate the archive
validateExportStream(inputStream)
.then(({ valid, errors }) => {
if (values.output === 'json') {
// Output errors as JSON
console.log(JSON.stringify({ valid, errors }, null, 2))
} else {
// Output errors to stdio
if (valid) {
console.log('✅ Export is valid.')
} else {
console.error('❌ Export is invalid:')
errors.forEach((error) => {
console.error(`- ${error}`)
})
}
}

// Exit with appropriate code
if (!valid) {
process.exit(1)
}
})
.catch((error) => {
console.error('An error occurred:', error)
process.exit(1)
})

0 comments on commit f575f7d

Please sign in to comment.