-
Notifications
You must be signed in to change notification settings - Fork 14
/
generate-client.js
99 lines (83 loc) · 2.3 KB
/
generate-client.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
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
const arg = require("arg");
const { exec } = require("child_process");
const { log, error } = console;
// by default, run this script against the server
const DEFAULT_URL = "http://localhost:3030/api/v3/docs-json/";
const DEFAULT_PATH = "src/serverApi/v3";
const args = arg(
{
"--help": Boolean,
"-h": "--help",
"--url": String,
"-u": "--url",
"--path": String,
"-p": "--path",
"--config": String,
"-c": "--config",
},
{
argv: process.argv.slice(2),
}
);
if ("--help" in args) {
log(`Usage: node generate-client.js [opts]
OPTIONS:
--help (-h) Show this help.
--path (-p) Path to the newly created client's directory.
default: ${DEFAULT_PATH}
--url (-u) URL/path to the spec file in yml/json format.
default: ${DEFAULT_URL}
--config (-c) path to the additional-properties config file in yml/json format
`);
process.exit(0);
}
const params = {
/** url to load the open-api definition from */
url: args._[0] || args["--url"] || DEFAULT_URL,
/** folder to save the open-api client */
path: args._[1] || args["--path"] || DEFAULT_PATH,
config: args._[2] || args["--config"] || "",
};
const errorMessageContains = (includedString, error) => {
return (
error &&
error.message &&
typeof error.message === "string" &&
error.message.includes(includedString)
);
};
const generateClient = () => {
const cmd = getOpenApiCommand(params);
log(
`Try updating the openapi client in the folder ${params.path} from ${params.url} ...`
);
asyncExec(cmd)
.then((stdout) => log(stdout))
.catch((stderr) => {
if (errorMessageContains("ConnectException", stderr)) {
error(
`Failed to connect to ${params.url}, is the server started at this url?`
);
} else error(stderr.message);
});
};
const getOpenApiCommand = (params) => {
const { url, path, config } = params;
const configFile = config ? `-c ${config}` : "";
const command = `openapi-generator-cli generate -i ${url} -g typescript-axios -o ${path} ${configFile} --skip-validate-spec`;
return command;
};
const asyncExec = (command) =>
new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(error);
}
return resolve(stdout || stderr);
});
});
const main = () => {
generateClient();
};
main();