|
| 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