UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2exports.__esModule = true;
3exports.buildRelativePath = exports.findModule = exports.findModuleFromOptions = 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 */
11var core_1 = require("@angular-devkit/core");
12/**
13 * Find the module referred by a set of options passed to the schematics.
14 */
15function findModuleFromOptions(host, options) {
16 if (options.hasOwnProperty('skipImport') && options.skipImport) {
17 return undefined;
18 }
19 if (!options.module) {
20 var pathToCheck = (options.path || '') +
21 (options.flat ? '' : '/' + core_1.strings.dasherize(options.name));
22 return core_1.normalize(findModule(host, pathToCheck));
23 }
24 else {
25 var modulePath = core_1.normalize('/' + options.path + '/' + options.module);
26 var moduleBaseName = core_1.normalize(modulePath).split('/').pop();
27 if (host.exists(modulePath)) {
28 return core_1.normalize(modulePath);
29 }
30 else if (host.exists(modulePath + '.ts')) {
31 return core_1.normalize(modulePath + '.ts');
32 }
33 else if (host.exists(modulePath + '.module.ts')) {
34 return core_1.normalize(modulePath + '.module.ts');
35 }
36 else if (host.exists(modulePath + '/' + moduleBaseName + '.module.ts')) {
37 return core_1.normalize(modulePath + '/' + moduleBaseName + '.module.ts');
38 }
39 else {
40 throw new Error("Specified module path " + modulePath + " does not exist");
41 }
42 }
43}
44exports.findModuleFromOptions = findModuleFromOptions;
45/**
46 * Function to find the "closest" module to a generated file's path.
47 */
48function findModule(host, generateDir) {
49 var dir = host.getDir('/' + generateDir);
50 var moduleRe = /\.module\.ts$/;
51 var routingModuleRe = /-routing\.module\.ts/;
52 while (dir) {
53 var matches = dir.subfiles.filter(function (p) { return moduleRe.test(p) && !routingModuleRe.test(p); });
54 if (matches.length == 1) {
55 return core_1.join(dir.path, matches[0]);
56 }
57 else if (matches.length > 1) {
58 throw new Error('More than one module matches. Use skip-import option to skip importing ' +
59 'the component into the closest module.');
60 }
61 dir = dir.parent;
62 }
63 throw new Error('Could not find an NgModule. Use the skip-import ' +
64 'option to skip importing in NgModule.');
65}
66exports.findModule = findModule;
67/**
68 * Build a relative path from one file path to another file path.
69 */
70function buildRelativePath(from, to) {
71 var _a = parsePath(from), fromPath = _a.path, fromFileName = _a.filename, fromDirectory = _a.directory;
72 var _b = parsePath(to), toPath = _b.path, toFileName = _b.filename, toDirectory = _b.directory;
73 var relativePath = core_1.relative(fromDirectory, toDirectory);
74 var fixedRelativePath = relativePath.startsWith('.')
75 ? relativePath
76 : "./" + relativePath;
77 return !toFileName || toFileName === 'index.ts'
78 ? fixedRelativePath
79 : "" + (fixedRelativePath.endsWith('/')
80 ? fixedRelativePath
81 : fixedRelativePath + '/') + convertToTypeScriptFileName(toFileName);
82}
83exports.buildRelativePath = buildRelativePath;
84function parsePath(path) {
85 var pathNormalized = core_1.normalize(path);
86 var filename = core_1.extname(pathNormalized) ? core_1.basename(pathNormalized) : '';
87 var directory = filename ? core_1.dirname(pathNormalized) : pathNormalized;
88 return {
89 path: pathNormalized,
90 filename: filename,
91 directory: directory
92 };
93}
94/**
95 * Strips the typescript extension and clears index filenames
96 * foo.ts -> foo
97 * index.ts -> empty
98 */
99function convertToTypeScriptFileName(filename) {
100 return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
101}
102//# sourceMappingURL=find-module.js.map
\No newline at end of file