Skip to content

Commit

Permalink
feat: add output flag (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Feb 18, 2022
1 parent ad6acbd commit 2774fb8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:'));
Expand All @@ -20,11 +30,9 @@ const showErrorAndExit = err => {
program
.version(packageInfo.version)
.arguments('<document> [version]')
.action((asyncAPIPath, v) => {
asyncapiFile = path.resolve(asyncAPIPath);
version = v;
})
.action(parseArguments)
.option('--id <id>', 'application id (defaults to a generated one)')
.option('-o, --output <outputFile>', 'file where to put the converted AsyncAPI document', parseOutput)
.parse(process.argv);

if (!asyncapiFile) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 2774fb8

Please sign in to comment.