-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.mjs
83 lines (73 loc) · 1.88 KB
/
generate.mjs
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
import {exec, execSync} from "node:child_process"
import path from "node:path"
const templates = execSync(
"find ./integration-tests -mindepth 1 -maxdepth 1 -type d",
)
.toString("utf-8")
.split("\n")
.map((it) => it.trim())
.filter(Boolean)
const definitions = execSync("find ./integration-tests-definitions -type f")
.toString("utf-8")
.split("\n")
.map((it) => it.trim())
.filter(Boolean)
Promise.all(
templates.flatMap((templatePath) =>
definitions.map((definition) => runSingle(templatePath, definition)),
),
)
.then(() => {
console.log("success!")
process.exit(0)
})
.catch((err) => {
console.error(err)
process.exit(1)
})
async function runSingle(templatePath, input) {
const inputType = input.endsWith(".tsp") ? "typespec" : "openapi3"
const filename = path.basename(input)
const template = path.basename(templatePath)
const args = [
`--input="${input}"`,
`--input-type=${inputType}`,
`--output="integration-tests/${template}/src/generated/${filename}"`,
`--template="${template}"`,
"--schema-builder=zod",
]
try {
const result = await runCmd(
`node ./packages/openapi-code-generator/dist/cli.js ${args.join(" ")}`,
)
for (const it of result) {
console.info(`[${template} - ${filename}] ${it}`)
}
} catch (err) {
console.error(err)
for (const it of err.output) {
console.error(`[${template} - ${filename}] ${it}`)
}
throw err
}
}
function runCmd(cmd) {
return new Promise((resolve, reject) => {
const output = []
console.info(cmd)
const child = exec(cmd, (err, stdout, stderr) => {
if (err) {
err.output = output
reject(err)
return
}
resolve(output)
})
child.stdout.on("data", (it) => {
output.push(it.trim())
})
child.stderr.on("data", (it) => {
output.push(it.trim())
})
})
}