index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.dev/license
  8. */
  9. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || (function () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. exports.default = default_1;
  44. const schematics_1 = require("@angular-devkit/schematics");
  45. const posix_1 = require("node:path/posix");
  46. const ts = __importStar(require("../third_party/github.com/Microsoft/TypeScript/lib/typescript"));
  47. const ast_utils_1 = require("../utility/ast-utils");
  48. const change_1 = require("../utility/change");
  49. const find_module_1 = require("../utility/find-module");
  50. const parse_name_1 = require("../utility/parse-name");
  51. const validation_1 = require("../utility/validation");
  52. const workspace_1 = require("../utility/workspace");
  53. const schema_1 = require("./schema");
  54. function buildRelativeModulePath(options, modulePath) {
  55. const importModulePath = (0, posix_1.join)(options.path ?? '', options.flat ? '' : schematics_1.strings.dasherize(options.name), schematics_1.strings.dasherize(options.name) + options.typeSeparator + 'module');
  56. return (0, find_module_1.buildRelativePath)(modulePath, importModulePath);
  57. }
  58. function addImportToNgModule(options) {
  59. return (host) => {
  60. if (!options.module) {
  61. return host;
  62. }
  63. const modulePath = options.module;
  64. const sourceText = host.readText(modulePath);
  65. const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
  66. const relativePath = buildRelativeModulePath(options, modulePath);
  67. const changes = (0, ast_utils_1.addImportToModule)(source, modulePath, schematics_1.strings.classify(`${options.name}Module`), relativePath);
  68. const recorder = host.beginUpdate(modulePath);
  69. for (const change of changes) {
  70. if (change instanceof change_1.InsertChange) {
  71. recorder.insertLeft(change.pos, change.toAdd);
  72. }
  73. }
  74. host.commitUpdate(recorder);
  75. return host;
  76. };
  77. }
  78. function addRouteDeclarationToNgModule(options, routingModulePath) {
  79. return (host) => {
  80. if (!options.route) {
  81. return host;
  82. }
  83. if (!options.module) {
  84. throw new Error('Module option required when creating a lazy loaded routing module.');
  85. }
  86. let path;
  87. if (routingModulePath) {
  88. path = routingModulePath;
  89. }
  90. else {
  91. path = options.module;
  92. }
  93. const sourceText = host.readText(path);
  94. const addDeclaration = (0, ast_utils_1.addRouteDeclarationToModule)(ts.createSourceFile(path, sourceText, ts.ScriptTarget.Latest, true), path, buildRoute(options, options.module));
  95. const recorder = host.beginUpdate(path);
  96. recorder.insertLeft(addDeclaration.pos, addDeclaration.toAdd);
  97. host.commitUpdate(recorder);
  98. return host;
  99. };
  100. }
  101. function getRoutingModulePath(host, modulePath) {
  102. const routingModulePath = modulePath.endsWith(find_module_1.ROUTING_MODULE_EXT) || modulePath.endsWith('-routing-module.ts')
  103. ? modulePath
  104. : modulePath
  105. .replace(find_module_1.MODULE_EXT, find_module_1.ROUTING_MODULE_EXT)
  106. .replace('-module.ts', '-routing-module.ts');
  107. return host.exists(routingModulePath) ? routingModulePath : undefined;
  108. }
  109. function buildRoute(options, modulePath) {
  110. const relativeModulePath = buildRelativeModulePath(options, modulePath);
  111. const moduleName = `${schematics_1.strings.classify(options.name)}Module`;
  112. const loadChildren = `() => import('${relativeModulePath}').then(m => m.${moduleName})`;
  113. return `{ path: '${options.route}', loadChildren: ${loadChildren} }`;
  114. }
  115. function default_1(options) {
  116. return async (host) => {
  117. if (options.path === undefined) {
  118. options.path = await (0, workspace_1.createDefaultPath)(host, options.project);
  119. }
  120. if (options.module) {
  121. try {
  122. options.module = (0, find_module_1.findModuleFromOptions)(host, options);
  123. }
  124. catch {
  125. options.module = (0, find_module_1.findModuleFromOptions)(host, {
  126. ...options,
  127. moduleExt: '-module.ts',
  128. routingModuleExt: '-routing-module.ts',
  129. });
  130. }
  131. }
  132. let routingModulePath;
  133. const isLazyLoadedModuleGen = !!(options.route && options.module);
  134. if (isLazyLoadedModuleGen) {
  135. options.routingScope = schema_1.RoutingScope.Child;
  136. routingModulePath = getRoutingModulePath(host, options.module);
  137. }
  138. const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
  139. options.name = parsedPath.name;
  140. options.path = parsedPath.path;
  141. (0, validation_1.validateClassName)(schematics_1.strings.classify(options.name));
  142. const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
  143. options.routing || (isLazyLoadedModuleGen && routingModulePath)
  144. ? (0, schematics_1.noop)()
  145. : (0, schematics_1.filter)((path) => !path.includes('-routing')),
  146. (0, schematics_1.applyTemplates)({
  147. ...schematics_1.strings,
  148. 'if-flat': (s) => (options.flat ? '' : s),
  149. lazyRoute: isLazyLoadedModuleGen,
  150. lazyRouteWithoutRouteModule: isLazyLoadedModuleGen && !routingModulePath,
  151. lazyRouteWithRouteModule: isLazyLoadedModuleGen && !!routingModulePath,
  152. ...options,
  153. }),
  154. (0, schematics_1.move)(parsedPath.path),
  155. ]);
  156. const moduleDasherized = schematics_1.strings.dasherize(options.name);
  157. const modulePath = `${!options.flat ? moduleDasherized + '/' : ''}${moduleDasherized}${options.typeSeparator}module.ts`;
  158. const componentOptions = {
  159. module: modulePath,
  160. flat: options.flat,
  161. name: options.name,
  162. path: options.path,
  163. project: options.project,
  164. standalone: false,
  165. };
  166. return (0, schematics_1.chain)([
  167. !isLazyLoadedModuleGen ? addImportToNgModule(options) : (0, schematics_1.noop)(),
  168. addRouteDeclarationToNgModule(options, routingModulePath),
  169. (0, schematics_1.mergeWith)(templateSource),
  170. isLazyLoadedModuleGen ? (0, schematics_1.schematic)('component', componentOptions) : (0, schematics_1.noop)(),
  171. ]);
  172. };
  173. }