Skip to content

Commit 847d314

Browse files
committed
Handle file extensions (but why?)
1 parent f6c4a35 commit 847d314

6 files changed

+50
-46
lines changed

src/filesystem.ts

+4
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ export function fileExistsAsync(
6060
callback2(undefined, stats ? stats.isFile() : false);
6161
});
6262
}
63+
64+
export function removeExtension(path: string): string {
65+
return path.substring(0, path.lastIndexOf(".")) || path;
66+
}

src/match-path-async.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as TryPath from "./try-path";
22
import * as MappingEntry from "./mapping-entry";
33
import * as Filesystem from "./filesystem";
4+
import { dirname } from "path";
45

56
/**
67
* Function that can match a path async
@@ -86,7 +87,7 @@ export function matchFromAbsolutePathsAsync(
8687
return afterFilesChecked(new Error("pathsToTry cannot be undefined."));
8788
}
8889
const toTry = pathsToTry[index];
89-
if (toTry.type === "file") {
90+
if (toTry.type === "file" || toTry.type === "index") {
9091
fileExists(toTry.path, (err: Error, exists: boolean) => {
9192
if (err) {
9293
return afterFilesChecked(err);
@@ -116,7 +117,14 @@ export function matchFromAbsolutePathsAsync(
116117
}
117118
for (let i = 0; i < fileExistsResults.length; i++) {
118119
if (fileExistsResults[i]) {
119-
return callback(undefined, pathsToTry[i].path);
120+
const tryPath = pathsToTry[i];
121+
const result =
122+
tryPath.type === "index"
123+
? dirname(tryPath.path)
124+
: tryPath.type === "file"
125+
? Filesystem.removeExtension(tryPath.path)
126+
: tryPath.path;
127+
return callback(undefined, result);
120128
}
121129
}
122130
return callback();

src/try-path.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { matchStar } from "./match-star";
33
import { MappingEntry } from "./mapping-entry";
44

55
export interface TryPath {
6-
readonly type: "file" | "package";
6+
readonly type: "file" | "index" | "package";
77
readonly path: string;
88
}
99

@@ -49,7 +49,7 @@ export function getPathsToTry(
4949
const indexPath = path.join(physicalPath, "/index");
5050
pathsToTry.push(
5151
...extensions.map(
52-
e => ({ type: "file", path: indexPath + e } as TryPath)
52+
e => ({ type: "index", path: indexPath + e } as TryPath)
5353
)
5454
);
5555
}

test/match-path-async-tests.ts

+29-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert } from "chai";
22
import { createMatchPathAsync } from "../src/match-path-async";
3-
import { join } from "path";
3+
import { join, dirname } from "path";
44

55
describe("match-path-async", () => {
66
it("should locate path that matches with star and exists", done => {
@@ -14,106 +14,103 @@ describe("match-path-async", () => {
1414
(path, callback) => callback(undefined, path === existingPath),
1515
undefined,
1616
(_err, result) => {
17-
assert.equal(result, existingPath);
17+
assert.equal(result, dirname(existingPath));
1818
done();
1919
}
2020
);
2121
});
22-
2322
/*
2423
it("should resolve to correct path when many are specified", () => {
2524
const matchPath = createMatchPath("/root/", {
2625
"lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"]
2726
});
27+
const existingPath = join("/root", "location", "mylib", "index.ts");
2828
const result = matchPath(
29-
"/root/test.ts",
3029
"lib/mylib",
3130
(_: string) => undefined,
32-
(name: string) => name === join("/root", "location", "mylib", "index.ts"),
31+
(name: string) => name === existingPath,
3332
[".ts"]
3433
);
35-
assert.equal(result, join("/root", "location", "mylib"));
34+
assert.equal(result, dirname(existingPath));
3635
});
3736
3837
it("should locate path that matches with star and prioritize pattern with longest prefix", () => {
3938
const matchPath = createMatchPath("/root/", {
4039
"*": ["location/*"],
4140
"lib/*": ["location/*"]
4241
});
42+
const existingPath1 = join("/root", "location", "lib", "mylib", "index.ts");
43+
const existingPath2 = join("/root", "location", "mylib", "index.ts");
4344
const result = matchPath(
44-
"/root/test.ts",
4545
"lib/mylib",
4646
undefined,
47-
(name: string) =>
48-
name === join("/root", "location", "lib", "mylib", "index.ts") ||
49-
name === join("/root", "location", "mylib", "index.ts")
47+
(name: string) => name === existingPath1 || name === existingPath2
5048
);
51-
assert.equal(result, join("/root", "location", "mylib"));
49+
assert.equal(result, dirname(existingPath2));
5250
});
5351
5452
it("should locate path that matches with star and exists with extension", () => {
5553
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
54+
const existingPath = join("/root", "location", "mylib.myext");
5655
const result = matchPath(
57-
"/root/test.ts",
5856
"lib/mylib",
5957
(_: string) => undefined,
60-
(name: string) => name === join("/root", "location", "mylib.myext"),
58+
(name: string) => name === existingPath,
6159
[".js", ".myext"]
6260
);
63-
assert.equal(result, join("/root", "location", "mylib"));
61+
assert.equal(result, removeExtension(existingPath));
6462
});
6563
6664
it("should resolve request with extension specified", () => {
6765
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
66+
const existingPath = join("/root", "location", "test.jpg");
6867
const result = matchPath(
69-
"/root/test.ts",
7068
"lib/test.jpg",
7169
(_: string) => undefined,
72-
(name: string) => name === join("/root", "location", "test.jpg"),
70+
(name: string) => name === existingPath,
7371
[".js", ".myext"]
7472
);
75-
assert.equal(result, join("/root", "location", "test.jpg"));
73+
assert.equal(result, existingPath);
7674
});
7775
7876
it("should locate path that matches without star and exists", () => {
7977
const matchPath = createMatchPath("/root/", {
8078
"lib/foo": ["location/foo"]
8179
});
80+
const existingPath = join("/root", "location", "foo.ts");
8281
const result = matchPath(
83-
"/root/test.ts",
8482
"lib/foo",
8583
(_: string) => undefined,
86-
(name: string) => name === join("/root", "location", "foo.ts")
84+
(name: string) => name === existingPath
8785
);
88-
assert.equal(result, join("/root", "location", "foo"));
86+
assert.equal(result, removeExtension(existingPath));
8987
});
9088
9189
it("should resolve to parent folder when filename is in subfolder", () => {
9290
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
91+
const existingPath = join("/root", "location", "mylib", "index.ts");
9392
const result = matchPath(
94-
"/root/subfolder/file.ts",
9593
"lib/mylib",
9694
(_: string) => undefined,
97-
(name: string) => name === join("/root", "location", "mylib", "index.ts")
95+
(name: string) => name === existingPath
9896
);
99-
assert.equal(result, join("/root", "location", "mylib"));
97+
assert.equal(result, dirname(existingPath));
10098
});
10199
102100
it("should resolve from main field in package.json", () => {
103101
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
102+
const existingPath = join("/root", "location", "mylib", "kalle.ts");
104103
const result = matchPath(
105-
"/root/subfolder/file.ts",
106104
"lib/mylib",
107105
(_: string) => ({ main: "./kalle.ts" }),
108-
(name: string) => name === join("/root", "location", "mylib", "kalle.ts")
106+
(name: string) => name === existingPath
109107
);
110-
assert.equal(result, join("/root", "location", "mylib", "kalle"));
108+
assert.equal(result, removeExtension(existingPath));
111109
});
112110
113111
it("should resolve from main field in package.json and correctly remove file extension", () => {
114112
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
115113
const result = matchPath(
116-
"/root/subfolder/file.js",
117114
"lib/mylib.js",
118115
(_: string) => ({ main: "./kalle.js" }),
119116
(name: string) =>
@@ -123,7 +120,6 @@ describe("match-path-async", () => {
123120
124121
// Make sure we escape the "."
125122
const result2 = matchPath(
126-
"/root/subfolder/file.js",
127123
"lib/mylibjs",
128124
(_: string) => ({ main: "./kallejs" }),
129125
(name: string) =>
@@ -137,25 +133,24 @@ describe("match-path-async", () => {
137133
138134
it("should resolve to with the help of baseUrl when not explicitly set", () => {
139135
const matchPath = createMatchPath("/root/", {});
136+
const existingPath = join("/root", "mylib", "index.ts");
140137
const result = matchPath(
141-
"/root/test.ts",
142138
"mylib",
143139
(_: string) => undefined,
144-
(name: string) => name === join("/root", "mylib", "index.ts")
140+
(name: string) => name === existingPath
145141
);
146-
assert.equal(result, "/root/mylib");
142+
assert.equal(result, dirname(existingPath));
147143
});
148144
149145
it("should not locate path that does not match", () => {
150146
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
147+
const existingPath = join("root", "location", "mylib");
151148
const result = matchPath(
152-
"/root/asd.ts",
153149
"mylib",
154150
(_: string) => undefined,
155-
(name: string) => name === join("root", "location", "mylib")
151+
(name: string) => name === existingPath
156152
);
157153
assert.equal(result, undefined);
158154
});
159-
160155
*/
161156
});

