|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import {ConverterManager, Exception, Logger, version as libVersion} from 'retroload-lib'; |
| 4 | +import {Command} from 'commander'; |
| 5 | +import {version as cliVersion} from './version.js'; |
| 6 | +import {readFile, writeFile} from './Utils.js'; |
| 7 | + |
| 8 | +main() |
| 9 | + .catch((err) => { |
| 10 | + Logger.error(err as string); |
| 11 | + }); |
| 12 | + |
| 13 | +async function main() { |
| 14 | + // const formatNames = AdapterManager.getAllAdapters().map((a) => a.internalName); |
| 15 | + // formatNames.sort(); |
| 16 | + const program = (new Command()) |
| 17 | + .name('retroload-convert') |
| 18 | + .description('Convert raw data into tape archive formats. (EXPERIMENTAL!)') |
| 19 | + .argument('infile', 'Path to file to convert') |
| 20 | + .allowExcessArguments(false) |
| 21 | + .option('-o <outfile>', 'Path to file to write') |
| 22 | + .option('-f <format>', 'Name of format to create') |
| 23 | + .option('-l, --loglevel <loglevel>', 'Verbosity of log output', '1') |
| 24 | + .version(`retroload: ${cliVersion}\nretroload-lib: ${libVersion}`) |
| 25 | + .showHelpAfterError(); |
| 26 | + // Options defined in adapters/encoders |
| 27 | + |
| 28 | + /* |
| 29 | + const allOptions = AdapterManager.getAllOptions(); |
| 30 | + allOptions.sort((a, b) => a.common && !b.common ? -1 : 0); |
| 31 | + for (const option of allOptions) { |
| 32 | + program.addOption(new Option(getCommanderFlagsString(option), option.description).hideHelp()); |
| 33 | + } |
| 34 | + */ |
| 35 | + program.parse(); |
| 36 | + |
| 37 | + const options = program.opts(); |
| 38 | + const infile = program.args[0]; |
| 39 | + const outfile = options['o'] as string; // TODO: fix type stuff; let converter propose new filename ((input.bin).cas)? |
| 40 | + Logger.setVerbosity(parseInt(typeof options['loglevel'] === 'string' ? options['loglevel'] : '1', 10)); |
| 41 | + const inputBa = readFile(infile); |
| 42 | + const format: string = options['f'] as string; // TODO: fix type stuff |
| 43 | + |
| 44 | + Logger.debug(`Processing ${infile}...`); |
| 45 | + |
| 46 | + try { |
| 47 | + const outputBa = ConverterManager.convert(inputBa, format, options); |
| 48 | + writeFile(outfile, outputBa); |
| 49 | + } catch (e) { |
| 50 | + if (e instanceof Exception.UsageError) { |
| 51 | + Logger.error(e.message); |
| 52 | + process.exit(1); |
| 53 | + } else { |
| 54 | + throw e; // show full stack trace for unexpected errors |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | +function getCommanderFlagsString(optionDefinition: PublicOptionDefinition) { |
| 61 | + return optionDefinition.type !== 'text' || optionDefinition.argument === undefined ? `--${optionDefinition.name}` : `--${optionDefinition.name} <${optionDefinition.argument}>`; |
| 62 | +} |
| 63 | +*/ |
0 commit comments