-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·82 lines (74 loc) · 1.93 KB
/
index.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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const yargs = require("yargs");
const chalk = require("chalk");
const boxen = require("boxen");
const { genData, genMethods } = require("./gen_core");
const usage = chalk.keyword("violet")(
"\nUsage: openapi-to-ts-client [input] [options]\n" +
boxen(chalk.green("Convert OpenAPI definition to typescript client"), {
padding: 1,
borderColor: "green",
dimBorder: true,
}) +
"\n"
);
const options = yargs
.usage(usage)
.options("output", {
alias: "o",
describe: "Output directory",
default: "./",
type: "string",
})
.options("client", {
alias: "c",
describe: "Client filename",
default: "api_client.ts",
type: "string",
})
.options("contracts", {
alias: "t",
describe: "Contracts filename",
default: "data_contracts.ts",
type: "string",
})
.options("burl", {
alias: "b",
describe: "Base URL name",
default: "API_BASE_URL",
type: "string",
})
.options("fetch", {
alias: "f",
describe: "Fetch wrapper filename",
default: "fetch_wrapper.ts",
type: "string",
})
.help(true);
const input = yargs.argv._[0];
if (!input) {
options
.getHelp()
.then(console.log)
.finally(() => process.exit(1));
} else {
const swaggerDocument = JSON.parse(
fs.readFileSync(path.resolve(input), "utf8")
);
const gMethods = genMethods(
swaggerDocument,
// Strip the extension from the input file
options.argv.contracts.replace(/\.[^/.]+$/, ""),
options.argv.fetch.replace(/\.[^/.]+$/, ""),
options.argv.burl
);
const gData = genData(swaggerDocument);
if (!fs.existsSync(options.argv.output)) {
fs.mkdirSync(options.argv.output, { recursive: true });
}
fs.writeFileSync(path.join(options.argv.output, options.argv.client), gMethods);
fs.writeFileSync(path.join(options.argv.output, options.argv.contracts), gData);
}