Skip to content

Commit

Permalink
Handle file extensions (but why?)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaskello committed Jan 12, 2018
1 parent f6c4a35 commit 847d314
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 46 deletions.
4 changes: 4 additions & 0 deletions src/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ export function fileExistsAsync(
callback2(undefined, stats ? stats.isFile() : false);
});
}

export function removeExtension(path: string): string {
return path.substring(0, path.lastIndexOf(".")) || path;
}
12 changes: 10 additions & 2 deletions src/match-path-async.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as TryPath from "./try-path";
import * as MappingEntry from "./mapping-entry";
import * as Filesystem from "./filesystem";
import { dirname } from "path";

/**
* Function that can match a path async
Expand Down Expand Up @@ -86,7 +87,7 @@ export function matchFromAbsolutePathsAsync(
return afterFilesChecked(new Error("pathsToTry cannot be undefined."));
}
const toTry = pathsToTry[index];
if (toTry.type === "file") {
if (toTry.type === "file" || toTry.type === "index") {
fileExists(toTry.path, (err: Error, exists: boolean) => {
if (err) {
return afterFilesChecked(err);
Expand Down Expand Up @@ -116,7 +117,14 @@ export function matchFromAbsolutePathsAsync(
}
for (let i = 0; i < fileExistsResults.length; i++) {
if (fileExistsResults[i]) {
return callback(undefined, pathsToTry[i].path);
const tryPath = pathsToTry[i];
const result =
tryPath.type === "index"
? dirname(tryPath.path)
: tryPath.type === "file"
? Filesystem.removeExtension(tryPath.path)
: tryPath.path;
return callback(undefined, result);
}
}
return callback();
Expand Down
4 changes: 2 additions & 2 deletions src/try-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { matchStar } from "./match-star";
import { MappingEntry } from "./mapping-entry";

export interface TryPath {
readonly type: "file" | "package";
readonly type: "file" | "index" | "package";
readonly path: string;
}

Expand Down Expand Up @@ -49,7 +49,7 @@ export function getPathsToTry(
const indexPath = path.join(physicalPath, "/index");
pathsToTry.push(
...extensions.map(
e => ({ type: "file", path: indexPath + e } as TryPath)
e => ({ type: "index", path: indexPath + e } as TryPath)
)
);
}
Expand Down
63 changes: 29 additions & 34 deletions test/match-path-async-tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";
import { createMatchPathAsync } from "../src/match-path-async";
import { join } from "path";
import { join, dirname } from "path";

describe("match-path-async", () => {
it("should locate path that matches with star and exists", done => {
Expand All @@ -14,106 +14,103 @@ describe("match-path-async", () => {
(path, callback) => callback(undefined, path === existingPath),
undefined,
(_err, result) => {
assert.equal(result, existingPath);
assert.equal(result, dirname(existingPath));
done();
}
);
});

/*
it("should resolve to correct path when many are specified", () => {
const matchPath = createMatchPath("/root/", {
"lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"]
});
const existingPath = join("/root", "location", "mylib", "index.ts");
const result = matchPath(
"/root/test.ts",
"lib/mylib",
(_: string) => undefined,
(name: string) => name === join("/root", "location", "mylib", "index.ts"),
(name: string) => name === existingPath,
[".ts"]
);
assert.equal(result, join("/root", "location", "mylib"));
assert.equal(result, dirname(existingPath));
});
it("should locate path that matches with star and prioritize pattern with longest prefix", () => {
const matchPath = createMatchPath("/root/", {
"*": ["location/*"],
"lib/*": ["location/*"]
});
const existingPath1 = join("/root", "location", "lib", "mylib", "index.ts");
const existingPath2 = join("/root", "location", "mylib", "index.ts");
const result = matchPath(
"/root/test.ts",
"lib/mylib",
undefined,
(name: string) =>
name === join("/root", "location", "lib", "mylib", "index.ts") ||
name === join("/root", "location", "mylib", "index.ts")
(name: string) => name === existingPath1 || name === existingPath2
);
assert.equal(result, join("/root", "location", "mylib"));
assert.equal(result, dirname(existingPath2));
});
it("should locate path that matches with star and exists with extension", () => {
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
const existingPath = join("/root", "location", "mylib.myext");
const result = matchPath(
"/root/test.ts",
"lib/mylib",
(_: string) => undefined,
(name: string) => name === join("/root", "location", "mylib.myext"),
(name: string) => name === existingPath,
[".js", ".myext"]
);
assert.equal(result, join("/root", "location", "mylib"));
assert.equal(result, removeExtension(existingPath));
});
it("should resolve request with extension specified", () => {
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
const existingPath = join("/root", "location", "test.jpg");
const result = matchPath(
"/root/test.ts",
"lib/test.jpg",
(_: string) => undefined,
(name: string) => name === join("/root", "location", "test.jpg"),
(name: string) => name === existingPath,
[".js", ".myext"]
);
assert.equal(result, join("/root", "location", "test.jpg"));
assert.equal(result, existingPath);
});
it("should locate path that matches without star and exists", () => {
const matchPath = createMatchPath("/root/", {
"lib/foo": ["location/foo"]
});
const existingPath = join("/root", "location", "foo.ts");
const result = matchPath(
"/root/test.ts",
"lib/foo",
(_: string) => undefined,
(name: string) => name === join("/root", "location", "foo.ts")
(name: string) => name === existingPath
);
assert.equal(result, join("/root", "location", "foo"));
assert.equal(result, removeExtension(existingPath));
});
it("should resolve to parent folder when filename is in subfolder", () => {
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
const existingPath = join("/root", "location", "mylib", "index.ts");
const result = matchPath(
"/root/subfolder/file.ts",
"lib/mylib",
(_: string) => undefined,
(name: string) => name === join("/root", "location", "mylib", "index.ts")
(name: string) => name === existingPath
);
assert.equal(result, join("/root", "location", "mylib"));
assert.equal(result, dirname(existingPath));
});
it("should resolve from main field in package.json", () => {
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
const existingPath = join("/root", "location", "mylib", "kalle.ts");
const result = matchPath(
"/root/subfolder/file.ts",
"lib/mylib",
(_: string) => ({ main: "./kalle.ts" }),
(name: string) => name === join("/root", "location", "mylib", "kalle.ts")
(name: string) => name === existingPath
);
assert.equal(result, join("/root", "location", "mylib", "kalle"));
assert.equal(result, removeExtension(existingPath));
});
it("should resolve from main field in package.json and correctly remove file extension", () => {
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
const result = matchPath(
"/root/subfolder/file.js",
"lib/mylib.js",
(_: string) => ({ main: "./kalle.js" }),
(name: string) =>
Expand All @@ -123,7 +120,6 @@ describe("match-path-async", () => {
// Make sure we escape the "."
const result2 = matchPath(
"/root/subfolder/file.js",
"lib/mylibjs",
(_: string) => ({ main: "./kallejs" }),
(name: string) =>
Expand All @@ -137,25 +133,24 @@ describe("match-path-async", () => {
it("should resolve to with the help of baseUrl when not explicitly set", () => {
const matchPath = createMatchPath("/root/", {});
const existingPath = join("/root", "mylib", "index.ts");
const result = matchPath(
"/root/test.ts",
"mylib",
(_: string) => undefined,
(name: string) => name === join("/root", "mylib", "index.ts")
(name: string) => name === existingPath
);
assert.equal(result, "/root/mylib");
assert.equal(result, dirname(existingPath));
});
it("should not locate path that does not match", () => {
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
const existingPath = join("root", "location", "mylib");
const result = matchPath(
"/root/asd.ts",
"mylib",
(_: string) => undefined,
(name: string) => name === join("root", "location", "mylib")
(name: string) => name === existingPath
);
assert.equal(result, undefined);
});
*/
});
5 changes: 1 addition & 4 deletions test/match-path-sync-tests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { assert } from "chai";
import { createMatchPath } from "../src/match-path-sync";
import { join, dirname } from "path";

