UNPKG

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