UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

112 lines 5.2 kB
"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.dev/license */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseSourceFile = parseSourceFile; exports.addModuleImportToRootModule = addModuleImportToRootModule; exports.addModuleImportToModule = addModuleImportToModule; exports.findModuleFromOptions = findModuleFromOptions; const schematics_1 = require("@angular-devkit/schematics"); const change_1 = require("@schematics/angular/utility/change"); const utility_1 = require("@schematics/angular/utility"); 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 = __importStar(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); } /** 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); } /** * 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); } /** Wraps the internal find module from options with undefined path handling */ function findModuleFromOptions(host, options) { return __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, utility_1.readWorkspace)(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); }); } //# sourceMappingURL=ast.js.map