-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (92 loc) · 2.47 KB
/
Copy pathindex.js
File metadata and controls
101 lines (92 loc) · 2.47 KB
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
100
101
const { spawn } = require("node:child_process");
const fs = require("node:fs/promises");
const path = require("node:path");
const { rimraf } = require("rimraf");
const tmp = require("tmp-promise");
exports.IMAGE_TAG = "v6.6.0";
exports.DEFAULT_CONFIG = {
npmName: "temp",
npmVersion: "1.0.0",
snapshot: false,
supportsES6: true,
withInterfaces: false,
withoutPrefixEnums: false,
allowUnicodeIdentifiers: true,
legacyDiscriminatorBehavior: false,
nullSafeAdditionalProps: true,
withSeparateModelsAndApi: false,
useSingleRequestParameter: false,
disallowAdditionalPropertiesIfNotPresent: true,
stringEnums: false,
typescriptThreePlus: true
};
exports.generate = async function (
openapiPath,
outputDir,
imageTag = exports.IMAGE_TAG,
config = exports.DEFAULT_CONFIG
) {
if (!openapiPath) {
throw new Error("No openapiPath provided");
}
if (!outputDir) {
throw new Error("No outputDir provided");
}
openapiPath = path.resolve(openapiPath);
outputDir = path.resolve(outputDir);
console.log("Generating from", openapiPath, "to", outputDir);
// generate output
const tmpdir = await tmp.dir();
const configDir = await tmp.dir();
await fs.writeFile(path.join(configDir.path, "generate-config.json"), JSON.stringify(config), "utf8");
await new Promise((res, rej) => {
const proc = spawn(
"docker",
[
"run",
"--rm",
"-v",
`${configDir.path}:/openapi-gen-config`,
"-v",
`${path.dirname(openapiPath)}:/input`,
"-v",
`${tmpdir.path}:/output`,
`openapitools/openapi-generator-cli:${imageTag}`,
"generate",
"-g",
"typescript-axios",
"-c",
"/openapi-gen-config/generate-config.json",
"-i",
`/input/${path.basename(openapiPath)}`,
"-o",
"/output",
"--skip-validate-spec",
"--type-mappings",
"object=any"
],
{
cwd: __dirname,
stdio: "inherit"
}
);
proc.on("error", (err) => {
if (err) rej(err);
});
proc.on("exit", (code) => {
if (code === 0) {
res(outputDir);
} else {
rej(new Error("Failed with exit-code=" + code));
}
});
});
// copy to output dir
await rimraf(outputDir);
await fs.mkdir(outputDir);
for (const file of await fs.readdir(tmpdir.path)) {
if (path.extname(file) === ".ts") {
await fs.cp(path.join(tmpdir.path, file), path.join(outputDir, file));
}
}
};