@o3r/workspace
Version:
Workspace tooling of the Otter Framework
121 lines • 6.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSdk = void 0;
const node_fs_1 = require("node:fs");
const path = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const schematics_2 = require("@o3r/schematics");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const clean_standalone_rule_1 = require("./rules/clean-standalone.rule");
const rules_ng_1 = require("./rules/rules.ng");
const rules_nx_1 = require("./rules/rules.nx");
const update_ts_paths_rule_1 = require("./rules/update-ts-paths.rule");
/**
* Add an Otter compatible SDK to a monorepo
* @param options Schematic options
*/
function generateSdkFn(options) {
const splitName = options.name?.split('/');
const scope = schematics_1.strings.dasherize(splitName.length > 1 ? splitName[0].replace(/^@/, '') : options.name);
const projectName = splitName?.length === 2 ? schematics_1.strings.dasherize(splitName[1]) : 'sdk';
const cleanName = schematics_1.strings.dasherize(options.name).replace(/^@/, '').replaceAll(/\//g, '-');
const ownPackageJsonContent = JSON.parse((0, node_fs_1.readFileSync)(path.resolve(__dirname, '..', '..', 'package.json'), { encoding: 'utf8' }));
const o3rDevPackageToInstall = [];
if ((0, schematics_2.isPackageInstalled)('@o3r/eslint-config')) {
o3rDevPackageToInstall.push('@o3r/eslint-config');
}
const dependencies = {
...o3rDevPackageToInstall.reduce((acc, dep) => {
acc[dep] = {
inManifest: [
{
range: `${options.exactO3rVersion ? '' : '~'}${ownPackageJsonContent.version}`,
types: [dependencies_1.NodeDependencyType.Dev]
}
],
ngAddOptions: { exactO3rVersion: options.exactO3rVersion }
};
return acc;
}, {})
};
return (tree, context) => {
const isNx = (0, schematics_2.isNxContext)(tree);
const workspaceConfig = (0, schematics_2.getWorkspaceConfig)(tree);
if (!workspaceConfig) {
throw new schematics_2.O3rCliError('No workspace configuration file found');
}
const defaultRoot = (0, schematics_2.getPackagesBaseRootFolder)(tree, context, workspaceConfig, 'library');
/** Path to the folder where generate the new SDK */
const targetPath = path.posix.join(options.path || defaultRoot, cleanName);
const overrideFilesIfRootJestConfigUTExists = tree.exists('jest.config.ut.js')
? (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates'), [
(0, schematics_1.template)({
...options,
rootRelativePath: path.posix.relative(targetPath, tree.root.path.replace(/^\//, './'))
}),
(0, schematics_1.move)(targetPath),
(0, schematics_1.renameTemplateFiles)()
]), schematics_1.MergeStrategy.Overwrite)
: (0, schematics_1.noop)();
const packageManager = (0, schematics_2.getPackageManager)({ workspaceConfig });
const specExtension = options.specPackagePath ? path.extname(options.specPackagePath) : '.yaml';
// If spec path is relative to process.cwd, we need to make it relative to the project root
if (options.specPath && !path.isAbsolute(options.specPath)) {
const resolvedPath = path.resolve(process.cwd(), options.specPath);
if ((0, node_fs_1.existsSync)(resolvedPath)) {
options.specPath = path.relative(path.resolve(targetPath), resolvedPath);
}
}
const specPath = options.specPackageName ? `openapi${specExtension}` : options.specPath;
const specUpgradeTask = [];
const sdkGenerationTasks = [];
if (specPath) {
const installTask = context.addTask(new tasks_1.NodePackageInstallTask());
if (options.specPackageName) {
specUpgradeTask.push(context.addTask(new schematics_2.NpmExecTask('amasdk-update-spec-from-npm', [
options.specPackageName,
...options.specPackagePath ? ['--package-path', options.specPackagePath] : [],
'--output', path.join(process.cwd(), targetPath, `openapi${specExtension}`)
], targetPath), [installTask]));
}
const generationTask = context.addTask(new tasks_1.RunSchematicTask('@ama-sdk/schematics', 'typescript-core', {
...options,
specPath,
directory: targetPath,
packageManager
}), [
installTask,
...specUpgradeTask
]);
sdkGenerationTasks.push(installTask, ...specUpgradeTask, generationTask);
}
return (0, schematics_1.chain)([
(0, schematics_1.externalSchematic)('@ama-sdk/schematics', 'typescript-shell', {
...options,
package: projectName,
name: scope,
directory: targetPath,
packageManager,
skipInstall: !!options.specPath || options.skipInstall
}),
isNx ? (0, rules_nx_1.nxRegisterProjectTasks)(options, targetPath, cleanName) : (0, rules_ng_1.ngRegisterProjectTasks)(options, targetPath, cleanName),
(0, update_ts_paths_rule_1.updateTsConfig)(targetPath, projectName, scope),
(0, clean_standalone_rule_1.cleanStandaloneFiles)(targetPath),
overrideFilesIfRootJestConfigUTExists,
(0, schematics_2.setupDependencies)({
dependencies,
skipInstall: options.skipInstall,
ngAddToRun: Object.keys(dependencies),
projectName: cleanName,
runAfterTasks: sdkGenerationTasks
})
]);
};
}
/**
* Add an Otter compatible SDK to a monorepo
* @param options Schematic options
*/
exports.generateSdk = (0, schematics_2.createOtterSchematic)(generateSdkFn);
//# sourceMappingURL=index.js.map