Support writing to stdout

This commit is contained in:
Tony Garnock-Jones 2021-03-17 16:14:45 +01:00
parent bacf310648
commit 306c7c2cae
1 changed files with 21 additions and 3 deletions

View File

@ -13,6 +13,7 @@ export type CommandLineArguments = {
input: string;
base: string | undefined;
output: string | undefined;
stdout: boolean;
core: string;
watch: boolean;
traceback: boolean;
@ -167,15 +168,26 @@ export function runOnce(options: CommandLineArguments): CompilationResult {
})),
];
fs.mkdirSync(path.dirname(c.outputFilePath), { recursive: true });
let compiledModule;
try {
fs.writeFileSync(c.outputFilePath, compile(env, c.schema, {
compiledModule = compile(env, c.schema, {
preservesModule: options.core,
warn: (message: string, pos: Position | null) =>
failures.push({ type: 'warn', file: c.inputFilePath, detail: { message, pos } }),
}), 'utf-8');
});
} catch (e) {
failures.push({ type: 'error', file: c.inputFilePath, detail: e });
}
if (compiledModule !== void 0) {
if (options.stdout) {
console.log('////------------------------------------------------------------');
console.log('//// ' + c.outputFilePath);
console.log();
console.log(compiledModule);
} else {
fs.writeFileSync(c.outputFilePath, compiledModule, 'utf-8');
}
}
});
for (const d of failures) {
@ -208,7 +220,13 @@ export function main(argv: Array<string>) {
})
.option('output', {
type: 'string',
description: 'Output directory for sources (default: next to sources)',
description: 'Output directory for modules (default: next to sources)',
})
.option('stdout', {
type: 'boolean',
description: 'Prints each module to stdout one after the other instead ' +
'of writing them to files in the `--output` directory',
default: false,
})
.option('base', {
type: 'string',