UNPKG

@progress/kendo-angular-schematics

Version:

Kendo UI Schematics for Angular

139 lines 5.26 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngAdd = void 0; const tslib_1 = require("tslib"); const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const ts = tslib_1.__importStar(require("typescript")); const rules_1 = require("../rules"); const utils_1 = require("../utils"); const theming_1 = require("./theming"); const readSource = (modulePath) => (tree) => { const sourceText = (0, utils_1.fileContent)(tree, modulePath); return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); }; function updateNgModule(host, moduleToUpdate, data) { const read = readSource(moduleToUpdate); const changes = (0, utils_1.addImportToModule)({ source: read(host), symbolName: data.import, modulePath: data.from }); (0, rules_1.applyChanges)(host, moduleToUpdate, changes); if (data.export) { const changes = (0, utils_1.addExportToModule)({ source: read(host), symbolName: data.import, modulePath: data.from }); (0, rules_1.applyChanges)(host, moduleToUpdate, changes); } } const runner = (deps, callback) => (...options) => deps.forEach(dep => { if (!Object.hasOwnProperty.call(dep, 'condition') || dep.condition) { callback(...options, dep); } }); function importAllModules(options) { return (host) => { const moduleToUpdate = options.module; if (!moduleToUpdate) return host; const dependencies = [ { import: options.mainNgModule, from: `@progress/kendo-angular-${options.package}`, export: options.export }, { import: 'BrowserAnimationsModule', from: '@angular/platform-browser/animations' }, ...options.dependencies ]; const run = runner(dependencies, updateNgModule); run(host, moduleToUpdate); return host; }; } function npmInstall(options) { return (host, context) => { if (options.skipInstall) return; const installTask = context.addTask(new tasks_1.NodePackageInstallTask()); if (needsLocalize(host)) { context.addTask(new tasks_1.RunSchematicTask('@angular/localize', 'ng-add', { project: options.project }), [installTask]); } }; } const progressDependencies = (json) => { const validator = new RegExp(`^(@progress.*)$`); return Object.entries(json.peerDependencies) .filter(([k]) => validator.test(k)) .reduce((s, [k, v]) => (s[k] = v, s), {}); }; function getPackageVersion(tree, name) { if (!tree.exists('package.json')) { return null; } const packageJson = JSON.parse((0, utils_1.fileContent)(tree, 'package.json')); if (packageJson.dependencies && packageJson.dependencies[name]) { return packageJson.dependencies[name]; } return null; } function getAngularVersionString(tree) { return getPackageVersion(tree, '@angular/core'); } function getAngularMajorVersion(tree) { const version = getAngularVersionString(tree); if (!version) { return null; } const major = version.match(/(\d+)\./); if (major !== null && major.length === 2) { return parseInt(major[1], 10); } return null; } function needsLocalize(tree) { const version = getAngularMajorVersion(tree); return version >= 9 || version === null; } function registerInPackageJson(options) { return (tree, context) => { const fullPackageName = `kendo-angular-${options.package}`; const content = (0, utils_1.fileContent)(tree, `node_modules/@progress/${fullPackageName}/package.json`); const dependencies = { ...progressDependencies(JSON.parse(content)), ...options.peerDependencies }; if (needsLocalize(tree)) { dependencies['@angular/localize'] = getAngularVersionString(tree); } return (0, rules_1.addToPackageJson)({ dependencies })(tree, context); }; } function ngAdd(options) { return (host, context) => { const project = (0, utils_1.resolveProject)(host, options.project); const targets = project.architect || project.targets; const mainPath = targets.build.options.main; options.module = (0, utils_1.getAppModulePath)(host, mainPath); const toInstall = (0, schematics_1.chain)([ registerInPackageJson(options), (0, theming_1.importTheme)(options), npmInstall(options) //install as a last rule ]); return (0, schematics_1.chain)([ importAllModules(options), toInstall ])(host, context); }; } exports.ngAdd = ngAdd; //# sourceMappingURL=index.js.map