@o3r/core
Version:
Core of the Otter Framework
115 lines • 5.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngGenerateComponent = 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");
/**
* Execute component container schematic with provided options
* @param options
*/
function generateComponentContainer(options) {
return (0, schematics_1.schematic)('component-container', {
...options,
projectName: options.projectName || undefined,
componentName: options.componentName || undefined,
prefix: options.prefix || undefined,
description: options.description || ''
});
}
/**
* Execute component presenter schematic with provided options
* @param options
*/
function generateComponentPresenter(options) {
return (0, schematics_1.schematic)('component-presenter', {
...options,
projectName: options.projectName || undefined,
prefix: options.prefix || undefined,
description: options.description || ''
});
}
/**
* Add Otter component to an Angular Project
* @param options
*/
function ngGenerateComponentFn(options) {
const generateRootBarrel = (tree, _context) => {
const inputComponentName = (0, schematics_2.getInputComponentName)(options.componentName);
const folderName = (0, schematics_2.getComponentFolderName)(inputComponentName);
const destination = (0, schematics_2.getDestinationPath)('@o3r/core:component', options.path, tree, options.projectName);
let currentComponentIndex = '';
const barrelPath = path.posix.join(destination, 'index.ts');
if ((0, schematics_2.moduleHasSubEntryPoints)(tree, destination)) {
(0, schematics_2.writeSubEntryPointPackageJson)(tree, destination, core_1.strings.dasherize(options.componentName));
}
else {
if (tree.exists(barrelPath)) {
const currentComponentIndexBuffer = tree.read(barrelPath);
currentComponentIndex = currentComponentIndexBuffer ? currentComponentIndexBuffer.toString() : '';
if (!(new RegExp(`\\.[\\\\/]${folderName}[\\\\/]index`).test(currentComponentIndex))) {
currentComponentIndex = `export * from './${folderName}/index';\n` + currentComponentIndex;
}
tree.overwrite(barrelPath, currentComponentIndex);
}
}
if (options.useComponentFixtures) {
const barrelFixturePath = path.posix.join(destination, 'fixtures.ts');
if (tree.exists(barrelFixturePath)) {
const currentComponentFixtureBuffer = tree.read(barrelFixturePath);
let currentComponentFixture = currentComponentFixtureBuffer ? currentComponentFixtureBuffer.toString() : '';
if (!(new RegExp(`\\.[\\\\/]${folderName}[\\\\/]fixtures`).test(currentComponentFixture))) {
currentComponentFixture = `export * from './${folderName}/fixtures';\n` + currentComponentFixture;
}
tree.overwrite(barrelFixturePath, currentComponentFixture);
}
}
return tree;
};
switch (options.componentStructure) {
case 'container': {
return (0, schematics_1.chain)([
generateRootBarrel,
generateComponentContainer(options)
]);
}
case 'simple':
case 'presenter': {
return (0, schematics_1.chain)([
generateRootBarrel,
generateComponentPresenter(options)
]);
}
default: {
const generateFiles = (tree, context) => {
const inputComponentName = (0, schematics_2.getInputComponentName)(options.componentName);
const folderName = (0, schematics_2.getComponentFolderName)(inputComponentName);
const destination = (0, schematics_2.getDestinationPath)('@o3r/core:component', options.path, tree, options.projectName);
const componentDestination = path.posix.join(destination, folderName);
return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates'), [
(0, schematics_1.template)({
...options,
folderName,
generateComponentIndex: true
}),
(0, schematics_1.renameTemplateFiles)(),
(0, schematics_1.move)(componentDestination)
]), schematics_1.MergeStrategy.Overwrite)(tree, context);
};
return (0, schematics_1.chain)([
generateRootBarrel,
generateComponentPresenter(options),
generateComponentContainer(options),
generateFiles,
options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)()
]);
}
}
}
/**
* Add Otter component to an Angular Project
* @param options
*/
exports.ngGenerateComponent = (0, schematics_2.createOtterSchematic)(ngGenerateComponentFn);
//# sourceMappingURL=index.js.map