diff --git a/README.md b/README.md index 739a88a9..341f4d1a 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,18 @@ id: 'urn:com.asynapi.streetlights' ... ``` -Save the result in a file: +Save the result in a file by stream: ```sh asyncapi-converter streetlights.yml > streetlights2.yml ``` +Save the result in a file by `-o, --output` flag: + +```sh +asyncapi-converter streetlights.yml -o streetlights2.yml +``` + ### As a package ```js diff --git a/cli.js b/cli.js index f6f22a8f..acc3ea4d 100755 --- a/cli.js +++ b/cli.js @@ -10,6 +10,16 @@ const red = text => `\x1b[31m${text}\x1b[0m`; let asyncapiFile; let version; +let output; + +const parseArguments = (asyncAPIPath, v) => { + asyncapiFile = path.resolve(asyncAPIPath); + version = v; +} + +const parseOutput = (filePath) => { + output = path.resolve(filePath); +} const showErrorAndExit = err => { console.error(red('Something went wrong:')); @@ -20,11 +30,9 @@ const showErrorAndExit = err => { program .version(packageInfo.version) .arguments(' [version]') - .action((asyncAPIPath, v) => { - asyncapiFile = path.resolve(asyncAPIPath); - version = v; - }) + .action(parseArguments) .option('--id ', 'application id (defaults to a generated one)') + .option('-o, --output ', 'file where to put the converted AsyncAPI document', parseOutput) .parse(process.argv); if (!asyncapiFile) { @@ -46,7 +54,11 @@ try { converted = JSON.stringify(converted, undefined, 2); } - console.log(converted); + if (output) { + fs.writeFileSync(output, converted, 'utf-8'); + } else { + console.log(converted); + } } catch (e) { showErrorAndExit(e); }