forked from asyncapi/generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·87 lines (73 loc) · 2.8 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env node
const path = require('path');
const os = require('os');
const program = require('commander');
const packageInfo = require('./package.json');
const mkdirp = require('mkdirp');
const Generator = require('./lib/generator');
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;
let template;
const params = {};
const noOverwriteGlobs = [];
const disabledHooks = [];
const parseOutput = dir => path.resolve(dir);
const paramParser = v => {
if (!v.includes('=')) throw new Error(`Invalid param ${v}. It must be in the format of --param name=value.`);
const chunks = v.split(/=(.+)/, 2);
const paramName = chunks[0];
const paramValue = chunks[1];
params[paramName] = paramValue;
return v;
};
const noOverwriteParser = v => noOverwriteGlobs.push(v);
const disableHooksParser = v => disabledHooks.push(v);
const showErrorAndExit = err => {
console.error(red('Something went wrong:'));
console.error(red(err.stack || err.message));
if (err.errors) {
console.error(red(JSON.stringify(err.errors)));
}
process.exit(1);
};
program
.version(packageInfo.version)
.arguments('<asyncapi> <template>')
.action((asyncAPIPath, tmpl) => {
asyncapiFile = path.resolve(asyncAPIPath);
template = tmpl;
})
.option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
.option('-d, --disable-hook <hookName>', 'disable a specific hook', disableHooksParser)
.option('-n, --no-overwrite <glob>', 'glob or path of the file(s) to skip when regenerating', noOverwriteParser)
.option('-p, --param <name=value>', 'additional param to pass to templates', paramParser)
.option('-t, --templates <templateDir>', 'directory where templates are located (defaults to internal templates directory)', null, path.resolve(__dirname, '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 showErrorAndExit(err);
let generator;
try {
generator = new Generator(template, program.output || path.resolve(os.tmpdir(), 'asyncapi-generator'), {
templatesDir: program.templates,
templateParams: params,
noOverwriteGlobs,
disabledHooks,
});
} catch (e) {
return showErrorAndExit(e);
}
generator.generateFromFile(asyncapiFile)
.then(() => {
console.log(green('Done! ✨'));
console.log(yellow('Check out your shiny new generated files at ') + magenta(program.output) + yellow('.'));
})
.catch(showErrorAndExit);
});
process.on('unhandledRejection', showErrorAndExit);