-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·46 lines (36 loc) · 1.38 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
#!/usr/bin/env node
import path from 'path';
import fs from 'fs';
import program from 'commander';
const require = (filepath, encoding = "utf8") => JSON.parse(fs.readFileSync(filepath, { encoding }));
const packageInfo = require('./package.json');
import {codegen} from './lib/codegen.js';
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 swaggerFile;
const parseOutput = dir => path.resolve(dir);
program
.version(packageInfo.version)
.arguments('<swaggerFile>')
.action((swaggerFilePath) => {
swaggerFile = path.resolve(swaggerFilePath);
})
.option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
.parse(process.argv);
if (!swaggerFile) {
console.error(red('> Path to Swagger file not provided.'));
program.help(); // This exits the process
}
codegen({
swagger: swaggerFile,
target_dir: program.output,
}).then(() => {
console.log(green('Done! ✨'));
console.log(yellow('Check out your shiny new API at ') + magenta(program.output) + yellow('.'));
}).catch(err => {
console.error(red('Aaww 💩. Something went wrong:'));
console.error(red(err.stack || err.message));
});
process.on('unhandledRejection', (err) => console.error(err));