index.js 6.1 KB

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