1 | "use strict";
|
2 | exports.__esModule = true;
|
3 | exports.buildRelativePath = exports.findComponent = exports.findComponentFromOptions = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var core_1 = require("@angular-devkit/core");
|
12 |
|
13 |
|
14 |
|
15 | function findComponentFromOptions(host, options) {
|
16 | if (options.hasOwnProperty('skipImport') && options.skipImport) {
|
17 | return undefined;
|
18 | }
|
19 | if (!options.component) {
|
20 | var pathToCheck = (options.path || '') +
|
21 | (options.flat ? '' : '/' + core_1.strings.dasherize(options.name));
|
22 | return core_1.normalize(findComponent(host, pathToCheck));
|
23 | }
|
24 | else {
|
25 | var componentPath = core_1.normalize('/' + options.path + '/' + options.component);
|
26 | var componentBaseName = core_1.normalize(componentPath).split('/').pop();
|
27 | if (host.exists(componentPath)) {
|
28 | return core_1.normalize(componentPath);
|
29 | }
|
30 | else if (host.exists(componentPath + '.ts')) {
|
31 | return core_1.normalize(componentPath + '.ts');
|
32 | }
|
33 | else if (host.exists(componentPath + '.component.ts')) {
|
34 | return core_1.normalize(componentPath + '.component.ts');
|
35 | }
|
36 | else if (host.exists(componentPath + '/' + componentBaseName + '.component.ts')) {
|
37 | return core_1.normalize(componentPath + '/' + componentBaseName + '.component.ts');
|
38 | }
|
39 | else {
|
40 | throw new Error("Specified component path " + componentPath + " does not exist");
|
41 | }
|
42 | }
|
43 | }
|
44 | exports.findComponentFromOptions = findComponentFromOptions;
|
45 |
|
46 |
|
47 |
|
48 | function findComponent(host, generateDir) {
|
49 | var dir = host.getDir('/' + generateDir);
|
50 | var componentRe = /\.component\.ts$/;
|
51 | while (dir) {
|
52 | var matches = dir.subfiles.filter(function (p) { return componentRe.test(p); });
|
53 | if (matches.length == 1) {
|
54 | return core_1.join(dir.path, matches[0]);
|
55 | }
|
56 | else if (matches.length > 1) {
|
57 | throw new Error('More than one component matches. Use skip-import option to skip importing ' +
|
58 | 'the component store into the closest component.');
|
59 | }
|
60 | dir = dir.parent;
|
61 | }
|
62 | throw new Error('Could not find an Component. Use the skip-import ' +
|
63 | 'option to skip importing in Component.');
|
64 | }
|
65 | exports.findComponent = findComponent;
|
66 |
|
67 |
|
68 |
|
69 | function buildRelativePath(from, to) {
|
70 | var _a = parsePath(from), fromPath = _a.path, fromFileName = _a.filename, fromDirectory = _a.directory;
|
71 | var _b = parsePath(to), toPath = _b.path, toFileName = _b.filename, toDirectory = _b.directory;
|
72 | var relativePath = core_1.relative(fromDirectory, toDirectory);
|
73 | var fixedRelativePath = relativePath.startsWith('.')
|
74 | ? relativePath
|
75 | : "./" + relativePath;
|
76 | return !toFileName || toFileName === 'index.ts'
|
77 | ? fixedRelativePath
|
78 | : "" + (fixedRelativePath.endsWith('/')
|
79 | ? fixedRelativePath
|
80 | : fixedRelativePath + '/') + convertToTypeScriptFileName(toFileName);
|
81 | }
|
82 | exports.buildRelativePath = buildRelativePath;
|
83 | function parsePath(path) {
|
84 | var pathNormalized = core_1.normalize(path);
|
85 | var filename = core_1.extname(pathNormalized) ? core_1.basename(pathNormalized) : '';
|
86 | var directory = filename ? core_1.dirname(pathNormalized) : pathNormalized;
|
87 | return {
|
88 | path: pathNormalized,
|
89 | filename: filename,
|
90 | directory: directory
|
91 | };
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function convertToTypeScriptFileName(filename) {
|
99 | return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
|
100 | }
|
101 |
|
\ | No newline at end of file |