UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

78 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getModuleIndex = getModuleIndex; exports.getFileInfo = getFileInfo; const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const ts = require("typescript"); const modules_1 = require("./modules"); /** * Get the module index of the decorator in a file (supports both standalone and module) * @param sourceFile * @param sourceContent */ function getModuleIndex(sourceFile, sourceContent) { const decorators = [ { name: 'NgModule', isStandalone: false }, { name: 'Component', isStandalone: true } ]; for (const decorator of decorators) { const moduleMetadata = (0, ast_utils_1.getDecoratorMetadata)(sourceFile, decorator.name, '@angular/core'); if (moduleMetadata[0]) { return { ngModulesMetadata: moduleMetadata, moduleIndex: moduleMetadata[0].pos - (decorator.name.length + 1), isStandalone: decorator.isStandalone }; } else { const index = sourceContent.indexOf(`@${decorator.name}`); if (index !== -1) { return { ngModulesMetadata: undefined, moduleIndex: index, isStandalone: decorator.isStandalone }; } } } // No decorators found, put the index just before the first export (which will work for the appConfig in standalone) return { ngModulesMetadata: undefined, moduleIndex: sourceContent.indexOf('export '), isStandalone: true }; } /** * Get file information in schematics context * @param tree * @param context * @param projectName */ function getFileInfo(tree, context, projectName) { const fileInfo = { moduleFilePath: undefined, sourceFile: undefined, ngModulesMetadata: undefined, appModuleFile: undefined, moduleIndex: -1, isStandalone: false }; const moduleFilePath = (0, modules_1.getAppModuleFilePath)(tree, context, projectName); if (!moduleFilePath) { return fileInfo; } fileInfo.moduleFilePath = moduleFilePath; if (!tree.exists(moduleFilePath)) { context.logger.warn(`The module file ${moduleFilePath} does not exist, the edition will be skipped`); return fileInfo; } const sourceFile = ts.createSourceFile(moduleFilePath, tree.read(moduleFilePath).toString(), ts.ScriptTarget.ES2015, true); fileInfo.sourceFile = sourceFile; fileInfo.appModuleFile = tree.read(moduleFilePath).toString(); const moduleIndex = getModuleIndex(sourceFile, fileInfo.appModuleFile); fileInfo.ngModulesMetadata = moduleIndex.ngModulesMetadata; fileInfo.moduleIndex = moduleIndex.moduleIndex; fileInfo.isStandalone = moduleIndex.isStandalone; return fileInfo; } //# sourceMappingURL=file-info.js.map