This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
executable file
·47 lines (37 loc) · 1.5 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env node
const path = require('path');
const program = require('commander');
const mkdirp = require('mkdirp');
const packageInfo = require('./package.json');
const generate = require('./lib/codegen').process;
const red = text => `\x1b[31m${text}\x1b[0m`;
const magenta = text => `\x1b[35m${text}\x1b[0m`;
const yellow = text => `\x1b[33m${text}\x1b[0m`;
const green = text => `\x1b[32m${text}\x1b[0m`;
let asyncAPIFile;
const parseOutput = dir => path.resolve(dir);
const showError = err => {
console.error(red('Something went wrong:'));
console.error(red(err.stack || err.message));
};
program
.version(packageInfo.version)
.arguments('<asyncAPI>')
.action((asyncAPIFilePath) => {
asyncAPIFile = path.resolve(asyncAPIFilePath);
})
.option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
.option('-t, --templates <templateDir>', 'directory where templates are located (defaults to internal nodejs templates)')
.parse(process.argv);
if (!asyncAPIFile) {
console.error(red('> Path to AsyncAPI file not provided.'));
program.help(); // This exits the process
}
mkdirp(program.output, err => {
if (err) return showError(err);
generate(asyncAPIFile, program.output, program.templates).then(() => {
console.log(green('Done! ✨'));
console.log(yellow('Check out your shiny new API at ') + magenta(program.output) + yellow('.'));
}).catch(showError);
});
process.on('unhandledRejection', showError);