test/match-path-sync-tests.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { assert } from "chai";
22
import { createMatchPath } from "../src/match-path-sync";
33
import { join, dirname } from "path";
4-
5-
function removeExtension(path: string): string {
6-
return path.substring(0, path.lastIndexOf(".")) || path;
7-
}
4+
import { removeExtension } from "../src/filesystem";
85

96
describe("match-path-sync", () => {
107
it("should locate path that matches with star and exists", () => {

test/try-path-tests.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ describe("mapping-entry", () => {
4646
{ type: "file", path: "/absolute/base/url/foo2/bar.ts" },
4747
{ type: "file", path: "/absolute/base/url/foo2/bar.tsx" },
4848
{ type: "package", path: "/absolute/base/url/foo2/bar/package.json" },
49-
{ type: "file", path: "/absolute/base/url/foo2/bar/index.ts" },
50-
{ type: "file", path: "/absolute/base/url/foo2/bar/index.tsx" },
49+
{ type: "index", path: "/absolute/base/url/foo2/bar/index.ts" },
50+
{ type: "index", path: "/absolute/base/url/foo2/bar/index.tsx" },
5151
// "*"
5252
{ type: "file", path: "/absolute/base/url/foo1" },
5353
{ type: "file", path: "/absolute/base/url/foo1.ts" },
5454
{ type: "file", path: "/absolute/base/url/foo1.tsx" },
5555
{ type: "package", path: "/absolute/base/url/foo1/package.json" },
56-
{ type: "file", path: "/absolute/base/url/foo1/index.ts" },
57-
{ type: "file", path: "/absolute/base/url/foo1/index.tsx" }
56+
{ type: "index", path: "/absolute/base/url/foo1/index.ts" },
57+
{ type: "index", path: "/absolute/base/url/foo1/index.tsx" }
5858
]);
5959
});
6060
});

0 commit comments

Comments
 (0)