UNPKG

@o3r/rules-engine

Version:

This module provides a rule engine that can be executed on your Otter application to customize your application (translations, placeholders and configs) based on a json file generated by your CMS.

68 lines 3.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngGenerateFactsService = void 0; const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("node:path")); const strings_1 = require("@angular-devkit/core/src/utils/strings"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); /** * Generate the facts service * @param options */ function ngGenerateFactsServiceFn(options) { const generateFiles = () => { const name = (0, strings_1.dasherize)(options.name); const serviceFolderPath = path.posix.join(options.path, name); const ngServicePath = path.posix.join(serviceFolderPath, `${name}-facts.ts`); const o3rServicePath = path.posix.join(serviceFolderPath, `${name}-facts-service.ts`); const factsInterfaceName = `${(0, strings_1.classify)(name)}Facts`; return (0, schematics_1.chain)([ (0, schematics_1.externalSchematic)('@schematics/angular', 'service', { project: options.projectName, path: serviceFolderPath, flat: true, name: factsInterfaceName, skipTests: true }), // Angular schematics will generate this service file with this pattern: -facts.ts (since the factsInterfaceName ends with 'Facts') // We need to rename it to -facts-service.ts to avoid confusion with the facts file that is created just after (0, schematics_1.move)(ngServicePath, o3rServicePath), (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates'), [ (0, schematics_1.template)({ name, factsInterfaceName }), (0, schematics_1.renameTemplateFiles)(), (0, schematics_1.move)(serviceFolderPath) ]), schematics_1.MergeStrategy.Overwrite), (0, schematics_2.addImportsRule)(o3rServicePath, [ { from: '@o3r/rules-engine', importNames: ['FactsService', 'RulesEngineService'] }, { from: `./${name}-facts`, importNames: [factsInterfaceName] } ]), (t) => { const serviceText = t.readText(o3rServicePath); t.overwrite(o3rServicePath, serviceText .replace('constructor() { }', 'public facts = {};') .replace(`${factsInterfaceName} {`, `${factsInterfaceName} extends FactsService<${factsInterfaceName}> {`)); return t; } ]); }; return (0, schematics_1.chain)([ generateFiles, options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)() ]); } /** * Generate the facts service * @param options */ exports.ngGenerateFactsService = (0, schematics_2.createOtterSchematic)(ngGenerateFactsServiceFn); //# sourceMappingURL=index.js.map