source: frontend/node_modules/tsconfig-paths/src/try-path.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[9af201e]1import * as path from "path";
2import { MappingEntry } from "./mapping-entry";
3import { dirname } from "path";
4import { removeExtension } from "./filesystem";
5
6export interface TryPath {
7 readonly type: "file" | "extension" | "index" | "package";
8 readonly path: string;
9}
10
11/**
12 * Builds a list of all physical paths to try by:
13 * 1. Check for file named exactly as request.
14 * 2. Check for files named as request ending in any of the extensions.
15 * 3. Check for file specified in package.json's main property.
16 * 4. Check for files named as request ending in "index" with any of the extensions.
17 */
18export function getPathsToTry(
19 extensions: ReadonlyArray<string>,
20 absolutePathMappings: ReadonlyArray<MappingEntry>,
21 requestedModule: string
22): ReadonlyArray<TryPath> | undefined {
23 if (!absolutePathMappings || !requestedModule || requestedModule[0] === ".") {
24 return undefined;
25 }
26
27 const pathsToTry: Array<TryPath> = [];
28 for (const entry of absolutePathMappings) {
29 const starMatch =
30 entry.pattern === requestedModule
31 ? ""
32 : matchStar(entry.pattern, requestedModule);
33 if (starMatch !== undefined) {
34 for (const physicalPathPattern of entry.paths) {
35 const physicalPath = physicalPathPattern.replace("*", starMatch);
36 pathsToTry.push({ type: "file", path: physicalPath });
37 pathsToTry.push(
38 ...extensions.map(
39 (e) => ({ type: "extension", path: physicalPath + e } as TryPath)
40 )
41 );
42 pathsToTry.push({
43 type: "package",
44 path: path.join(physicalPath, "/package.json"),
45 });
46 const indexPath = path.join(physicalPath, "/index");
47 pathsToTry.push(
48 ...extensions.map(
49 (e) => ({ type: "index", path: indexPath + e } as TryPath)
50 )
51 );
52 }
53 }
54 }
55 return pathsToTry.length === 0 ? undefined : pathsToTry;
56}
57
58// Not sure why we don't just return the full found path?
59export function getStrippedPath(tryPath: TryPath): string {
60 return tryPath.type === "index"
61 ? dirname(tryPath.path)
62 : tryPath.type === "file"
63 ? tryPath.path
64 : tryPath.type === "extension"
65 ? removeExtension(tryPath.path)
66 : tryPath.type === "package"
67 ? tryPath.path
68 : exhaustiveTypeException(tryPath.type);
69}
70
71export function exhaustiveTypeException(check: never): never {
72 throw new Error(`Unknown type ${check}`);
73}
74
75/**
76 * Matches pattern with a single star against search.
77 * Star must match at least one character to be considered a match.
78 * @param patttern for example "foo*"
79 * @param search for example "fooawesomebar"
80 * @returns the part of search that * matches, or undefined if no match.
81 */
82function matchStar(pattern: string, search: string): string | undefined {
83 if (search.length < pattern.length) {
84 return undefined;
85 }
86 if (pattern === "*") {
87 return search;
88 }
89 const star = pattern.indexOf("*");
90 if (star === -1) {
91 return undefined;
92 }
93 const part1 = pattern.substring(0, star);
94 const part2 = pattern.substring(star + 1);
95 if (search.substr(0, star) !== part1) {
96 return undefined;
97 }
98 if (search.substr(search.length - part2.length) !== part2) {
99 return undefined;
100 }
101 return search.substr(star, search.length - part2.length);
102}
Note: See TracBrowser for help on using the repository browser.