1 | ;
|
2 | /**
|
3 | * @license
|
4 | * Copyright Google LLC All Rights Reserved.
|
5 | *
|
6 | * Use of this source code is governed by an MIT-style license that can be
|
7 | * found in the LICENSE file at https://angular.io/license
|
8 | */
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.AliasHost = void 0;
|
11 | const path_1 = require("../path");
|
12 | const resolver_1 = require("./resolver");
|
13 | /**
|
14 | * A Virtual Host that allow to alias some paths to other paths.
|
15 | *
|
16 | * This does not verify, when setting an alias, that the target or source exist. Neither does it
|
17 | * check whether it's a file or a directory. Please not that directories are also renamed/replaced.
|
18 | *
|
19 | * No recursion is done on the resolution, which means the following is perfectly valid then:
|
20 | *
|
21 | * ```
|
22 | * host.aliases.set(normalize('/file/a'), normalize('/file/b'));
|
23 | * host.aliases.set(normalize('/file/b'), normalize('/file/a'));
|
24 | * ```
|
25 | *
|
26 | * This will result in a proper swap of two files for each others.
|
27 | *
|
28 | * @example
|
29 | * const host = new SimpleMemoryHost();
|
30 | * host.write(normalize('/some/file'), content).subscribe();
|
31 | *
|
32 | * const aHost = new AliasHost(host);
|
33 | * aHost.read(normalize('/some/file'))
|
34 | * .subscribe(x => expect(x).toBe(content));
|
35 | * aHost.aliases.set(normalize('/some/file'), normalize('/other/path');
|
36 | *
|
37 | * // This file will not exist because /other/path does not exist.
|
38 | * aHost.read(normalize('/some/file'))
|
39 | * .subscribe(undefined, err => expect(err.message).toMatch(/does not exist/));
|
40 | *
|
41 | * @example
|
42 | * const host = new SimpleMemoryHost();
|
43 | * host.write(normalize('/some/folder/file'), content).subscribe();
|
44 | *
|
45 | * const aHost = new AliasHost(host);
|
46 | * aHost.read(normalize('/some/folder/file'))
|
47 | * .subscribe(x => expect(x).toBe(content));
|
48 | * aHost.aliases.set(normalize('/some'), normalize('/other');
|
49 | *
|
50 | * // This file will not exist because /other/path does not exist.
|
51 | * aHost.read(normalize('/some/folder/file'))
|
52 | * .subscribe(undefined, err => expect(err.message).toMatch(/does not exist/));
|
53 | *
|
54 | * // Create the file with new content and verify that this has the new content.
|
55 | * aHost.write(normalize('/other/folder/file'), content2).subscribe();
|
56 | * aHost.read(normalize('/some/folder/file'))
|
57 | * .subscribe(x => expect(x).toBe(content2));
|
58 | */
|
59 | class AliasHost extends resolver_1.ResolverHost {
|
60 | constructor() {
|
61 | super(...arguments);
|
62 | this._aliases = new Map();
|
63 | }
|
64 | _resolve(path) {
|
65 | let maybeAlias = this._aliases.get(path);
|
66 | const sp = (0, path_1.split)(path);
|
67 | const remaining = [];
|
68 | // Also resolve all parents of the requested files, only picking the first one that matches.
|
69 | // This can have surprising behaviour when aliases are inside another alias. It will always
|
70 | // use the closest one to the file.
|
71 | while (!maybeAlias && sp.length > 0) {
|
72 | const p = (0, path_1.join)(path_1.NormalizedRoot, ...sp);
|
73 | maybeAlias = this._aliases.get(p);
|
74 | if (maybeAlias) {
|
75 | maybeAlias = (0, path_1.join)(maybeAlias, ...remaining);
|
76 | }
|
77 | // Allow non-null-operator because we know sp.length > 0 (condition on while).
|
78 | remaining.unshift(sp.pop()); // eslint-disable-line @typescript-eslint/no-non-null-assertion
|
79 | }
|
80 | return maybeAlias || path;
|
81 | }
|
82 | get aliases() {
|
83 | return this._aliases;
|
84 | }
|
85 | }
|
86 | exports.AliasHost = AliasHost;
|