-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.generator.js
78 lines (59 loc) · 2.16 KB
/
swagger.generator.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
const download = require('node-wget');
const fs = require('fs');
const exec = require('child_process').exec;
const findFilesByGlob = require('glob').sync;
const slash = require('slash');
const generatedDirectory = `./src/app/services/generated`;
const jsonUrl = 'https://bot-shop-api-production.herokuapp.com/swagger/v1/swagger.json';
const generatorUrl = 'https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/3.3.4/openapi-generator-cli-3.3.4.jar';
const jsonFilename = 'swagger.json';
const generatorFilename = 'generator.jar';
const configFilename = 'open-api-config.json';
const pathToJson = `${generatedDirectory}/${jsonFilename}`;
const pathToGenerator = `${generatedDirectory}/${generatorFilename}`;
const allTsFilesPattern = `${generatedDirectory}/**/*.ts`;
const uselessFiles = [
'api/api.ts',
'model/models.ts',
'api.module.ts',
'git_push.sh',
'index.ts',
'README.md',
'.openapi-generator-ignore',
'.gitignore',
].map(filename => `${generatedDirectory}/${filename}`);
const generateCommand = `java -jar ${pathToGenerator} generate -g typescript-angular -c ${configFilename} -i ${pathToJson} -o ${generatedDirectory} --skip-validate-spec`;
const logSdt = (stdout, stdError) => {
console.log(stdout);
console.warn(stdError);
};
const downloadAsync = (url, filename) => new Promise(resolve =>
download({ url, dest: filename }, data => {
console.log(data);
resolve();
}),
);
const removeFile = filePath => fs.unlinkSync(slash(filePath));
const executeCommand = command => new Promise(
(resolve, reject) => exec(command, (error, stdout, stderr) => {
logSdt(stdout, stderr);
error ? reject() : resolve();
}),
);
const generate = async () => {
const allTsFiles = findFilesByGlob(allTsFilesPattern);
allTsFiles.forEach(removeFile);
if (!fs.existsSync(generatedDirectory)) {
fs.mkdirSync(generatedDirectory);
}
if (fs.existsSync(pathToJson)) {
removeFile(pathToJson);
}
await downloadAsync(jsonUrl, pathToJson);
if (!fs.existsSync(pathToGenerator)) {
await downloadAsync(generatorUrl, pathToGenerator);
}
await executeCommand(generateCommand);
uselessFiles.forEach(removeFile);
};
generate();