UNPKG

9.36 kBJavaScriptView Raw
1"use strict";
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.AliasHost = void 0;
11const path_1 = require("../path");
12const 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 */
59class 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}
86exports.AliasHost = AliasHost;
87//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWxpYXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi9wYWNrYWdlcy9hbmd1bGFyX2RldmtpdC9jb3JlL3NyYy92aXJ0dWFsLWZzL2hvc3QvYWxpYXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBRUgsa0NBQTBFO0FBQzFFLHlDQUEwQztBQUUxQzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBNkNHO0FBQ0gsTUFBYSxTQUFzQyxTQUFRLHVCQUFvQjtJQUEvRTs7UUFDWSxhQUFRLEdBQUcsSUFBSSxHQUFHLEVBQWMsQ0FBQztJQTJCN0MsQ0FBQztJQXpCVyxRQUFRLENBQUMsSUFBVTtRQUMzQixJQUFJLFVBQVUsR0FBRyxJQUFJLENBQUMsUUFBUSxDQUFDLEdBQUcsQ0FBQyxJQUFJLENBQUMs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