@o3r/core
Version:
Core of the Otter Framework
97 lines • 5.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngGenerateService = void 0;
const path = require("node:path");
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
/**
* Add a Service to an Otter project
* @param options
*/
function ngGenerateServiceFn(options) {
const generateFiles = (tree, context) => {
const destination = (0, schematics_2.getDestinationPath)('@o3r/core:service', options.path, tree, options.projectName);
const featureName = core_1.strings.dasherize(options.featureName);
const name = core_1.strings.dasherize(options.name);
let currentServiceIndex = '';
const barrelPath = path.join(destination, 'index.ts');
if (tree.exists(barrelPath) && !(0, schematics_2.moduleHasSubEntryPoints)(tree, destination)) {
const currentServiceIndexBuffer = tree.read(barrelPath);
currentServiceIndex = currentServiceIndexBuffer ? currentServiceIndexBuffer.toString() : '';
if (!(new RegExp(`\\.[\\\\/]${name}[\\\\/]index`).test(currentServiceIndex))) {
currentServiceIndex = `export * from './${name}/index';\n` + currentServiceIndex;
}
tree.overwrite(barrelPath, currentServiceIndex);
}
const barrelJasminePath = path.join(destination, name, 'fixture', 'jasmine', 'index.ts');
const currentFixtureJasmineIndex = (tree.exists(barrelJasminePath) && tree.read(barrelJasminePath)?.toString()) || '';
const barrelJestPath = path.join(destination, name, 'fixture', 'jest', 'index.ts');
const currentFixtureJestIndex = (tree.exists(barrelJestPath) && tree.read(barrelJestPath)?.toString()) || '';
const inServiceBarrelPath = path.join(destination, name, 'index.ts');
if (tree.exists(inServiceBarrelPath)) {
const currentServiceIndexBuffer = tree.read(inServiceBarrelPath);
currentServiceIndex = currentServiceIndexBuffer ? currentServiceIndexBuffer.toString() : '';
}
else {
currentServiceIndex = '';
}
const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined;
const workspaceConfig = (0, schematics_2.getWorkspaceConfig)(tree);
let packageName = destination;
if (workspaceProject?.projectType !== 'application') {
let currentDirectory = path.normalize(destination);
const parent = '..';
// Find the package.json file in the project directory
while (!currentDirectory.includes(parent) && !tree.exists(path.join(currentDirectory, 'package.json'))) {
currentDirectory = path.join(currentDirectory, parent);
}
if (currentDirectory.includes(parent)) {
throw new schematics_2.O3rCliError('Could not find package.json in the project directory.');
}
packageName = JSON.parse(tree.read(path.join(currentDirectory, 'package.json')).toString()).name?.split('/')[0] || destination;
}
const templateData = {
...core_1.strings,
...options,
featureName,
currentServiceIndex,
currentFixtureJasmineIndex,
currentFixtureJestIndex,
serviceName: core_1.strings.capitalize(core_1.strings.camelize(options.name + ' ' + options.featureName)),
packageName
};
const baseTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)('./templates/base'), [
(0, schematics_1.template)(templateData),
(0, schematics_1.renameTemplateFiles)(),
(0, schematics_1.move)(destination)
]);
const rules = [(0, schematics_1.mergeWith)(baseTemplateSource, schematics_1.MergeStrategy.Overwrite)];
const testFramework = (0, schematics_2.getTestFramework)(workspaceConfig, context);
if (testFramework) {
rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)(`./templates/${testFramework}`), [
(0, schematics_1.template)(templateData),
(0, schematics_1.renameTemplateFiles)(),
(0, schematics_1.move)(destination)
]), schematics_1.MergeStrategy.Overwrite));
context.logger.info(`Added fixture for '${testFramework}'.`);
}
else {
context.logger.info('No test framework has been identified thus no fixture added.');
}
if ((0, schematics_2.moduleHasSubEntryPoints)(tree, destination)) {
(0, schematics_2.writeSubEntryPointPackageJson)(tree, destination, core_1.strings.dasherize(name));
}
return (0, schematics_1.chain)(rules)(tree, context);
};
return (0, schematics_1.chain)([
generateFiles,
options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)()
]);
}
/**
* Add a Service to an Otter project
* @param options
*/
exports.ngGenerateService = (0, schematics_2.createOtterSchematic)(ngGenerateServiceFn);
//# sourceMappingURL=index.js.map