frontend-openapi-generator-cli/lib/bin.js

70 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2022-03-31 08:49:31 +00:00
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const sade = require('sade');
const shell = require('shelljs');
const DEFAULT_ANGULAR_VERSION = 'latest';
const DEFAULT_OUTPUT_DIR = 'lib';
2022-03-31 08:49:31 +00:00
const PACKAGE_JSON = require('../package.json');
const CLI_NAME = Object.keys(PACKAGE_JSON.bin)[0];
const prog = sade(CLI_NAME).describe('OpenAPI generator CLI').version(PACKAGE_JSON.version);
function createPackageJson(swagPackageJson, projectPackageJson) {
return {
...swagPackageJson,
version: projectPackageJson.version || swagPackageJson.version,
license: projectPackageJson.license || 'MIT',
publishConfig: projectPackageJson.publishConfig || {
access: 'public',
registry: 'https://registry.npmjs.org/',
},
};
}
2022-03-31 08:49:31 +00:00
prog.command('generate <src>')
.describe('Generate OpenAPI for Angular')
.option('-o, --output')
.option('-n, --ng-version')
.action((src, opts) => {
const angularVersion = opts.n || DEFAULT_ANGULAR_VERSION;
const outputDir = opts.o || DEFAULT_OUTPUT_DIR;
2022-03-31 08:49:31 +00:00
const ngCommand = `npm_config_yes=true npx -p @angular/cli@${angularVersion} ng`;
2022-08-02 18:55:46 +00:00
const openapiGeneratorCliCommand = `npx @openapitools/openapi-generator-cli`;
2022-03-31 08:49:31 +00:00
const generatedDir = CLI_NAME;
const packageJson = require(path.join(process.cwd(), 'package.json'));
const libraryName = packageJson.name;
const libraryPath = path.join(generatedDir, 'projects', libraryName.replace('@', ''));
shell.rm('-rf', generatedDir);
shell.rm('-rf', outputDir);
shell.exec(`${ngCommand} new ${generatedDir} --no-create-application`);
shell.cd(generatedDir);
shell.exec(`${ngCommand} generate library ${libraryName}`);
shell.cd(`..`);
shell.rm('-rf', path.join(libraryPath, 'src/lib'));
fs.writeFileSync(path.join(libraryPath, 'src/public-api.ts'), 'export * from "./lib";');
shell.exec(
`${openapiGeneratorCliCommand} generate -i ${src} -g typescript-angular -o ${path.join(
libraryPath,
'src/lib'
2022-04-11 15:59:57 +00:00
)} -p=useSingleRequestParameter=true`
2022-03-31 08:49:31 +00:00
);
shell.cd(generatedDir);
shell.exec(`npm run build`);
shell.cd('..');
shell.mv(path.join(generatedDir, 'dist', libraryName.replace('@', '')), outputDir);
const swagPackageJson = createPackageJson(
require(path.join(process.cwd(), outputDir, 'package.json')),
packageJson
);
fs.writeFileSync(path.join(outputDir, 'package.json'), JSON.stringify(swagPackageJson, null, 2));
2022-03-31 08:49:31 +00:00
shell.rm('-rf', generatedDir);
});
prog.parse(process.argv);