UNPKG

@o3r/core

Version:
131 lines • 7.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngGenerateComponentPresenter = exports.PRESENTER_FOLDER = void 0; const path = require("node:path"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const analytics_1 = require("../../rule-factories/component/analytics"); const configuration_1 = require("../../rule-factories/component/configuration"); const context_1 = require("../../rule-factories/component/context"); const design_token_1 = require("../../rule-factories/component/design-token"); const fixture_1 = require("../../rule-factories/component/fixture"); const localization_1 = require("../../rule-factories/component/localization"); const theming_1 = require("../../rule-factories/component/theming"); const structures_types_1 = require("../structures.types"); exports.PRESENTER_FOLDER = 'presenter'; /** * Generates the template properties * @param options * @param componentStructureDef * @param prefix */ const getTemplateProperties = (options, componentStructureDef, prefix) => { const inputComponentName = (0, schematics_2.getInputComponentName)(options.componentName); const folderName = (0, schematics_2.getComponentFolderName)(inputComponentName); const structure = componentStructureDef === structures_types_1.ComponentStructureDef.Simple ? '' : componentStructureDef; return { ...options, componentName: (0, schematics_2.getComponentName)(inputComponentName, structure).replace(/Component$/, ''), componentSelector: (0, schematics_2.getComponentSelectorWithoutSuffix)(options.componentName, prefix || null), projectName: options.projectName || (0, schematics_2.getLibraryNameFromPath)(options.path), folderName, name: (0, schematics_2.getComponentFileName)(options.componentName, structure), suffix: structure.toLowerCase(), description: options.description || '' }; }; /** * Add Otter component to an Angular Project * @param options */ function ngGenerateComponentPresenterFn(options) { const fullStructureRequested = options.componentStructure === 'full'; const generateFiles = (tree, _context) => { const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined; const properties = getTemplateProperties(options, options.componentStructure === 'simple' ? structures_types_1.ComponentStructureDef.Simple : structures_types_1.ComponentStructureDef.Pres, options.prefix || workspaceProject?.prefix); const destination = (0, schematics_2.getDestinationPath)('@o3r/core:component', options.path, tree, options.projectName); const componentDestination = path.posix.join(destination, fullStructureRequested ? path.posix.join(properties.folderName, exports.PRESENTER_FOLDER) : properties.folderName); const componentPath = path.posix.join(componentDestination, `${properties.name}.component.ts`); const ngSpecPath = path.posix.join(componentDestination, `${properties.name}.component.spec.ts`); const o3rSpecPath = path.posix.join(componentDestination, `${properties.name}.spec.ts`); const ngStylePath = path.posix.join(componentDestination, `${properties.name}.component.scss`); const o3rStylePath = path.posix.join(componentDestination, `${properties.name}.style.scss`); const o3rDesignTokenPath = path.posix.join(componentDestination, `${properties.name}.theme.json`); const ngTemplatePath = path.posix.join(componentDestination, `${properties.name}.component.html`); const o3rTemplatePath = path.posix.join(componentDestination, `${properties.name}.template.html`); const componentSelector = `${properties.componentSelector}${properties.suffix ? ('-' + properties.suffix) : ''}`; const rules = []; if (!options.standalone) { rules.push((0, schematics_1.externalSchematic)('@schematics/angular', 'module', { project: properties.projectName, path: componentDestination, flat: true, name: properties.componentName })); } rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates'), [ (0, schematics_1.template)(properties), (0, schematics_1.renameTemplateFiles)(), (0, schematics_1.move)(componentDestination) ]), schematics_1.MergeStrategy.Overwrite), (0, schematics_1.externalSchematic)('@schematics/angular', 'component', { project: properties.projectName, selector: componentSelector, path: componentDestination, name: properties.componentName, inlineStyle: false, inlineTemplate: false, viewEncapsulation: 'None', changeDetection: 'OnPush', style: 'scss', type: 'Component', skipSelector: false, standalone: options.standalone, ...(options.standalone ? { skipImport: true } : { module: `${properties.name}.module.ts`, export: true }), flat: true }), // Angular schematics generate spec file with this pattern: component-name.component.spec.ts (0, schematics_1.move)(ngSpecPath, o3rSpecPath), // Angular schematics generate style file with this pattern: component-name.component.scss (0, schematics_1.chain)([ (0, schematics_1.move)(ngStylePath, o3rStylePath), (t) => { // Styling file is empty by default, as we create component with `viewEncapsulation` set to 'None', we should wrap the styling into the selector of the component t.overwrite(o3rStylePath, `${componentSelector} {\n\t// Your component custom SCSS\n}\n`); return t; }, (t) => { t.overwrite(componentPath, t.readText(componentPath).replace(path.basename(ngStylePath), path.basename(o3rStylePath))); return t; } ]), // Angular schematics generate template file with this pattern: component-name.component.html (0, schematics_1.chain)([ (0, schematics_1.move)(ngTemplatePath, o3rTemplatePath), (t) => { t.overwrite(componentPath, t.readText(componentPath).replace(path.basename(ngTemplatePath), path.basename(o3rTemplatePath))); return t; } ]), (0, schematics_1.schematic)('convert-component', { path: componentPath, skipLinter: options.skipLinter }), (0, configuration_1.getAddConfigurationRules)(componentPath, options), (0, theming_1.getAddThemingRules)(o3rStylePath, options), (0, design_token_1.getAddDesignTokenRules)(o3rStylePath, o3rDesignTokenPath, options), (0, localization_1.getAddLocalizationRules)(componentPath, options), (0, fixture_1.getAddFixtureRules)(componentPath, options), (0, analytics_1.getAddAnalyticsRules)(componentPath, options), (0, context_1.getAddContextRules)(componentPath, options)); return (0, schematics_1.chain)(rules); }; return (0, schematics_1.chain)([ generateFiles, fullStructureRequested ? (0, schematics_1.noop)() : (options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)()) ]); } /** * Add Otter component to an Angular Project * @param options */ exports.ngGenerateComponentPresenter = (0, schematics_2.createOtterSchematic)(ngGenerateComponentPresenterFn); //# sourceMappingURL=index.js.map