1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.buildRelativePath = exports.findModule = exports.findModuleFromOptions = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var core_1 = require("@angular-devkit/core");
|
12 |
|
13 |
|
14 |
|
15 | function 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 (0, core_1.normalize)(findModule(host, pathToCheck));
|
23 | }
|
24 | else {
|
25 | var modulePath = (0, core_1.normalize)('/' + options.path + '/' + options.module);
|
26 | var moduleBaseName = (0, core_1.normalize)(modulePath).split('/').pop();
|
27 | if (host.exists(modulePath)) {
|
28 | return (0, core_1.normalize)(modulePath);
|
29 | }
|
30 | else if (host.exists(modulePath + '.ts')) {
|
31 | return (0, core_1.normalize)(modulePath + '.ts');
|
32 | }
|
33 | else if (host.exists(modulePath + '.module.ts')) {
|
34 | return (0, core_1.normalize)(modulePath + '.module.ts');
|
35 | }
|
36 | else if (host.exists(modulePath + '/' + moduleBaseName + '.module.ts')) {
|
37 | return (0, core_1.normalize)(modulePath + '/' + moduleBaseName + '.module.ts');
|
38 | }
|
39 | else {
|
40 | throw new Error("Specified module path ".concat(modulePath, " does not exist"));
|
41 | }
|
42 | }
|
43 | }
|
44 | exports.findModuleFromOptions = findModuleFromOptions;
|
45 |
|
46 |
|
47 |
|
48 | function 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 (0, 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 | }
|
66 | exports.findModule = findModule;
|
67 |
|
68 |
|
69 |
|
70 | function 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 = (0, core_1.relative)(fromDirectory, toDirectory);
|
74 | var fixedRelativePath = relativePath.startsWith('.')
|
75 | ? relativePath
|
76 | : "./".concat(relativePath);
|
77 | return !toFileName || toFileName === 'index.ts'
|
78 | ? fixedRelativePath
|
79 | : "".concat(fixedRelativePath.endsWith('/')
|
80 | ? fixedRelativePath
|
81 | : fixedRelativePath + '/').concat(convertToTypeScriptFileName(toFileName));
|
82 | }
|
83 | exports.buildRelativePath = buildRelativePath;
|
84 | function parsePath(path) {
|
85 | var pathNormalized = (0, core_1.normalize)(path);
|
86 | var filename = (0, core_1.extname)(pathNormalized) ? (0, core_1.basename)(pathNormalized) : '';
|
87 | var directory = filename ? (0, core_1.dirname)(pathNormalized) : pathNormalized;
|
88 | return {
|
89 | path: pathNormalized,
|
90 | filename: filename,
|
91 | directory: directory,
|
92 | };
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function convertToTypeScriptFileName(filename) {
|
100 | return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
|
101 | }
|
102 |
|
\ | No newline at end of file |