[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 29 | const core_1 = require("@angular-devkit/core");
|
---|
| 30 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
| 31 | const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
|
---|
| 32 | const ast_utils_1 = require("../utility/ast-utils");
|
---|
| 33 | const change_1 = require("../utility/change");
|
---|
| 34 | const find_module_1 = require("../utility/find-module");
|
---|
| 35 | const lint_fix_1 = require("../utility/lint-fix");
|
---|
| 36 | const parse_name_1 = require("../utility/parse-name");
|
---|
| 37 | const workspace_1 = require("../utility/workspace");
|
---|
| 38 | const schema_1 = require("./schema");
|
---|
| 39 | function buildRelativeModulePath(options, modulePath) {
|
---|
| 40 | const importModulePath = core_1.normalize(`/${options.path}/` +
|
---|
| 41 | (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') +
|
---|
| 42 | core_1.strings.dasherize(options.name) +
|
---|
| 43 | '.module');
|
---|
| 44 | return find_module_1.buildRelativePath(modulePath, importModulePath);
|
---|
| 45 | }
|
---|
| 46 | function addDeclarationToNgModule(options) {
|
---|
| 47 | return (host) => {
|
---|
| 48 | if (!options.module) {
|
---|
| 49 | return host;
|
---|
| 50 | }
|
---|
| 51 | const modulePath = options.module;
|
---|
| 52 | const text = host.read(modulePath);
|
---|
| 53 | if (text === null) {
|
---|
| 54 | throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
|
---|
| 55 | }
|
---|
| 56 | const sourceText = text.toString();
|
---|
| 57 | const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
|
---|
| 58 | const relativePath = buildRelativeModulePath(options, modulePath);
|
---|
| 59 | const changes = ast_utils_1.addImportToModule(source, modulePath, core_1.strings.classify(`${options.name}Module`), relativePath);
|
---|
| 60 | const recorder = host.beginUpdate(modulePath);
|
---|
| 61 | for (const change of changes) {
|
---|
| 62 | if (change instanceof change_1.InsertChange) {
|
---|
| 63 | recorder.insertLeft(change.pos, change.toAdd);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | host.commitUpdate(recorder);
|
---|
| 67 | return host;
|
---|
| 68 | };
|
---|
| 69 | }
|
---|
| 70 | function addRouteDeclarationToNgModule(options, routingModulePath) {
|
---|
| 71 | return (host) => {
|
---|
| 72 | if (!options.route) {
|
---|
| 73 | return host;
|
---|
| 74 | }
|
---|
| 75 | if (!options.module) {
|
---|
| 76 | throw new Error('Module option required when creating a lazy loaded routing module.');
|
---|
| 77 | }
|
---|
| 78 | let path;
|
---|
| 79 | if (routingModulePath) {
|
---|
| 80 | path = routingModulePath;
|
---|
| 81 | }
|
---|
| 82 | else {
|
---|
| 83 | path = options.module;
|
---|
| 84 | }
|
---|
| 85 | const text = host.read(path);
|
---|
| 86 | if (!text) {
|
---|
| 87 | throw new Error(`Couldn't find the module nor its routing module.`);
|
---|
| 88 | }
|
---|
| 89 | const sourceText = text.toString();
|
---|
| 90 | const addDeclaration = ast_utils_1.addRouteDeclarationToModule(ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true), path, buildRoute(options, options.module));
|
---|
| 91 | const recorder = host.beginUpdate(path);
|
---|
| 92 | recorder.insertLeft(addDeclaration.pos, addDeclaration.toAdd);
|
---|
| 93 | host.commitUpdate(recorder);
|
---|
| 94 | return host;
|
---|
| 95 | };
|
---|
| 96 | }
|
---|
| 97 | function getRoutingModulePath(host, modulePath) {
|
---|
| 98 | const routingModulePath = modulePath.endsWith(find_module_1.ROUTING_MODULE_EXT)
|
---|
| 99 | ? modulePath
|
---|
| 100 | : modulePath.replace(find_module_1.MODULE_EXT, find_module_1.ROUTING_MODULE_EXT);
|
---|
| 101 | return host.exists(routingModulePath) ? core_1.normalize(routingModulePath) : undefined;
|
---|
| 102 | }
|
---|
| 103 | function buildRoute(options, modulePath) {
|
---|
| 104 | const relativeModulePath = buildRelativeModulePath(options, modulePath);
|
---|
| 105 | const moduleName = `${core_1.strings.classify(options.name)}Module`;
|
---|
| 106 | const loadChildren = `() => import('${relativeModulePath}').then(m => m.${moduleName})`;
|
---|
| 107 | return `{ path: '${options.route}', loadChildren: ${loadChildren} }`;
|
---|
| 108 | }
|
---|
| 109 | function default_1(options) {
|
---|
| 110 | return async (host) => {
|
---|
| 111 | if (options.path === undefined) {
|
---|
| 112 | options.path = await workspace_1.createDefaultPath(host, options.project);
|
---|
| 113 | }
|
---|
| 114 | if (options.module) {
|
---|
| 115 | options.module = find_module_1.findModuleFromOptions(host, options);
|
---|
| 116 | }
|
---|
| 117 | let routingModulePath;
|
---|
| 118 | const isLazyLoadedModuleGen = !!(options.route && options.module);
|
---|
| 119 | if (isLazyLoadedModuleGen) {
|
---|
| 120 | options.routingScope = schema_1.RoutingScope.Child;
|
---|
| 121 | routingModulePath = getRoutingModulePath(host, options.module);
|
---|
| 122 | }
|
---|
| 123 | const parsedPath = parse_name_1.parseName(options.path, options.name);
|
---|
| 124 | options.name = parsedPath.name;
|
---|
| 125 | options.path = parsedPath.path;
|
---|
| 126 | const templateSource = schematics_1.apply(schematics_1.url('./files'), [
|
---|
| 127 | options.routing || (isLazyLoadedModuleGen && routingModulePath)
|
---|
| 128 | ? schematics_1.noop()
|
---|
| 129 | : schematics_1.filter((path) => !path.endsWith('-routing.module.ts.template')),
|
---|
| 130 | schematics_1.applyTemplates({
|
---|
| 131 | ...core_1.strings,
|
---|
| 132 | 'if-flat': (s) => (options.flat ? '' : s),
|
---|
| 133 | lazyRoute: isLazyLoadedModuleGen,
|
---|
| 134 | lazyRouteWithoutRouteModule: isLazyLoadedModuleGen && !routingModulePath,
|
---|
| 135 | lazyRouteWithRouteModule: isLazyLoadedModuleGen && !!routingModulePath,
|
---|
| 136 | ...options,
|
---|
| 137 | }),
|
---|
| 138 | schematics_1.move(parsedPath.path),
|
---|
| 139 | ]);
|
---|
| 140 | const moduleDasherized = core_1.strings.dasherize(options.name);
|
---|
| 141 | const modulePath = `${!options.flat ? moduleDasherized + '/' : ''}${moduleDasherized}.module.ts`;
|
---|
| 142 | const componentOptions = {
|
---|
| 143 | module: modulePath,
|
---|
| 144 | flat: options.flat,
|
---|
| 145 | name: options.name,
|
---|
| 146 | path: options.path,
|
---|
| 147 | project: options.project,
|
---|
| 148 | };
|
---|
| 149 | return schematics_1.chain([
|
---|
| 150 | !isLazyLoadedModuleGen ? addDeclarationToNgModule(options) : schematics_1.noop(),
|
---|
| 151 | addRouteDeclarationToNgModule(options, routingModulePath),
|
---|
| 152 | schematics_1.mergeWith(templateSource),
|
---|
| 153 | isLazyLoadedModuleGen ? schematics_1.schematic('component', componentOptions) : schematics_1.noop(),
|
---|
| 154 | options.lintFix ? lint_fix_1.applyLintFix(options.path) : schematics_1.noop(),
|
---|
| 155 | ]);
|
---|
| 156 | };
|
---|
| 157 | }
|
---|
| 158 | exports.default = default_1;
|
---|