dashboard/tools/swagger-codegen.ts

62 lines
2.1 KiB
TypeScript
Raw Normal View History

import * as del from 'del';
2019-08-12 13:34:18 +00:00
import * as path from 'path';
import * as shell from 'shelljs';
2019-08-12 13:34:18 +00:00
import * as config from '../swagger-codegen-config.json';
import { createLog } from './utils/create-log';
import { execWithLog } from './utils/exec-with-log';
2019-08-12 13:34:18 +00:00
type Schemes = { [name: string]: string };
2019-12-10 15:14:50 +00:00
async function replaceModuleWithProvidersNg10(outputDirPath: string) {
const modulePath = `${outputDirPath}/api.module.ts`;
return new Promise((resolve, reject) => {
try {
const result = shell.sed('-i', /ModuleWithProviders\s/, `ModuleWithProviders<ApiModule> `, modulePath);
// tslint:disable-next-line:no-console
console.log(`Replaced "ModuleWithProviders" in "${modulePath}"`);
resolve(result);
} catch (err) {
// tslint:disable-next-line:no-console
console.error(err);
console.error(`Error on path "${modulePath}"`);
reject(err);
}
});
}
2019-12-10 15:14:50 +00:00
async function swaggerCodegenAngularCli({
schemes,
2020-05-19 12:50:28 +00:00
outputDir,
outputRootDir,
cliPath,
2019-12-10 15:14:50 +00:00
}: {
schemes: Schemes;
2019-12-10 15:14:50 +00:00
outputDir: string;
outputRootDir: string;
cliPath: string;
2019-12-10 15:14:50 +00:00
}) {
const swaggerLog = createLog('Swagger 2 Codegen');
swaggerLog('Generate...');
2019-08-12 13:34:18 +00:00
await Promise.all(
Object.entries(schemes).map(async ([specName, specPath]) => {
const inputPath = specPath;
const outputDirPath = path.join(outputRootDir, specName, outputDir);
await del([outputDirPath]);
swaggerLog(`${outputDirPath} deleted`);
const cmd = `java -jar ${cliPath} generate -l typescript-angular --additional-properties ngVersion=7 -i ${inputPath} -o ${outputDirPath}`;
// angular 10 requires change "ModuleWithProviders" with "ModuleWithProviders<ApiModule>"
// swagger-codegen has a fix in v2.4.17 but it breaks our backward compatibility
swaggerLog(`> ${cmd}`);
return execWithLog(cmd).then(() => replaceModuleWithProvidersNg10(outputDirPath));
})
2019-08-12 13:34:18 +00:00
);
swaggerLog('Successfully generated 😀');
2019-12-10 15:14:50 +00:00
}
(async () => {
await swaggerCodegenAngularCli(config);
2019-08-12 13:34:18 +00:00
})();