From 306c7c2cae3c9855aa68347bb3ae01e7eb535c25 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 17 Mar 2021 16:14:45 +0100 Subject: [PATCH] Support writing to stdout --- .../schema/src/bin/preserves-schema-ts.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/implementations/javascript/packages/schema/src/bin/preserves-schema-ts.ts b/implementations/javascript/packages/schema/src/bin/preserves-schema-ts.ts index 88bf4ba..53b4c03 100644 --- a/implementations/javascript/packages/schema/src/bin/preserves-schema-ts.ts +++ b/implementations/javascript/packages/schema/src/bin/preserves-schema-ts.ts @@ -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) { }) .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',