function removeExtension(path: string): string {
return path.substring(0, path.lastIndexOf(".")) || path;
}
import { removeExtension } from "../src/filesystem";

describe("match-path-sync", () => {
it("should locate path that matches with star and exists", () => {
Expand Down
8 changes: 4 additions & 4 deletions test/try-path-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ describe("mapping-entry", () => {
{ type: "file", path: "/absolute/base/url/foo2/bar.ts" },
{ type: "file", path: "/absolute/base/url/foo2/bar.tsx" },
{ type: "package", path: "/absolute/base/url/foo2/bar/package.json" },
{ type: "file", path: "/absolute/base/url/foo2/bar/index.ts" },
{ type: "file", path: "/absolute/base/url/foo2/bar/index.tsx" },
{ type: "index", path: "/absolute/base/url/foo2/bar/index.ts" },
{ type: "index", path: "/absolute/base/url/foo2/bar/index.tsx" },
// "*"
{ type: "file", path: "/absolute/base/url/foo1" },
{ type: "file", path: "/absolute/base/url/foo1.ts" },
{ type: "file", path: "/absolute/base/url/foo1.tsx" },
{ type: "package", path: "/absolute/base/url/foo1/package.json" },
{ type: "file", path: "/absolute/base/url/foo1/index.ts" },
{ type: "file", path: "/absolute/base/url/foo1/index.tsx" }
{ type: "index", path: "/absolute/base/url/foo1/index.ts" },
{ type: "index", path: "/absolute/base/url/foo1/index.tsx" }
]);
});
});

0 comments on commit 847d314

Please sign in to comment.