-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
155 lines (143 loc) · 7.44 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
const yargs = require("yargs");
const fetch = require("node-fetch")
const services = ['checkout', 'checkout utility', 'payments', 'recurring', 'payouts', 'platforms account', 'platforms fund', 'platforms notification configuration', 'platforms notifications', 'platforms hosted onboarding page', 'binlookup', 'pos terminal management'];
const argv = yargs
.usage('Usage: $0 <command> [options]')
.command('generate', 'Generate API models from an OpenAPI spec')
.option('versionNumber', { alias: 'v', description: 'Version of the API to download. Does not work for local files. Use "latest" for latest version.', type: 'string' })
.option('files', { alias: 'f', description: 'Generate model from a local file', type: "array" })
.option('language', {
alias: 'l', description: 'Generate model for specified language', type: 'string',
choices: ['apex', 'csharp', 'go', 'go-experimental', 'java', 'javascript', 'php', 'python', 'typescript-node']
})
.option('packageName', { alias: 'p', description: 'Name of the API Service ' })
.option('templatesPath', { alias: 't', description: 'Use custom templates from given path' })
.option('services', {
alias: 's', description: 'Generate models for specified services only', type: "array",
choices: services
})
.option('modelsOnly', { alias: 'm', description: 'Generate only models (No API)', type: 'boolean' })
.option('output', { alias: 'o', description: 'Path where generated models are saved', type: 'string', default: './models' })
.demandOption(['l'])
.help()
.alias('help', 'h')
.argv;
const shell = require("shelljs");
const path = require("path");
const fs = require("fs");
const tmpFolder = "./tmp";
let baseUrl = "https://docs.adyen.com/api-explorer/json";
const repo = "https://api.github.com/repos/OpenAPITools/openapi-generator"
const latestReleaseUri = `${repo}/releases/latest`
shell.mkdir(tmpFolder);
const generateTemplate = (f, p = argv.packageName, o = argv.output) => {
const generateCommand = `node ./openapi-generator-cli/bin/openapi-generator generate --skip-validate-spec -i "${f}" ${argv.templatesPath ? `-t ${argv.templatesPath}` : ""} ${p ? `--package-name ${p}` : ""} -g ${argv.language} -o ${o}`
const command = argv.modelsOnly ? `export JAVA_OPTS='-Dmodels -DskipFormModel=true' && ${generateCommand}` : generateCommand
shell.exec(command, { silent: true });
}
const fetchServices = async () => {
const res = await fetch(`${baseUrl}/Services.json`)
return await res.json()
}
const downloadApiFiles = async (value, filePath) => {
const res = await fetch(`${baseUrl}/${value.reference}`)
const json = await res.json()
fs.writeFileSync(filePath, JSON.stringify(json, null, 4), { flag: 'w', encoding: 'utf8' }, function (err) {
if (err) {
return console.error(err)
}
})
}
const getLatestVersion = async () => {
const res = await fetch(latestReleaseUri)
const { tag_name: tagName } = await res.json()
return tagName.slice(1)
}
const hasLatestVersion = async (tagName) => {
if (fs.existsSync("./openapi-generator-cli")) {
if (fs.existsSync("./openapi-generator-cli/bin/openapi-generator.jar")) {
const { stdout, stderr } = shell.exec(`node ./openapi-generator-cli/bin/openapi-generator version`, { silent: true })
if (!stderr) {
return stdout.startsWith(tagName)
}
console.error(stderr)
shell.exit(1)
}
} else {
console.log("OpenAPI generator not found. Cloning repo and generating build...")
shell.exec(`git clone https://github.com/OpenAPITools/openapi-generator-cli.git openapi-generator-cli`, { silent: true })
shell.cd("./openapi-generator-cli")
shell.exec("chmod -R 777 .")
shell.rm("-rf", ".git")
shell.rm("-rf", "lib")
shell.cd("..")
console.log("✔️ Done")
}
return false
}
(async function run() {
const latestVer = await getLatestVersion()
const isLatest = await hasLatestVersion(latestVer)
if (!isLatest) {
console.log("Downloading latest version of the OpenAPI Generator JAR file... (This will happen only once for every new version)")
shell.rm("-rf", "./openapi-generator-cli/bin/openapi-generator-*.jar")
shell.cd("./openapi-generator-cli")
shell.exec(`curl https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/${latestVer}/openapi-generator-cli-${latestVer}.jar -o ./bin/openapi-generator.jar`, { silent: true })
shell.cd("..")
console.log("✔️ Done")
}
try {
if (argv.files) {
argv.files.forEach(f => {
console.log(`┌ ⚙️ Generating specs for file: ${f}`);
generateTemplate(f)
console.log("└ ✔️ Done");
})
} else {
const { services } = await fetchServices()
const filteredServices = argv.services ? Object.entries(services).filter(([name]) => argv.services.includes(name.toLowerCase())) : Object.entries(services);
console.log(`\n 💬 Models will be saved on ${path.resolve(__dirname, argv.output)} folder\n`)
for ([name, service] of filteredServices) {
const slicedName = name.split(" ").join("");
const formattedName = slicedName.charAt(0).toLowerCase() + slicedName.slice(1);
const serviceFolder = `${tmpFolder}/${formattedName}`;
shell.mkdir(serviceFolder);
const versions = Object.entries(service.versions);
const filteredVersions = argv.versionNumber ?
argv.versionNumber === "latest" ? [versions.pop()] : versions.filter(([key]) => key === argv.versionNumber)
: versions;
if (!filteredVersions.length) {
console.error('Error: API returned 0 results for given version number')
process.exit(1)
}
for ([index, [key, value]] of filteredVersions.entries()) {
const versionFolder = `${serviceFolder}/v${key}`;
const filePath = `${versionFolder}/${formattedName}.json`;
const output = `${argv.output}/${formattedName}/v${key}`
shell.mkdir(versionFolder);
console.log(` │ ┌ v${key} (${index + 1}/${versions.length})`);
console.log(` │ │ 🗜 Downloading OpenAPI file from ${baseUrl}/${value.reference}`);
try {
await downloadApiFiles(value, filePath)
shell.exec(`curl ${baseUrl}/${value.reference} -o "${filePath}"`);
console.log(` │ │ ⚙️ Generating specs for ${name} v${key}`);
generateTemplate(filePath, formattedName, output)
} catch (e) {
console.error(e)
} finally {
console.log(" │ └ ✔️ done");
}
};
console.log(` └─ Done!`);
};
}
} catch (e) {
console.error(e)
} finally {
// shell.exec("dtsgen --out types.d.ts ./tmp/**/*.json")
shell.rm("-rf", tmpFolder)
console.log(" \r");
console.log(" 🤘 All Done!");
}
})()