UNPKG

@o3r/core

Version:
180 lines • 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngGenerateComponentContainer = exports.CONTAINER_FOLDER = void 0; const path = require("node:path"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); const ts = require("typescript"); const configuration_1 = require("../../rule-factories/component/configuration"); const context_1 = require("../../rule-factories/component/context"); const fixture_1 = require("../../rule-factories/component/fixture"); const rules_engine_1 = require("../../rule-factories/component/rules-engine"); const presenter_1 = require("../presenter"); const structures_types_1 = require("../structures.types"); exports.CONTAINER_FOLDER = 'container'; /** * 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 componentSelector = (0, schematics_2.getComponentSelectorWithoutSuffix)(options.componentName, prefix || null); return { ...options, componentType: options.componentStructure === 'full' ? 'Block' : 'Component', presenterModuleName: (0, schematics_2.getComponentModuleName)(inputComponentName, structures_types_1.ComponentStructureDef.Pres), componentName: (0, schematics_2.getComponentBaseName)(inputComponentName, componentStructureDef), presenterComponentName: (0, schematics_2.getComponentName)(inputComponentName, structures_types_1.ComponentStructureDef.Pres, options.type), presenterComponentSelector: `${componentSelector}-${structures_types_1.ComponentStructureDef.Pres.toLowerCase()}`, componentSelector: (0, schematics_2.getComponentSelectorWithoutSuffix)(options.componentName, prefix || null), folderName, name: (0, schematics_2.getComponentFileName)(options.componentName, componentStructureDef, options.type), // air-offer | air-offer-cont, suffix: componentStructureDef.toLowerCase(), // cont | '' description: options.description || '' }; }; /** * Add Otter container component to an Angular Project * @param options */ function ngGenerateComponentContainerFn(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, structures_types_1.ComponentStructureDef.Cont, 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.CONTAINER_FOLDER) : `${properties.folderName}${properties.suffix ? ('-' + properties.suffix) : ''}`); const componentPath = path.posix.join(componentDestination, `${properties.name}.ts`); const specPath = path.posix.join(componentDestination, `${properties.name}.spec.ts`); const templatePath = path.posix.join(componentDestination, `${properties.name}.html`); 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: `${properties.componentSelector}-${properties.suffix}`, path: componentDestination, name: properties.componentName, inlineStyle: false, inlineTemplate: false, viewEncapsulation: 'None', changeDetection: 'OnPush', style: 'none', type: properties.type, skipSelector: false, skipTests: false, standalone: options.standalone, ...(options.standalone ? { skipImport: true } : { module: `${properties.name}-module.ts`, export: true }), flat: true }), (0, schematics_1.schematic)('convert-component', { path: componentPath, skipLinter: options.skipLinter, componentType: properties.componentType })); if (fullStructureRequested) { const componentPresenterDestination = path.posix.join('..', presenter_1.PRESENTER_FOLDER, 'index'); const addPresenterComponentOrModuleToImport = options.standalone ? (0, schematics_1.chain)([ (0, schematics_2.addImportsRule)(componentPath, [ { from: componentPresenterDestination, importNames: [properties.presenterComponentName] } ]), () => { const componentSourceFile = ts.createSourceFile(componentPath, tree.readText(componentPath), ts.ScriptTarget.ES2020, true); const result = ts.transform(componentSourceFile, [ (0, schematics_2.addImportsIntoComponentDecoratorTransformerFactory)([properties.presenterComponentName]) ]); const printer = ts.createPrinter({ removeComments: false, newLine: ts.NewLineKind.LineFeed }); tree.overwrite(componentPath, printer.printFile(result.transformed[0])); return tree; } ]) : () => { const modulePath = path.posix.join(componentDestination, `${properties.name}-module.ts`); const moduleSourceFile = ts.createSourceFile(modulePath, tree.readText(modulePath), ts.ScriptTarget.ES2020, true); const changes = (0, ast_utils_1.addImportToModule)(moduleSourceFile, modulePath, properties.presenterModuleName, componentPresenterDestination); const recorder = tree.beginUpdate(modulePath); (0, change_1.applyToUpdateRecorder)(recorder, changes); tree.commitUpdate(recorder); return tree; }; const addMockPresenterComponentInSpecFile = () => { if (!tree.exists(specPath)) { context.logger.warn(`No update applied on spec file because ${specPath} does not exist.`); return; } let specSourceFile = ts.createSourceFile(specPath, tree.readText(specPath), ts.ScriptTarget.ES2020, true); const recorder = tree.beginUpdate(specPath); const lastImport = [...specSourceFile.statements].reverse().find((statement) => ts.isImportDeclaration(statement)); const changes = [ (0, ast_utils_1.insertImport)(specSourceFile, specPath, 'Component', '@angular/core'), new change_1.InsertChange(specPath, lastImport?.getEnd() || 0, ` @Component({ template: '', selector: '${properties.presenterComponentSelector}' }) class Mock${properties.presenterComponentName} {} `) ]; (0, change_1.applyToUpdateRecorder)(recorder, changes); tree.commitUpdate(recorder); specSourceFile = ts.createSourceFile(specPath, tree.readText(specPath), ts.ScriptTarget.ES2020, true); const result = ts.transform(specSourceFile, [ (0, schematics_2.addImportsAndCodeBlockStatementAtSpecInitializationTransformerFactory)([ `Mock${properties.presenterComponentName}` ]) ]); const printer = ts.createPrinter({ removeComments: false, newLine: ts.NewLineKind.LineFeed }); const newContent = printer.printFile(result.transformed[0]); tree.overwrite(specPath, newContent); return tree; }; rules.push((t) => { t.overwrite(templatePath, `<${properties.presenterComponentSelector}></${properties.presenterComponentSelector}>`); return t; }, addPresenterComponentOrModuleToImport, addMockPresenterComponentInSpecFile); } rules.push((0, configuration_1.getAddConfigurationRules)(componentPath, options), (0, fixture_1.getAddFixtureRules)(componentPath, options), (0, context_1.getAddContextRules)(componentPath, options), (0, rules_engine_1.getAddRulesEngineRules)(path.posix.join(componentDestination, `${properties.name}.ts`), 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 container component to an Angular Project * @param options */ exports.ngGenerateComponentContainer = (0, schematics_2.createOtterSchematic)(ngGenerateComponentContainerFn); //# sourceMappingURL=index.js.map