dashboard/tools/swagger-codegen.ts

86 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-08-12 13:34:18 +00:00
import * as fs from 'fs';
import { exec } from 'child_process';
import * as path from 'path';
import * as config from '../swagger-codegen-config.json';
const ROOT_DIR = path.join(__dirname, '..');
function deleteFolderRecursive(deletedDir: string) {
if (fs.existsSync(deletedDir)) {
fs.readdirSync(deletedDir).forEach(file => {
const currentPath = path.join(deletedDir, file);
fs.lstatSync(currentPath).isDirectory() ? deleteFolderRecursive(currentPath) : fs.unlinkSync(currentPath);
});
fs.rmdirSync(deletedDir);
}
}
2019-12-10 15:14:50 +00:00
function execWithLog(cmd: string) {
2019-08-12 13:34:18 +00:00
return new Promise((res, rej) =>
exec(
cmd,
{
cwd: ROOT_DIR
},
(error, stdout, stderr) => {
if (error === null) {
console.log(stderr);
res(stdout);
} else {
console.error(error);
console.error(stderr);
rej(error);
}
}
)
);
}
2019-12-10 15:14:50 +00:00
function swaggerCodegenAngular(cliPath: string, inputPath: string, outputDirPath: string) {
deleteFolderRecursive(outputDirPath);
console.log(`${outputDirPath} deleted`);
const cmd = `java -jar ${cliPath} generate -l typescript-angular --additional-properties ngVersion=7 -i ${inputPath} -o ${outputDirPath}`;
console.log(`> ${cmd}`);
return execWithLog(cmd);
}
async function swaggerCodegenAngularCli({
name,
codegenPath,
swags,
outputDir
}: {
name: string;
codegenPath: string;
swags: { [name: string]: string };
outputDir: string;
}) {
const log = (text: string) => console.log(`[${name}]: ${text}`);
log('generate...');
2019-08-12 13:34:18 +00:00
await Promise.all(
2019-12-10 15:14:50 +00:00
Object.entries(swags).map(([swagName, swagPath]) =>
swaggerCodegenAngular(codegenPath, swagPath, path.join(config.outputRootDir, swagName, outputDir))
2019-08-12 13:34:18 +00:00
)
);
2019-12-10 15:14:50 +00:00
log('generated');
}
(async () => {
const { outputDir } = config;
await Promise.all([
swaggerCodegenAngularCli({
name: 'Swagger Codegen 3',
2019-12-12 12:07:38 +00:00
codegenPath: path.join(config.rootDir, config.cli3),
2019-12-10 15:14:50 +00:00
swags: config.schemes3,
outputDir
}),
swaggerCodegenAngularCli({
name: 'Swagger Codegen 2',
2019-12-12 12:07:38 +00:00
codegenPath: path.join(config.rootDir, config.cli),
2019-12-10 15:14:50 +00:00
swags: config.schemes,
outputDir
})
]);
2019-08-12 13:34:18 +00:00
})();