Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add output flag #100

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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