find-module.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. exports.MODULE_EXT = '.module.ts';
  12. exports.ROUTING_MODULE_EXT = '-routing.module.ts';
  13. /**
  14. * Find the module referred by a set of options passed to the schematics.
  15. */
  16. function findModuleFromOptions(host, options) {
  17. if (options.hasOwnProperty('skipImport') && options.skipImport) {
  18. return undefined;
  19. }
  20. const moduleExt = options.moduleExt || exports.MODULE_EXT;
  21. const routingModuleExt = options.routingModuleExt || exports.ROUTING_MODULE_EXT;
  22. if (!options.module) {
  23. const pathToCheck = (options.path || '') + '/' + options.name;
  24. return core_1.normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
  25. }
  26. else {
  27. const modulePath = core_1.normalize(`/${options.path}/${options.module}`);
  28. const componentPath = core_1.normalize(`/${options.path}/${options.name}`);
  29. const moduleBaseName = core_1.normalize(modulePath).split('/').pop();
  30. const candidateSet = new Set([
  31. core_1.normalize(options.path || '/'),
  32. ]);
  33. for (let dir = modulePath; dir != core_1.NormalizedRoot; dir = core_1.dirname(dir)) {
  34. candidateSet.add(dir);
  35. }
  36. for (let dir = componentPath; dir != core_1.NormalizedRoot; dir = core_1.dirname(dir)) {
  37. candidateSet.add(dir);
  38. }
  39. const candidatesDirs = [...candidateSet].sort((a, b) => b.length - a.length);
  40. for (const c of candidatesDirs) {
  41. const candidateFiles = [
  42. '',
  43. `${moduleBaseName}.ts`,
  44. `${moduleBaseName}${moduleExt}`,
  45. ].map(x => core_1.join(c, x));
  46. for (const sc of candidateFiles) {
  47. if (host.exists(sc)) {
  48. return core_1.normalize(sc);
  49. }
  50. }
  51. }
  52. throw new Error(`Specified module '${options.module}' does not exist.\n`
  53. + `Looked in the following directories:\n ${candidatesDirs.join('\n ')}`);
  54. }
  55. }
  56. exports.findModuleFromOptions = findModuleFromOptions;
  57. /**
  58. * Function to find the "closest" module to a generated file's path.
  59. */
  60. function findModule(host, generateDir, moduleExt = exports.MODULE_EXT, routingModuleExt = exports.ROUTING_MODULE_EXT) {
  61. let dir = host.getDir('/' + generateDir);
  62. let foundRoutingModule = false;
  63. while (dir) {
  64. const allMatches = dir.subfiles.filter(p => p.endsWith(moduleExt));
  65. const filteredMatches = allMatches.filter(p => !p.endsWith(routingModuleExt));
  66. foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length;
  67. if (filteredMatches.length == 1) {
  68. return core_1.join(dir.path, filteredMatches[0]);
  69. }
  70. else if (filteredMatches.length > 1) {
  71. throw new Error('More than one module matches. Use skip-import option to skip importing '
  72. + 'the component into the closest module.');
  73. }
  74. dir = dir.parent;
  75. }
  76. const errorMsg = foundRoutingModule ? 'Could not find a non Routing NgModule.'
  77. + `\nModules with suffix '${routingModuleExt}' are strictly reserved for routing.`
  78. + '\nUse the skip-import option to skip importing in NgModule.'
  79. : 'Could not find an NgModule. Use the skip-import option to skip importing in NgModule.';
  80. throw new Error(errorMsg);
  81. }
  82. exports.findModule = findModule;
  83. /**
  84. * Build a relative path from one file path to another file path.
  85. */
  86. function buildRelativePath(from, to) {
  87. from = core_1.normalize(from);
  88. to = core_1.normalize(to);
  89. // Convert to arrays.
  90. const fromParts = from.split('/');
  91. const toParts = to.split('/');
  92. // Remove file names (preserving destination)
  93. fromParts.pop();
  94. const toFileName = toParts.pop();
  95. const relativePath = core_1.relative(core_1.normalize(fromParts.join('/') || '/'), core_1.normalize(toParts.join('/') || '/'));
  96. let pathPrefix = '';
  97. // Set the path prefix for same dir or child dir, parent dir starts with `..`
  98. if (!relativePath) {
  99. pathPrefix = '.';
  100. }
  101. else if (!relativePath.startsWith('.')) {
  102. pathPrefix = `./`;
  103. }
  104. if (pathPrefix && !pathPrefix.endsWith('/')) {
  105. pathPrefix += '/';
  106. }
  107. return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
  108. }
  109. exports.buildRelativePath = buildRelativePath;