62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
import yargs from 'yargs/yargs';
|
|
import { canonicalEncode, KeyedDictionary, underlying } from '@preserves/core';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fromSchema, fromBundle } from '../meta';
|
|
import { expandInputGlob, formatFailures } from './cli-utils';
|
|
|
|
export type CommandLineArguments = {
|
|
input: string;
|
|
base: string | undefined;
|
|
bundle: boolean;
|
|
};
|
|
|
|
export function run(options: CommandLineArguments): void {
|
|
const { failures, inputFiles } = expandInputGlob(options.input, options.base);
|
|
|
|
if (!options.bundle && inputFiles.length !== 1) {
|
|
failures.push({ type: 'error', file: null, detail: {
|
|
message: 'Cannot emit non-bundle with anything other than exactly one input file',
|
|
pos: null,
|
|
}});
|
|
}
|
|
|
|
formatFailures(failures);
|
|
|
|
if (failures.length === 0) {
|
|
if (options.bundle) {
|
|
fs.writeSync(1, underlying(canonicalEncode(fromBundle({
|
|
modules: new KeyedDictionary(inputFiles.map(i => [i.modulePath, i.schema])),
|
|
}))));
|
|
} else {
|
|
fs.writeSync(1, underlying(canonicalEncode(fromSchema(inputFiles[0].schema))));
|
|
}
|
|
}
|
|
}
|
|
|
|
export function main(argv: Array<string>) {
|
|
const options: CommandLineArguments = yargs(argv)
|
|
.command('$0 <input>',
|
|
'Compile textual Preserves schema definitions to binary format',
|
|
yargs => yargs
|
|
.positional('input', {
|
|
type: 'string',
|
|
description: 'Input filename or glob',
|
|
demandOption: true,
|
|
})
|
|
.option('bundle', {
|
|
type: 'boolean',
|
|
description: 'Determines whether to emit a schema Bundle or a lone Schema',
|
|
default: true,
|
|
})
|
|
.option('base', {
|
|
type: 'string',
|
|
description: 'Base directory for sources (default: common prefix)',
|
|
}),
|
|
argv => argv)
|
|
.argv;
|
|
options.input = path.normalize(options.input);
|
|
Error.stackTraceLimit = Infinity;
|
|
run(options);
|
|
}
|