Skip to content

Commit

Permalink
Create retroload-convert CLI script
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanschramm committed Jan 1, 2024
1 parent 7db2c68 commit 7ba3e90
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
4 changes: 4 additions & 0 deletions retroload/bin/retroload-convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

import('../dist/esm/retroload-convert.js');

4 changes: 4 additions & 0 deletions retroload/bin/retroload-tokenize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

import('../dist/esm/retroload-tokenize.js');

4 changes: 0 additions & 4 deletions retroload/bin/retroload-tokenizer.js

This file was deleted.

5 changes: 3 additions & 2 deletions retroload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
},
"bin": {
"retroload": "./bin/retroload.js",
"retroload-tokenizer": "./bin/retroload-tokenizer.js",
"retroload-decode": "./bin/retroload-decode.js"
"retroload-convert": "./bin/retroload-convert.js",
"retroload-decode": "./bin/retroload-decode.js",
"retroload-tokenize": "./bin/retroload-tokenize.js"
},
"dependencies": {
"commander": "^11.1.0",
Expand Down
63 changes: 63 additions & 0 deletions retroload/src/retroload-convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node

import {ConverterManager, Exception, Logger, version as libVersion} from 'retroload-lib';
import {Command} from 'commander';
import {version as cliVersion} from './version.js';
import {readFile, writeFile} from './Utils.js';

main()
.catch((err) => {
Logger.error(err as string);
});

async function main() {
// const formatNames = AdapterManager.getAllAdapters().map((a) => a.internalName);
// formatNames.sort();
const program = (new Command())
.name('retroload-convert')
.description('Convert raw data into tape archive formats. (EXPERIMENTAL!)')
.argument('infile', 'Path to file to convert')
.allowExcessArguments(false)
.option('-o <outfile>', 'Path to file to write')
.option('-f <format>', 'Name of format to create')
.option('-l, --loglevel <loglevel>', 'Verbosity of log output', '1')
.version(`retroload: ${cliVersion}\nretroload-lib: ${libVersion}`)
.showHelpAfterError();
// Options defined in adapters/encoders

/*
const allOptions = AdapterManager.getAllOptions();
allOptions.sort((a, b) => a.common && !b.common ? -1 : 0);
for (const option of allOptions) {
program.addOption(new Option(getCommanderFlagsString(option), option.description).hideHelp());
}
*/
program.parse();

const options = program.opts();
const infile = program.args[0];
const outfile = options['o'] as string; // TODO: fix type stuff; let converter propose new filename ((input.bin).cas)?
Logger.setVerbosity(parseInt(typeof options['loglevel'] === 'string' ? options['loglevel'] : '1', 10));
const inputBa = readFile(infile);
const format: string = options['f'] as string; // TODO: fix type stuff

Logger.debug(`Processing ${infile}...`);

try {
const outputBa = ConverterManager.convert(inputBa, format, options);
writeFile(outfile, outputBa);
} catch (e) {
if (e instanceof Exception.UsageError) {
Logger.error(e.message);
process.exit(1);
} else {
throw e; // show full stack trace for unexpected errors
}
}
}

/*
function getCommanderFlagsString(optionDefinition: PublicOptionDefinition) {
return optionDefinition.type !== 'text' || optionDefinition.argument === undefined ? `--${optionDefinition.name}` : `--${optionDefinition.name} <${optionDefinition.argument}>`;
}
*/
2 changes: 1 addition & 1 deletion retroload/src/retroload-decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function main() {
// - visualize WAVE samples of section around error as ASCII graph when --on-error was set to 'stop'
const program = (new Command())
.name('retroload-decode')
.description('Decode WAVE files of historical computers.')
.description('Decode WAVE files of historical computers. (EXPERIMENTAL)')
.argument('<infile>', 'Path to WAVE file to decode')
.allowExcessArguments(false)
.option('-o <outpath>', 'Prefix (filename or complete path) for files to write', './')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {BasicTokenizers} from 'retroload-lib';

function main() {
const program = (new Command())
.name('retroload-tokenizer')
.description('Convert plain text BASIC files into tokenized BASIC files for loading.')
.name('retroload-tokenize')
.description('Convert plain text BASIC files into tokenized BASIC files for loading. (EXPERIMENTAL)')
.argument('<infile>', 'Path to source file to tokenize')
.option('-d, --dialect <basic dialect>', 'BASIC dialect to use')
.option('-o, --outfile <output file>', 'Where to write to binary output to. By default the input filename with a dialect specific extension is used as output filename.');
Expand Down

0 comments on commit 7ba3e90

Please sign in to comment.