Backend half
This commit is contained in:
Generated
Vendored
+101
@@ -0,0 +1,101 @@
|
||||
import {
|
||||
configLoader,
|
||||
loadConfig,
|
||||
ConfigLoaderFailResult,
|
||||
ConfigLoaderSuccessResult,
|
||||
} from "../config-loader";
|
||||
import { join } from "path";
|
||||
|
||||
describe("config-loader", (): void => {
|
||||
it("should use explicitParams when set", () => {
|
||||
const result = configLoader({
|
||||
explicitParams: {
|
||||
baseUrl: "/foo/bar",
|
||||
paths: {
|
||||
asd: ["asd"],
|
||||
},
|
||||
},
|
||||
cwd: "/baz",
|
||||
});
|
||||
|
||||
const successResult = result as ConfigLoaderSuccessResult;
|
||||
expect(successResult.resultType).toBe("success");
|
||||
expect(successResult.absoluteBaseUrl).toBe("/foo/bar");
|
||||
expect(successResult.paths["asd"][0]).toBe("asd");
|
||||
});
|
||||
|
||||
it("should use explicitParams when set and add cwd when path is relative", () => {
|
||||
const result = configLoader({
|
||||
explicitParams: {
|
||||
baseUrl: "bar/",
|
||||
paths: {
|
||||
asd: ["asd"],
|
||||
},
|
||||
},
|
||||
cwd: "/baz",
|
||||
});
|
||||
|
||||
const successResult = result as ConfigLoaderSuccessResult;
|
||||
expect(successResult.resultType).toBe("success");
|
||||
expect(successResult.absoluteBaseUrl).toBe(join("/baz", "bar/"));
|
||||
});
|
||||
|
||||
it("should fallback to tsConfigLoader when explicitParams is not set", () => {
|
||||
const result = configLoader({
|
||||
explicitParams: undefined,
|
||||
cwd: "/baz",
|
||||
tsConfigLoader: () => ({
|
||||
tsConfigPath: "/baz/tsconfig.json",
|
||||
baseUrl: "./src",
|
||||
paths: {},
|
||||
}),
|
||||
});
|
||||
|
||||
const successResult = result as ConfigLoaderSuccessResult;
|
||||
expect(successResult.resultType).toBe("success");
|
||||
expect(successResult.absoluteBaseUrl).toBe(join("/baz", "src"));
|
||||
});
|
||||
|
||||
it("should tolerate a missing baseUrl", () => {
|
||||
const result = configLoader({
|
||||
explicitParams: undefined,
|
||||
cwd: "/baz",
|
||||
tsConfigLoader: () => ({
|
||||
tsConfigPath: "/baz/tsconfig.json",
|
||||
baseUrl: undefined,
|
||||
paths: {},
|
||||
}),
|
||||
});
|
||||
|
||||
const failResult = result as ConfigLoaderFailResult;
|
||||
expect(failResult.resultType).toBe("success");
|
||||
});
|
||||
|
||||
it("should presume cwd to be a tsconfig file when loadConfig is called with absolute path to tsconfig.json", () => {
|
||||
// using tsconfig-named.json to ensure that future changes to fix
|
||||
// https://github.com/dividab/tsconfig-paths/issues/31
|
||||
// do not pass this test case just because of a directory walk looking
|
||||
// for tsconfig.json
|
||||
const configFile = join(__dirname, "tsconfig-named.json");
|
||||
const result = loadConfig(configFile);
|
||||
|
||||
const successResult = result as ConfigLoaderSuccessResult;
|
||||
expect(successResult.resultType).toBe("success");
|
||||
expect(successResult.configFileAbsolutePath).toBe(configFile);
|
||||
});
|
||||
|
||||
it("should allow an absolute baseUrl in tsconfig.json", () => {
|
||||
const result = configLoader({
|
||||
explicitParams: undefined,
|
||||
cwd: "/baz",
|
||||
tsConfigLoader: () => ({
|
||||
tsConfigPath: "/baz/tsconfig.json",
|
||||
baseUrl: "/baz",
|
||||
paths: {},
|
||||
}),
|
||||
});
|
||||
|
||||
const successResult = result as ConfigLoaderSuccessResult;
|
||||
expect(successResult.absoluteBaseUrl).toEqual("/baz");
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+230
@@ -0,0 +1,230 @@
|
||||
import { join, dirname } from "path";
|
||||
import { removeExtension } from "../../filesystem";
|
||||
|
||||
export interface OneTest {
|
||||
readonly name: string;
|
||||
readonly only?: boolean;
|
||||
readonly skip?: boolean;
|
||||
readonly absoluteBaseUrl: string;
|
||||
readonly paths: { [key: string]: Array<string> };
|
||||
readonly mainFields?: (string | string[])[];
|
||||
readonly addMatchAll?: boolean;
|
||||
readonly existingFiles: ReadonlyArray<string>;
|
||||
readonly requestedModule: string;
|
||||
readonly extensions: ReadonlyArray<string>;
|
||||
readonly packageJson?: {};
|
||||
readonly expectedPath: string | undefined;
|
||||
}
|
||||
|
||||
const defaultExtensionsWhenRunningInTsNode = [
|
||||
".js",
|
||||
".json",
|
||||
".node",
|
||||
".ts",
|
||||
".tsx",
|
||||
];
|
||||
|
||||
export const tests: ReadonlyArray<OneTest> = [
|
||||
{
|
||||
name: "should locate path that matches with star and exists",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {
|
||||
"lib/*": ["location/*"],
|
||||
},
|
||||
existingFiles: [join("/root", "location", "mylib", "index.ts")],
|
||||
requestedModule: "lib/mylib",
|
||||
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should resolve to correct path when many are specified",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {
|
||||
"lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"],
|
||||
},
|
||||
existingFiles: [join("/root", "location", "mylib", "index.ts")],
|
||||
requestedModule: "lib/mylib",
|
||||
extensions: [".ts"],
|
||||
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
|
||||
},
|
||||
{
|
||||
name:
|
||||
"should locate path that matches with star and prioritize pattern with longest prefix",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {
|
||||
"*": ["location/*"],
|
||||
"lib/*": ["location/*"],
|
||||
},
|
||||
existingFiles: [
|
||||
join("/root", "location", "lib", "mylib", "index.ts"),
|
||||
join("/root", "location", "mylib", "index.ts"),
|
||||
],
|
||||
requestedModule: "lib/mylib",
|
||||
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should locate path that matches with star and exists with extension",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [join("/root", "location", "mylib.myext")],
|
||||
requestedModule: "lib/mylib",
|
||||
extensions: [".js", ".myext"],
|
||||
expectedPath: removeExtension(join("/root", "location", "mylib.myext")),
|
||||
},
|
||||
{
|
||||
name: "should resolve request with extension specified",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [join("/root", "location", "test.jpg")],
|
||||
requestedModule: "lib/test.jpg",
|
||||
expectedPath: join("/root", "location", "test.jpg"),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should locate path that matches without star and exists",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {
|
||||
"lib/foo": ["location/foo"],
|
||||
},
|
||||
existingFiles: [join("/root", "location", "foo.ts")],
|
||||
requestedModule: "lib/foo",
|
||||
expectedPath: removeExtension(join("/root", "location", "foo.ts")),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should resolve to parent folder when filename is in subfolder",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [join("/root", "location", "mylib", "index.ts")],
|
||||
requestedModule: "lib/mylib",
|
||||
expectedPath: dirname(join("/root", "location", "mylib", "index.ts")),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should resolve from main field in package.json",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [join("/root", "location", "mylib", "kalle.ts")],
|
||||
packageJson: { main: "./kalle.ts" },
|
||||
requestedModule: "lib/mylib",
|
||||
expectedPath: join("/root", "location", "mylib", "kalle.ts"),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should resolve from main field in package.json (js)",
|
||||
absoluteBaseUrl: "/root",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [join("/root", "location", "mylib.js", "kalle.js")],
|
||||
packageJson: { main: "./kalle.js" },
|
||||
requestedModule: "lib/mylib.js",
|
||||
extensions: [".ts", ".js"],
|
||||
expectedPath: join("/root", "location", "mylib.js", "kalle.js"),
|
||||
},
|
||||
{
|
||||
name: "should resolve from list of fields by priority in package.json",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
mainFields: ["missing", "browser", "main"],
|
||||
packageJson: { main: "./main.js", browser: "./browser.js" },
|
||||
existingFiles: [
|
||||
join("/root", "location", "mylibjs", "main.js"), // mainFilePath
|
||||
join("/root", "location", "mylibjs", "browser.js"), // browserFilePath
|
||||
],
|
||||
extensions: [".ts", ".js"],
|
||||
requestedModule: "lib/mylibjs",
|
||||
expectedPath: join("/root", "location", "mylibjs", "browser.js"),
|
||||
},
|
||||
{
|
||||
name: "should ignore field mappings to missing files in package.json",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
mainFields: ["browser", "main"],
|
||||
existingFiles: [join("/root", "location", "mylibjs", "kalle.js")],
|
||||
requestedModule: "lib/mylibjs",
|
||||
packageJson: {
|
||||
main: "./kalle.js",
|
||||
browser: "./nope.js",
|
||||
},
|
||||
extensions: [".ts", ".js"],
|
||||
expectedPath: join("/root", "location", "mylibjs", "kalle.js"),
|
||||
},
|
||||
{
|
||||
name: "should resolve nested main fields",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
mainFields: [["esnext", "main"]],
|
||||
packageJson: { esnext: { main: "./main.js" } },
|
||||
existingFiles: [join("/root", "location", "mylibjs", "main.js")],
|
||||
extensions: [".ts", ".js"],
|
||||
requestedModule: "lib/mylibjs",
|
||||
expectedPath: join("/root", "location", "mylibjs", "main.js"),
|
||||
},
|
||||
{
|
||||
name: "should ignore advanced field mappings in package.json",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [
|
||||
join("/root", "location", "mylibjs", "kalle.js"),
|
||||
join("/root", "location", "mylibjs", "browser.js"),
|
||||
],
|
||||
requestedModule: "lib/mylibjs",
|
||||
packageJson: {
|
||||
main: "./kalle.js",
|
||||
browser: { mylibjs: "./browser.js", "./kalle.js": "./browser.js" },
|
||||
},
|
||||
extensions: [".ts", ".js"],
|
||||
expectedPath: join("/root", "location", "mylibjs", "kalle.js"),
|
||||
},
|
||||
{
|
||||
name: "should resolve to with the help of baseUrl when not explicitly set",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {},
|
||||
existingFiles: [join("/root", "mylib", "index.ts")],
|
||||
requestedModule: "mylib",
|
||||
expectedPath: dirname(join("/root", "mylib", "index.ts")),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should not resolve with the help of baseUrl when asked not to",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {},
|
||||
addMatchAll: false,
|
||||
existingFiles: [join("/root", "mylib", "index.ts")],
|
||||
requestedModule: "mylib",
|
||||
expectedPath: undefined,
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should not locate path that does not match",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: { "lib/*": ["location/*"] },
|
||||
existingFiles: [join("root", "location", "mylib")],
|
||||
requestedModule: "mylib",
|
||||
expectedPath: undefined,
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should not resolve typings file (index.d.ts)",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {
|
||||
"lib/*": ["location/*"],
|
||||
},
|
||||
existingFiles: [join("/root", "location", "mylib", "index.d.ts")],
|
||||
requestedModule: "lib/mylib",
|
||||
expectedPath: undefined,
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
{
|
||||
name: "should resolve main file with cjs file extension",
|
||||
absoluteBaseUrl: "/root/",
|
||||
paths: {},
|
||||
existingFiles: [join("/root", "mylib", "index.cjs")],
|
||||
packageJson: {
|
||||
main: "./index.cjs",
|
||||
},
|
||||
requestedModule: "mylib",
|
||||
expectedPath: join("/root", "mylib", "index.cjs"),
|
||||
extensions: defaultExtensionsWhenRunningInTsNode,
|
||||
},
|
||||
];
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
import * as Filesystem from "../filesystem";
|
||||
import * as path from "path";
|
||||
|
||||
describe("filesystem", () => {
|
||||
const fileThatExists = path.join(__dirname, "../../package.json");
|
||||
const fileThatNotExists = path.join(__dirname, "../../package2.json");
|
||||
|
||||
it("should find file that exists, sync", () => {
|
||||
const result = Filesystem.fileExistsSync(fileThatExists);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should not find file that not exists, sync", () => {
|
||||
const result = Filesystem.fileExistsSync(fileThatNotExists);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should find file that exists, async", (done) => {
|
||||
Filesystem.fileExistsAsync(fileThatExists, (_err, result) => {
|
||||
try {
|
||||
expect(result).toBe(true);
|
||||
done();
|
||||
} catch (error) {
|
||||
done(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should not find file that not exists, async", (done) => {
|
||||
Filesystem.fileExistsAsync(fileThatNotExists, (_err, result) => {
|
||||
try {
|
||||
expect(result).toBe(false);
|
||||
done();
|
||||
} catch (error) {
|
||||
done(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should load json, sync", () => {
|
||||
const result = Filesystem.readJsonFromDiskSync(fileThatExists);
|
||||
expect(result);
|
||||
expect(result.main).toBe("lib/index.js");
|
||||
});
|
||||
|
||||
it("should load json, async", (done) => {
|
||||
Filesystem.readJsonFromDiskAsync(fileThatExists, (_err, result) => {
|
||||
try {
|
||||
expect(result).toBeTruthy();
|
||||
expect(result.main).toBe("lib/index.js");
|
||||
done();
|
||||
} catch (error) {
|
||||
done(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
import { getAbsoluteMappingEntries } from "../mapping-entry";
|
||||
import { join } from "path";
|
||||
|
||||
describe("mapping-entry", () => {
|
||||
it("should change to absolute paths and sort in longest prefix order", () => {
|
||||
const result = getAbsoluteMappingEntries(
|
||||
"/absolute/base/url",
|
||||
{
|
||||
"*": ["/foo1", "./foo2"],
|
||||
"longest/pre/fix/*": ["./foo2/bar"],
|
||||
"pre/fix/*": ["/foo3"],
|
||||
},
|
||||
true
|
||||
);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
pattern: "longest/pre/fix/*",
|
||||
paths: [join("/absolute", "base", "url", "foo2", "bar")],
|
||||
},
|
||||
{
|
||||
pattern: "pre/fix/*",
|
||||
paths: [join("/foo3")],
|
||||
},
|
||||
{
|
||||
pattern: "*",
|
||||
paths: [join("/foo1"), join("/absolute", "base", "url", "foo2")],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should should add a match-all pattern when requested", () => {
|
||||
let result = getAbsoluteMappingEntries("/absolute/base/url", {}, true);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
pattern: "*",
|
||||
paths: [join("/absolute", "base", "url", "*")],
|
||||
},
|
||||
]);
|
||||
|
||||
result = getAbsoluteMappingEntries("/absolute/base/url", {}, false);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
import { createMatchPathAsync } from "../match-path-async";
|
||||
import * as Tests from "./data/match-path-data";
|
||||
|
||||
describe("match-path-async", () => {
|
||||
Tests.tests.forEach((t) =>
|
||||
it(t.name, (done) => {
|
||||
const matchPath = createMatchPathAsync(
|
||||
t.absoluteBaseUrl,
|
||||
t.paths,
|
||||
t.mainFields,
|
||||
t.addMatchAll
|
||||
);
|
||||
matchPath(
|
||||
t.requestedModule,
|
||||
(_path, callback) => callback(undefined, t.packageJson),
|
||||
(path, callback) =>
|
||||
callback(undefined, t.existingFiles.indexOf(path) !== -1),
|
||||
t.extensions,
|
||||
(_err, result) => {
|
||||
expect(result).toBe(t.expectedPath);
|
||||
done();
|
||||
}
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
Generated
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
import { createMatchPath } from "../match-path-sync";
|
||||
import * as Tests from "./data/match-path-data";
|
||||
|
||||
describe("match-path-sync", () => {
|
||||
Tests.tests.forEach((t) =>
|
||||
it(t.name, () => {
|
||||
const matchPath = createMatchPath(
|
||||
t.absoluteBaseUrl,
|
||||
t.paths,
|
||||
t.mainFields,
|
||||
t.addMatchAll
|
||||
);
|
||||
const result = matchPath(
|
||||
t.requestedModule,
|
||||
(_: string) => t.packageJson,
|
||||
(name: string) => t.existingFiles.indexOf(name) !== -1,
|
||||
t.extensions
|
||||
);
|
||||
expect(result).toBe(t.expectedPath);
|
||||
})
|
||||
);
|
||||
});
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
import { getPathsToTry } from "../try-path";
|
||||
import { join } from "path";
|
||||
|
||||
describe("mapping-entry", () => {
|
||||
const abosolutePathMappings = [
|
||||
{
|
||||
pattern: "longest/pre/fix/*",
|
||||
paths: [join("/absolute", "base", "url", "foo2", "bar")],
|
||||
},
|
||||
{ pattern: "pre/fix/*", paths: [join("/absolute", "base", "url", "foo3")] },
|
||||
{ pattern: "*", paths: [join("/absolute", "base", "url", "foo1")] },
|
||||
];
|
||||
const abosolutePathMappingsStarstWithSlash = [
|
||||
{
|
||||
pattern: "/opt/*",
|
||||
paths: [join("/absolute", "src", "aws-layer")],
|
||||
},
|
||||
{
|
||||
pattern: "*",
|
||||
paths: [join("/absolute", "src")],
|
||||
},
|
||||
];
|
||||
it("should return no paths for relative requested module", () => {
|
||||
const result = getPathsToTry(
|
||||
[".ts", "tsx"],
|
||||
abosolutePathMappings,
|
||||
"./requested-module"
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return no paths if no pattern match the requested module", () => {
|
||||
const result = getPathsToTry(
|
||||
[".ts", "tsx"],
|
||||
[
|
||||
{
|
||||
pattern: "longest/pre/fix/*",
|
||||
paths: [join("/absolute", "base", "url", "foo2", "bar")],
|
||||
},
|
||||
{
|
||||
pattern: "pre/fix/*",
|
||||
paths: [join("/absolute", "base", "url", "foo3")],
|
||||
},
|
||||
],
|
||||
"requested-module"
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should get all paths that matches requested module", () => {
|
||||
const result = getPathsToTry(
|
||||
[".ts", ".tsx"],
|
||||
abosolutePathMappings,
|
||||
"longest/pre/fix/requested-module"
|
||||
);
|
||||
expect(result).toEqual([
|
||||
// "longest/pre/fix/*"
|
||||
{ type: "file", path: join("/absolute", "base", "url", "foo2", "bar") },
|
||||
{
|
||||
type: "extension",
|
||||
path: join("/absolute", "base", "url", "foo2", "bar.ts"),
|
||||
},
|
||||
{
|
||||
type: "extension",
|
||||
path: join("/absolute", "base", "url", "foo2", "bar.tsx"),
|
||||
},
|
||||
{
|
||||
type: "package",
|
||||
path: join("/absolute", "base", "url", "foo2", "bar", "package.json"),
|
||||
},
|
||||
{
|
||||
type: "index",
|
||||
path: join("/absolute", "base", "url", "foo2", "bar", "index.ts"),
|
||||
},
|
||||
{
|
||||
type: "index",
|
||||
path: join("/absolute", "base", "url", "foo2", "bar", "index.tsx"),
|
||||
},
|
||||
// "*"
|
||||
{ type: "file", path: join("/absolute", "base", "url", "foo1") },
|
||||
{ type: "extension", path: join("/absolute", "base", "url", "foo1.ts") },
|
||||
{ type: "extension", path: join("/absolute", "base", "url", "foo1.tsx") },
|
||||
{
|
||||
type: "package",
|
||||
path: join("/absolute", "base", "url", "foo1", "package.json"),
|
||||
},
|
||||
{
|
||||
type: "index",
|
||||
path: join("/absolute", "base", "url", "foo1", "index.ts"),
|
||||
},
|
||||
{
|
||||
type: "index",
|
||||
path: join("/absolute", "base", "url", "foo1", "index.tsx"),
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should resolve paths starting with a slash", () => {
|
||||
const result = getPathsToTry(
|
||||
[".ts"],
|
||||
abosolutePathMappingsStarstWithSlash,
|
||||
"/opt/utils"
|
||||
);
|
||||
expect(result).toEqual([
|
||||
// "opt/*"
|
||||
{
|
||||
path: join("/absolute", "src", "aws-layer"),
|
||||
type: "file",
|
||||
},
|
||||
{
|
||||
path: join("/absolute", "src", "aws-layer.ts"),
|
||||
type: "extension",
|
||||
},
|
||||
{
|
||||
path: join("/absolute", "src", "aws-layer", "package.json"),
|
||||
type: "package",
|
||||
},
|
||||
{
|
||||
path: join("/absolute", "src", "aws-layer", "index.ts"),
|
||||
type: "index",
|
||||
},
|
||||
// "*"
|
||||
{
|
||||
path: join("/absolute", "src"),
|
||||
type: "file",
|
||||
},
|
||||
{
|
||||
path: join("/absolute", "src.ts"),
|
||||
type: "extension",
|
||||
},
|
||||
{
|
||||
path: join("/absolute", "src", "package.json"),
|
||||
type: "package",
|
||||
},
|
||||
{
|
||||
path: join("/absolute", "src", "index.ts"),
|
||||
type: "index",
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+428
@@ -0,0 +1,428 @@
|
||||
import {
|
||||
loadTsconfig,
|
||||
tsConfigLoader,
|
||||
walkForTsConfig,
|
||||
} from "../tsconfig-loader";
|
||||
import { join } from "path";
|
||||
|
||||
describe("tsconfig-loader", () => {
|
||||
it("should find tsconfig in cwd", () => {
|
||||
const result = tsConfigLoader({
|
||||
cwd: "/foo/bar",
|
||||
getEnv: (_: string) => undefined,
|
||||
loadSync: (cwd: string) => {
|
||||
return {
|
||||
tsConfigPath: `${cwd}/tsconfig.json`,
|
||||
baseUrl: "./",
|
||||
paths: {},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.tsConfigPath).toBe("/foo/bar/tsconfig.json");
|
||||
});
|
||||
|
||||
it("should return loaderResult.tsConfigPath as undefined when not found", () => {
|
||||
const result = tsConfigLoader({
|
||||
cwd: "/foo/bar",
|
||||
getEnv: (_: string) => undefined,
|
||||
loadSync: (_: string) => {
|
||||
return {
|
||||
tsConfigPath: undefined,
|
||||
baseUrl: "./",
|
||||
paths: {},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.tsConfigPath).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should use TS_NODE_PROJECT env if exists", () => {
|
||||
const result = tsConfigLoader({
|
||||
cwd: "/foo/bar",
|
||||
getEnv: (key: string) =>
|
||||
key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined,
|
||||
loadSync: (cwd: string, fileName: string) => {
|
||||
if (cwd === "/foo/bar" && fileName === "/foo/baz") {
|
||||
return {
|
||||
tsConfigPath: "/foo/baz/tsconfig.json",
|
||||
baseUrl: "./",
|
||||
paths: {},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
tsConfigPath: undefined,
|
||||
baseUrl: "./",
|
||||
paths: {},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.tsConfigPath).toBe("/foo/baz/tsconfig.json");
|
||||
});
|
||||
|
||||
it("should use TS_NODE_BASEURL env if exists", () => {
|
||||
const result = tsConfigLoader({
|
||||
cwd: "/foo/bar",
|
||||
getEnv: (key: string) =>
|
||||
key === "TS_NODE_BASEURL" ? "SOME_BASEURL" : undefined,
|
||||
loadSync: (_0: string, _1: string, baseUrl: string) => {
|
||||
return {
|
||||
tsConfigPath: undefined,
|
||||
baseUrl,
|
||||
paths: {},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.baseUrl).toBe("SOME_BASEURL");
|
||||
});
|
||||
|
||||
it("should not use TS_NODE_BASEURL env if it does not exist", () => {
|
||||
const result = tsConfigLoader({
|
||||
cwd: "/foo/bar",
|
||||
getEnv: (_: string) => {
|
||||
return undefined;
|
||||
},
|
||||
loadSync: (_0: string, _1: string, baseUrl: string) => {
|
||||
return {
|
||||
tsConfigPath: undefined,
|
||||
baseUrl,
|
||||
paths: {},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.baseUrl).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("walkForTsConfig", () => {
|
||||
it("should find tsconfig in starting directory", () => {
|
||||
const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
|
||||
const mockFiles: Record<string, string[]> = {
|
||||
"/root/dir1": ["tsconfig.json"],
|
||||
};
|
||||
const res = walkForTsConfig(
|
||||
join("/root", "dir1"),
|
||||
(path) => mockFiles[path] || []
|
||||
);
|
||||
expect(res).toBe(pathToTsconfig);
|
||||
});
|
||||
|
||||
it("should find jsconfig in starting directory", () => {
|
||||
const pathToJsconfig = join("/root", "dir1", "jsconfig.json");
|
||||
const mockFiles: Record<string, string[]> = {
|
||||
"/root/dir1": ["jsconfig.json"],
|
||||
};
|
||||
const res = walkForTsConfig(
|
||||
join("/root", "dir1"),
|
||||
(path) => mockFiles[path] || []
|
||||
);
|
||||
expect(res).toBe(pathToJsconfig);
|
||||
});
|
||||
|
||||
// see https://github.com/Microsoft/TypeScript/issues/15869#issuecomment-301845650
|
||||
it("tsconfig.json take precedence over jsconfig.json when both exist", () => {
|
||||
const pathToTsconfig = join("/root/dir1", "tsconfig.json");
|
||||
const mockFiles: Record<string, string[]> = {
|
||||
"/root/dir1": ["jsconfig.json", "tsconfig.json"],
|
||||
};
|
||||
const res = walkForTsConfig(
|
||||
join("/root", "dir1"),
|
||||
(path) => mockFiles[path] || []
|
||||
);
|
||||
expect(res).toBe(pathToTsconfig);
|
||||
});
|
||||
|
||||
it("should find tsconfig in parent directory", () => {
|
||||
const pathToTsconfig = join("/root", "tsconfig.json");
|
||||
const mockFiles: Record<string, string[]> = {
|
||||
"/root": ["tsconfig.json"],
|
||||
};
|
||||
const res = walkForTsConfig(
|
||||
join("/root", "dir1"),
|
||||
(path) => mockFiles[path] || []
|
||||
);
|
||||
expect(res).toBe(pathToTsconfig);
|
||||
});
|
||||
|
||||
it("should find jsconfig in parent directory", () => {
|
||||
const pathToTsconfig = join("/root", "jsconfig.json");
|
||||
const mockFiles: Record<string, string[]> = {
|
||||
"/root": ["jsconfig.json"],
|
||||
};
|
||||
const res = walkForTsConfig(
|
||||
join("/root", "dir1"),
|
||||
(path) => mockFiles[path] || []
|
||||
);
|
||||
expect(res).toBe(pathToTsconfig);
|
||||
});
|
||||
|
||||
it("should return undefined when reaching the top", () => {
|
||||
const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => []);
|
||||
expect(res).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadConfig", () => {
|
||||
it("should load a config", () => {
|
||||
const config = { compilerOptions: { baseUrl: "hej" } };
|
||||
const res = loadTsconfig(
|
||||
"/root/dir1/tsconfig.json",
|
||||
(path) => path === "/root/dir1/tsconfig.json",
|
||||
(_) => JSON.stringify(config)
|
||||
);
|
||||
expect(res).toStrictEqual(config);
|
||||
});
|
||||
|
||||
it("should load a config with comments", () => {
|
||||
const config = { compilerOptions: { baseUrl: "hej" } };
|
||||
const res = loadTsconfig(
|
||||
"/root/dir1/tsconfig.json",
|
||||
(path) => path === "/root/dir1/tsconfig.json",
|
||||
(_) => `{
|
||||
// my comment
|
||||
"compilerOptions": {
|
||||
"baseUrl": "hej"
|
||||
}
|
||||
}`
|
||||
);
|
||||
expect(res).toStrictEqual(config);
|
||||
});
|
||||
|
||||
it("should load a config with trailing commas", () => {
|
||||
const config = { compilerOptions: { baseUrl: "hej" } };
|
||||
const res = loadTsconfig(
|
||||
"/root/dir1/tsconfig.json",
|
||||
(path) => path === "/root/dir1/tsconfig.json",
|
||||
(_) => `{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "hej",
|
||||
},
|
||||
}`
|
||||
);
|
||||
expect(res).toStrictEqual(config);
|
||||
});
|
||||
|
||||
it("should throw an error including the file path when encountering invalid JSON5", () => {
|
||||
expect(() =>
|
||||
loadTsconfig(
|
||||
"/root/dir1/tsconfig.json",
|
||||
(path) => path === "/root/dir1/tsconfig.json",
|
||||
(_) => `{
|
||||
"compilerOptions": {
|
||||
}`
|
||||
)
|
||||
).toThrowError(
|
||||
"/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12"
|
||||
);
|
||||
});
|
||||
|
||||
it("should load a config with string extends and overwrite all options", () => {
|
||||
const firstConfig = {
|
||||
extends: "../base-config.json",
|
||||
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
|
||||
};
|
||||
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
|
||||
const baseConfig = {
|
||||
compilerOptions: {
|
||||
baseUrl: "olle",
|
||||
paths: { foo: ["bar1"] },
|
||||
strict: true,
|
||||
},
|
||||
};
|
||||
const baseConfigPath = join("/root", "base-config.json");
|
||||
const res = loadTsconfig(
|
||||
join("/root", "dir1", "tsconfig.json"),
|
||||
(path) => path === firstConfigPath || path === baseConfigPath,
|
||||
(path) => {
|
||||
if (path === firstConfigPath) {
|
||||
return JSON.stringify(firstConfig);
|
||||
}
|
||||
if (path === baseConfigPath) {
|
||||
return JSON.stringify(baseConfig);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
);
|
||||
|
||||
expect(res).toEqual({
|
||||
extends: "../base-config.json",
|
||||
compilerOptions: {
|
||||
baseUrl: "kalle",
|
||||
paths: { foo: ["bar2"] },
|
||||
strict: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should load a config with string extends from node_modules and overwrite all options", () => {
|
||||
const firstConfig = {
|
||||
extends: "my-package/base-config.json",
|
||||
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
|
||||
};
|
||||
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
|
||||
const baseConfig = {
|
||||
compilerOptions: {
|
||||
baseUrl: "olle",
|
||||
paths: { foo: ["bar1"] },
|
||||
strict: true,
|
||||
},
|
||||
};
|
||||
const baseConfigPath = join(
|
||||
"/root",
|
||||
"dir1",
|
||||
"node_modules",
|
||||
"my-package",
|
||||
"base-config.json"
|
||||
);
|
||||
const res = loadTsconfig(
|
||||
join("/root", "dir1", "tsconfig.json"),
|
||||
(path) => path === firstConfigPath || path === baseConfigPath,
|
||||
(path) => {
|
||||
if (path === firstConfigPath) {
|
||||
return JSON.stringify(firstConfig);
|
||||
}
|
||||
if (path === baseConfigPath) {
|
||||
return JSON.stringify(baseConfig);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
);
|
||||
|
||||
expect(res).toEqual({
|
||||
extends: "my-package/base-config.json",
|
||||
compilerOptions: {
|
||||
baseUrl: "kalle",
|
||||
paths: { foo: ["bar2"] },
|
||||
strict: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should use baseUrl relative to location of extended tsconfig", () => {
|
||||
const firstConfig = { compilerOptions: { baseUrl: "." } };
|
||||
const firstConfigPath = join("/root", "first-config.json");
|
||||
const secondConfig = { extends: "../first-config.json" };
|
||||
const secondConfigPath = join("/root", "dir1", "second-config.json");
|
||||
const thirdConfig = { extends: "../second-config.json" };
|
||||
const thirdConfigPath = join("/root", "dir1", "dir2", "third-config.json");
|
||||
const res = loadTsconfig(
|
||||
join("/root", "dir1", "dir2", "third-config.json"),
|
||||
(path) =>
|
||||
path === firstConfigPath ||
|
||||
path === secondConfigPath ||
|
||||
path === thirdConfigPath,
|
||||
(path) => {
|
||||
if (path === firstConfigPath) {
|
||||
return JSON.stringify(firstConfig);
|
||||
}
|
||||
if (path === secondConfigPath) {
|
||||
return JSON.stringify(secondConfig);
|
||||
}
|
||||
if (path === thirdConfigPath) {
|
||||
return JSON.stringify(thirdConfig);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
);
|
||||
|
||||
expect(res).toEqual({
|
||||
extends: "../second-config.json",
|
||||
compilerOptions: { baseUrl: join("..", "..") },
|
||||
});
|
||||
});
|
||||
|
||||
it("should load a config with array extends and overwrite all options", () => {
|
||||
const baseConfig1 = {
|
||||
compilerOptions: { baseUrl: ".", paths: { foo: ["bar"] } },
|
||||
};
|
||||
const baseConfig1Path = join("/root", "base-config-1.json");
|
||||
const baseConfig2 = { compilerOptions: { baseUrl: "." } };
|
||||
const baseConfig2Path = join("/root", "dir1", "base-config-2.json");
|
||||
const baseConfig3 = {
|
||||
compilerOptions: { baseUrl: ".", paths: { foo: ["bar2"] } },
|
||||
};
|
||||
const baseConfig3Path = join("/root", "dir1", "dir2", "base-config-3.json");
|
||||
const actualConfig = {
|
||||
extends: [
|
||||
"./base-config-1.json",
|
||||
"./dir1/base-config-2.json",
|
||||
"./dir1/dir2/base-config-3.json",
|
||||
],
|
||||
};
|
||||
const actualConfigPath = join("/root", "tsconfig.json");
|
||||
|
||||
const res = loadTsconfig(
|
||||
join("/root", "tsconfig.json"),
|
||||
(path) =>
|
||||
[
|
||||
baseConfig1Path,
|
||||
baseConfig2Path,
|
||||
baseConfig3Path,
|
||||
actualConfigPath,
|
||||
].indexOf(path) >= 0,
|
||||
(path) => {
|
||||
if (path === baseConfig1Path) {
|
||||
return JSON.stringify(baseConfig1);
|
||||
}
|
||||
if (path === baseConfig2Path) {
|
||||
return JSON.stringify(baseConfig2);
|
||||
}
|
||||
if (path === baseConfig3Path) {
|
||||
return JSON.stringify(baseConfig3);
|
||||
}
|
||||
if (path === actualConfigPath) {
|
||||
return JSON.stringify(actualConfig);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
);
|
||||
|
||||
expect(res).toEqual({
|
||||
extends: [
|
||||
"./base-config-1.json",
|
||||
"./dir1/base-config-2.json",
|
||||
"./dir1/dir2/base-config-3.json",
|
||||
],
|
||||
compilerOptions: {
|
||||
baseUrl: join("dir1", "dir2"),
|
||||
paths: { foo: ["bar2"] },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should load a config with array extends without .json extension", () => {
|
||||
const baseConfig = {
|
||||
compilerOptions: { baseUrl: ".", paths: { foo: ["bar"] } },
|
||||
};
|
||||
const baseConfigPath = join("/root", "base-config-1.json");
|
||||
const actualConfig = { extends: ["./base-config-1"] };
|
||||
const actualConfigPath = join("/root", "tsconfig.json");
|
||||
|
||||
const res = loadTsconfig(
|
||||
join("/root", "tsconfig.json"),
|
||||
(path) => [baseConfigPath, actualConfigPath].indexOf(path) >= 0,
|
||||
(path) => {
|
||||
if (path === baseConfigPath) {
|
||||
return JSON.stringify(baseConfig);
|
||||
}
|
||||
if (path === actualConfigPath) {
|
||||
return JSON.stringify(actualConfig);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
);
|
||||
|
||||
expect(res).toEqual({
|
||||
extends: ["./base-config-1"],
|
||||
compilerOptions: {
|
||||
baseUrl: ".",
|
||||
paths: { foo: ["bar"] },
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "../base-tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"sourceMap": true,
|
||||
"outDir": "./js_out"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user