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 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.buildRelativePath = exports.findModule = exports.findModuleFromOptions = exports.ROUTING_MODULE_EXT = exports.MODULE_EXT = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | exports.MODULE_EXT = '.module.ts';
|
---|
13 | exports.ROUTING_MODULE_EXT = '-routing.module.ts';
|
---|
14 | /**
|
---|
15 | * Find the module referred by a set of options passed to the schematics.
|
---|
16 | */
|
---|
17 | function findModuleFromOptions(host, options) {
|
---|
18 | // eslint-disable-next-line no-prototype-builtins
|
---|
19 | if (options.hasOwnProperty('skipImport') && options.skipImport) {
|
---|
20 | return undefined;
|
---|
21 | }
|
---|
22 | const moduleExt = options.moduleExt || exports.MODULE_EXT;
|
---|
23 | const routingModuleExt = options.routingModuleExt || exports.ROUTING_MODULE_EXT;
|
---|
24 | if (!options.module) {
|
---|
25 | const pathToCheck = (options.path || '') + '/' + options.name;
|
---|
26 | return core_1.normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
|
---|
27 | }
|
---|
28 | else {
|
---|
29 | const modulePath = core_1.normalize(`/${options.path}/${options.module}`);
|
---|
30 | const componentPath = core_1.normalize(`/${options.path}/${options.name}`);
|
---|
31 | const moduleBaseName = core_1.normalize(modulePath).split('/').pop();
|
---|
32 | const candidateSet = new Set([core_1.normalize(options.path || '/')]);
|
---|
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 the skip-import option to skip importing ' +
|
---|
72 | 'the component into the closest module or use the module option to specify a module.');
|
---|
73 | }
|
---|
74 | dir = dir.parent;
|
---|
75 | }
|
---|
76 | const errorMsg = foundRoutingModule
|
---|
77 | ? '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 | }
|
---|
83 | exports.findModule = findModule;
|
---|
84 | /**
|
---|
85 | * Build a relative path from one file path to another file path.
|
---|
86 | */
|
---|
87 | function 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 | }
|
---|
110 | exports.buildRelativePath = buildRelativePath;
|
---|