1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const pathlib = require("path");
|
17 | const path_1 = require("path");
|
18 | const pathIsInside = require("path-is-inside");
|
19 | const url_1 = require("url");
|
20 | const utils_1 = require("../core/utils");
|
21 | const fs_url_resolver_1 = require("./fs-url-resolver");
|
22 |
|
23 |
|
24 |
|
25 | class PackageUrlResolver extends fs_url_resolver_1.FsUrlResolver {
|
26 | constructor(options = {}) {
|
27 | super(options.packageDir, options.host, options.protocol);
|
28 | this.componentDir = options.componentDir || 'bower_components/';
|
29 | this.resolvedComponentDir =
|
30 | pathlib.join(this.packageDir, this.componentDir);
|
31 | }
|
32 | modifyFsPath(path) {
|
33 |
|
34 |
|
35 | const parentOfPackageDir = pathlib.dirname(this.packageDir);
|
36 | if (pathIsInside(path, parentOfPackageDir) &&
|
37 | !pathIsInside(path, this.packageDir)) {
|
38 | path = pathlib.join(this.packageDir, this.componentDir, path.substring(parentOfPackageDir.length));
|
39 | }
|
40 | return path;
|
41 | }
|
42 | relative(fromOrTo, maybeTo, _kind) {
|
43 | const [from, to] = (maybeTo !== undefined) ? [fromOrTo, maybeTo] :
|
44 | [this.packageUrl, fromOrTo];
|
45 | const result = this.relativeImpl(from, to);
|
46 | if (maybeTo === undefined) {
|
47 | return this.brandAsPackageRelative(result);
|
48 | }
|
49 | else {
|
50 | return result;
|
51 | }
|
52 | }
|
53 | relativeImpl(from, to) {
|
54 | const pathnameInComponentDir = this.pathnameForComponentDirUrl(to);
|
55 |
|
56 |
|
57 | if (pathnameInComponentDir !== undefined) {
|
58 |
|
59 |
|
60 | if (this.pathnameForComponentDirUrl(from) === undefined) {
|
61 |
|
62 |
|
63 | const componentDirPath = pathnameInComponentDir.slice(this.resolvedComponentDir.length);
|
64 | const reresolved = this.simpleUrlResolve(this.packageUrl, ('../' + componentDirPath), this.protocol);
|
65 | if (reresolved !== undefined) {
|
66 | const reresolvedUrl = utils_1.parseUrl(reresolved);
|
67 | const toUrl = utils_1.parseUrl(to);
|
68 | reresolvedUrl.search = toUrl.search;
|
69 | reresolvedUrl.hash = toUrl.hash;
|
70 | to = this.brandAsResolved(url_1.format(reresolvedUrl));
|
71 | }
|
72 | }
|
73 | }
|
74 | return super.relative(from, to);
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 | pathnameForComponentDirUrl(resolvedUrl) {
|
82 | const url = utils_1.parseUrl(resolvedUrl);
|
83 | if (this.shouldHandleAsFileUrl(url) && url.pathname) {
|
84 | let pathname;
|
85 | try {
|
86 | pathname = path_1.posix.normalize(decodeURIComponent(url.pathname));
|
87 | }
|
88 | catch (_a) {
|
89 | return undefined;
|
90 | }
|
91 | const path = this.filesystemPathForPathname(pathname);
|
92 | if (path && pathIsInside(path, this.resolvedComponentDir)) {
|
93 | return path;
|
94 | }
|
95 | }
|
96 | return undefined;
|
97 | }
|
98 | }
|
99 | exports.PackageUrlResolver = PackageUrlResolver;
|
100 |
|
\ | No newline at end of file |