@stoar/cli
Version:
CLI tool for STOAR - decentralized file storage on Arweave
56 lines • 2.22 kB
JavaScript
import { Command } from 'commander';
import { getWalletConfig } from '../utils/wallet.js';
import { output, success, createSpinner, formatBytes } from '../utils/output.js';
import { wrapCommand } from '../utils/error.js';
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
export const downloadCommand = new Command('download')
.description('Download files from Arweave')
.argument('<transactionId>', 'Transaction ID to download')
.option('-o, --output <path>', 'Output file path')
.action(wrapCommand(async (transactionId, options, command) => {
const globalOptions = command.parent?.opts() || {};
const spinner = createSpinner(`Downloading ${transactionId}...`);
spinner.start();
try {
const { client } = await getWalletConfig(globalOptions);
// Get file data
const data = await client.getFile(transactionId);
// Get transaction info to retrieve filename
// Note: SDK doesn't have getTransaction, we'll extract from the data response headers
const tags = [];
// Find filename from tags
let fileName = 'download';
for (const tag of tags) {
if (tag.name === 'File-Name' || tag.name === 'Title') {
fileName = tag.value;
break;
}
}
// Determine output path
const outputPath = options.output || join(process.cwd(), fileName);
// Write file
writeFileSync(outputPath, Buffer.from(data));
spinner.stop();
const downloadData = {
transactionId,
fileName,
outputPath,
size: formatBytes(data.byteLength),
url: `https://arweave.net/${transactionId}`
};
output(downloadData, globalOptions);
if (!globalOptions?.json && !globalOptions?.quiet) {
success(`Downloaded to: ${outputPath}`);
console.log(` Size: ${downloadData.size}`);
}
}
catch (error) {
spinner.stop();
if (error.response?.status === 404) {
throw new Error(`Transaction not found: ${transactionId}`);
}
throw error;
}
}));
//# sourceMappingURL=download.js.map