"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ Object.defineProperty(exports, "__esModule", { value: true }); exports.findModuleFromOptions = exports.addModuleImportToModule = exports.addModuleImportToRootModule = exports.parseSourceFile = void 0; const schematics_1 = require("@angular-devkit/schematics"); const change_1 = require("@schematics/angular/utility/change"); const workspace_1 = require("@schematics/angular/utility/workspace"); const find_module_1 = require("@schematics/angular/utility/find-module"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const ts = require("typescript"); const project_main_file_1 = require("./project-main-file"); /** Reads file given path and returns TypeScript source file. */ function parseSourceFile(host, path) { const buffer = host.read(path); if (!buffer) { throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`); } return ts.createSourceFile(path, buffer.toString(), ts.ScriptTarget.Latest, true); } exports.parseSourceFile = parseSourceFile; /** Import and add module to root app module. */ function addModuleImportToRootModule(host, moduleName, src, project) { const modulePath = (0, ng_ast_utils_1.getAppModulePath)(host, (0, project_main_file_1.getProjectMainFile)(project)); addModuleImportToModule(host, modulePath, moduleName, src); } exports.addModuleImportToRootModule = addModuleImportToRootModule; /** * Import and add module to specific module path. * @param host the tree we are updating * @param modulePath src location of the module to import * @param moduleName name of module to import * @param src src location to import */ function addModuleImportToModule(host, modulePath, moduleName, src) { const moduleSource = parseSourceFile(host, modulePath); if (!moduleSource) { throw new schematics_1.SchematicsException(`Module not found: ${modulePath}`); } const changes = (0, ast_utils_1.addImportToModule)(moduleSource, modulePath, moduleName, src); const recorder = host.beginUpdate(modulePath); changes.forEach(change => { if (change instanceof change_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } }); host.commitUpdate(recorder); } exports.addModuleImportToModule = addModuleImportToModule; /** Wraps the internal find module from options with undefined path handling */ async function findModuleFromOptions(host, options) { const workspace = await (0, workspace_1.getWorkspace)(host); if (!options.project) { options.project = Array.from(workspace.projects.keys())[0]; } const project = workspace.projects.get(options.project); if (options.path === undefined) { options.path = `/${project.root}/src/app`; } return (0, find_module_1.findModuleFromOptions)(host, options); } exports.findModuleFromOptions = findModuleFromOptions; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXN0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay9zY2hlbWF0aWNzL3V0aWxzL2FzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Ozs7OztHQU1HOzs7QUFFSCwyREFBcUU7QUFFckUsK0RBQWdFO0FBQ2hFLHFFQUFtRTtBQUNuRSx5RUFBb0c7QUFDcEcscUVBQXdFO0FBQ3hFLDJFQUEwRTtBQUUxRSxpQ0FBaUM7QUFDakMsMkRBQXVEO0FBRXZELGdFQUFnRTtBQUNoRSxTQUFnQixlQUFlLENBQUMsSUFBVSxFQUFFLElBQVk7SUFDdEQsTUFBTSxNQUFNLEdBQUcsSUFBSSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUMvQixJQUFJLENBQUMsTUFBTSxFQUFFO1FBQ1gsTUFBTSxJQUFJLGdDQUFtQixDQUFDLGlDQUFpQyxJQUFJLEVBQUUsQ0FBQyxDQUFDO0tBQ3hFO0lBQ0QsT0FBTyxFQUFFLENBQUMsZ0JBQWdCLENBQUMsSUFBSSxFQUFFLE1BQU0sQ0FBQyxRQUFRLEVBQUUsRUFBRSxFQUFFLENBQUMsWUFBWSxDQUFDLE1BQU0sRUFBRSxJQUFJLENBQUMsQ0FBQztBQUNwRixDQUFDO0FBTkQsMENBTUM7QUFFRCxnREFBZ0Q7QUFDaEQsU0FBZ0IsMkJBQTJCLENBQ3pDLElBQVUsRUFDVixVQUFrQixFQUNsQixHQUFXLEVBQ1gsT0FBcUM7SUFFckMsTUFBTSxVQUFVLEdBQUcsSUFBQSwrQkFBZ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