UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2exports.__esModule = true;
3exports.buildRelativePath = exports.findComponent = exports.findComponentFromOptions = 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 component referred by a set of options passed to the schematics.
14 */
15function 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}
44exports.findComponentFromOptions = findComponentFromOptions;
45/**
46 * Function to find the "closest" component to a generated file's path.
47 */
48function 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}
65exports.findComponent = findComponent;
66/**
67 * Build a relative path from one file path to another file path.
68 */
69function 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}
82exports.buildRelativePath = buildRelativePath;
83function 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 * Strips the typescript extension and clears index filenames
95 * foo.ts -> foo
96 * index.ts -> empty
97 */
98function convertToTypeScriptFileName(filename) {
99 return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
100}
101//# sourceMappingURL=find-component.js.map
\No newline at end of file