Repair command-line handling

This commit is contained in:
Tony Garnock-Jones 2022-01-22 23:20:35 +01:00
parent 3d1b151462
commit a98aa357d4
1 changed files with 9 additions and 7 deletions

View File

@ -12,8 +12,7 @@ import { changeExt, Diagnostic, expandInputGlob, formatFailures } from './cli-ut
export type CommandLineArguments = {
inputs: string[];
xrefs: string[];
base: string | undefined;
output: string;
output: string | undefined;
stdout: boolean;
core: string;
watch: boolean;
@ -116,7 +115,7 @@ export function runOnce(options: CommandLineArguments): CompilationResult {
const inputFiles: Array<TranslatedFile> = inputFiles0.map(i => {
const { inputBaseDir, inputFilePath, baseRelPath, modulePath, schema } = i;
const outputFilePath = path.join(options.output, changeExt(baseRelPath, '.ts'));
const outputFilePath = path.join(options.output ?? '.', changeExt(baseRelPath, '.ts'));
return { inputBaseDir, inputFilePath, outputFilePath, schemaPath: modulePath, schema };
});
@ -174,14 +173,13 @@ export function main(argv: Array<string>) {
new Command()
.arguments('[input...]')
.description('Compile Preserves schema definitions to TypeScript', {
input: 'Input filename or glob',
input: 'Input directory, optionally with :<glob> on the end',
})
.option('--xref <glob>', 'Cross-reference other textual Preserves schema definitions',
(glob, prev: string[]) => [... prev, glob], [])
.option('--output <directory>', 'Output directory for modules (default: next to sources)')
.option('--output <directory>', 'Output directory for modules')
.option('--stdout', 'Prints each module to stdout one after the other instead ' +
'of writing them to files in the `--output` directory')
.option('--base <directory>', 'Base directory for sources (default: common prefix)')
.option('--core <path>', 'Import path for @preserves/core', '@preserves/core')
.option('--watch', 'Watch base directory for changes')
.option('--traceback', 'Include stack traces in compiler errors')
@ -189,10 +187,14 @@ export function main(argv: Array<string>) {
(nsPath: string, previous: string[]): string[] => [... previous, nsPath],
[])
.action((inputs: string[], rawOptions) => {
if ((rawOptions.output === void 0 && !rawOptions.stdout) ||
(rawOptions.output !== void 0 && rawOptions.stdout))
{
throw new Error("Either --output or --stdout (but not both) must be supplied.");
}
const options: CommandLineArguments = {
inputs: inputs.map(i => path.normalize(i)),
xrefs: rawOptions.xref.map((x: string) => path.normalize(x)),
base: rawOptions.base,
output: rawOptions.output,
stdout: rawOptions.stdout,
core: rawOptions.core,