@o3r/amaterasu-sdk
Version:
Ama-terasu module for DES SDK generation
48 lines • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTypescriptSdk = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const core_1 = require("@ama-terasu/core");
/**
* Create an empty Typescript SDK
* @param context Context of the command
* @param options Options
*/
const createTypescriptSdk = async (context, options) => {
const { logger } = context;
const cwd = (0, node_path_1.resolve)(process.cwd(), options.path);
const inPackageCwd = (0, node_path_1.resolve)(cwd, 'SDK');
const npmClient = options.yarn ? 'yarn' : 'npm';
const { version } = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.resolve)(__dirname, '..', 'package.json'), { encoding: 'utf8' }));
const npmrcFile = 'tmp.npmrc';
const deps = {
'@ama-sdk/schematics': version && version !== '0.0.0-placeholder' ? version : 'latest',
yo: 'latest'
};
await context.getSpinner('Generating SDK package...').fromPromise((async () => {
await (0, core_1.promiseSpawn)('npm init -y', { cwd, stderrLogger: (...args) => logger.debug(...args), logger });
await (0, core_1.promiseSpawn)(`npm install --userconfig ${npmrcFile} --include dev ${Object.entries(deps).map(([n, v]) => `${n}@${v}`).join(' ')}`, { cwd, stderrLogger: (...args) => logger.debug(...args), logger });
await (0, core_1.promiseSpawn)(`npx -p @angular-devkit/schematics-cli schematics @ama-sdk/schematics:typescript-shell --package-name sdk --description "${options.name} SDK"`, { cwd, stderrLogger: (...args) => logger.debug(...args), logger });
})(),
// TODO: simplify to the following line when migrated to schematics generation
// promiseSpawn(
// eslint-disable-next-line @stylistic/max-len -- keep the command on the same line
// `npx ${Object.entries(deps).map(([n, v]) => `-p ${n}@${v}`).join(' ')} --userconfig ${npmrcFile} yo${options.yes ? ' --force=true' : ''} @ama-sdk/sdk:shell --projectName "${options.name}" --projectPackageName sdk --projectDescription "${options.name} SDK" --projectHosting "Azure DevOps"`,
// { cwd, stderrLogger: (...args: any[]) => logger.debug(...args), logger }
// ),
`SDK Shell generated (in ${inPackageCwd})`);
await context.getSpinner('Clean up setup materials').fromPromise(Promise.all([
node_fs_1.promises.unlink((0, node_path_1.resolve)(cwd, npmrcFile)),
node_fs_1.promises.unlink((0, node_path_1.resolve)(cwd, 'package.json')),
node_fs_1.promises.rm((0, node_path_1.resolve)(cwd, 'node_modules'), { force: true, recursive: true })
]), 'Setup material removed');
await context.getSpinner(`Installing dependencies with ${npmClient}...`).fromPromise((0, core_1.promiseSpawn)(`${npmClient} install`, { cwd: inPackageCwd, stderrLogger: (...args) => logger.debug(...args), logger }), `NPM Dependency installed (with ${npmClient})`);
if (options.specification) {
await context.getSpinner('Creating specification file...').fromPromise(node_fs_1.promises.copyFile((0, node_path_1.resolve)(process.cwd(), options.specification), (0, node_path_1.resolve)(inPackageCwd, 'swagger-spec.yaml')), 'swagger-spec.yaml file created');
await context.getSpinner('Generating SDK based on specification...').fromPromise((0, core_1.promiseSpawn)(`${npmClient} run swagger:regen`, { cwd: inPackageCwd, stderrLogger: (...args) => logger.debug(...args), logger }), `SDK Code generated (in ${(0, node_path_1.resolve)(inPackageCwd, 'src')})`);
}
logger.info(`SDK created in ${inPackageCwd}`);
};
exports.createTypescriptSdk = createTypescriptSdk;
//# sourceMappingURL=create-typescript.command.js.map