UNPKG

@progress/kendo-angular-schematics

Version:

Kendo UI Schematics for Angular

67 lines 3.65 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.addDeclarationToNgModule = exports.readIntoSourceFile = void 0; const tslib_1 = require("tslib"); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const ts = tslib_1.__importStar(require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript")); const find_module_1 = require("@schematics/angular/utility/find-module"); const ast_utils_1 = require("@schematics/angular//utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); /** * The addDeclarationToNgModule and readIntoSourceFile are obtained from v13 of: * https://github.com/angular/angular-cli/blob/13.0.x/packages/schematics/angular/component/index.ts */ function readIntoSourceFile(host, modulePath) { const text = host.read(modulePath); if (text === null) { throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`); } const sourceText = text.toString('utf-8'); return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); } exports.readIntoSourceFile = readIntoSourceFile; function addDeclarationToNgModule(options) { return (host) => { if (options.skipImport || !options.module) { return host; } options.type = options.type != null ? options.type : 'Component'; const modulePath = options.module; const source = readIntoSourceFile(host, modulePath); const componentPath = `/${options.path}/` + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') + core_1.strings.dasherize(options.name) + (options.type ? '.' : '') + core_1.strings.dasherize(options.type); const relativePath = (0, find_module_1.buildRelativePath)(modulePath, componentPath); const classifiedName = core_1.strings.classify(options.name) + core_1.strings.classify(options.type); const declarationChanges = (0, ast_utils_1.addDeclarationToModule)(source, modulePath, classifiedName, relativePath); const declarationRecorder = host.beginUpdate(modulePath); for (const change of declarationChanges) { if (change instanceof change_1.InsertChange) { declarationRecorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(declarationRecorder); if (options.export) { // Need to refresh the AST because we overwrote the file in the host. const source = readIntoSourceFile(host, modulePath); const exportRecorder = host.beginUpdate(modulePath); const exportChanges = (0, ast_utils_1.addExportToModule)(source, modulePath, core_1.strings.classify(options.name) + core_1.strings.classify(options.type), relativePath); for (const change of exportChanges) { if (change instanceof change_1.InsertChange) { exportRecorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(exportRecorder); } return host; }; } exports.addDeclarationToNgModule = addDeclarationToNgModule; //# sourceMappingURL=index.v13.js.map