Skip to content

Commit

Permalink
Update src/tsconfig-loader.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
  • Loading branch information
F3n67u and MichaelDeBoey committed Apr 16, 2022
1 parent 61ee5bf commit 4a5c0c0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
63 changes: 41 additions & 22 deletions src/__tests__/tsconfig-loader.test.ts
Expand Up @@ -107,47 +107,66 @@ describe("tsconfig-loader", () => {
describe("walkForTsConfig", () => {
it("should find tsconfig in starting directory", () => {
const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
(path) => path === pathToTsconfig
);
// assert.equal(res, pathToTsconfig);
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root/dir1") {
return ["tsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should find jsconfig in starting directory", () => {
const pathToJsconfig = join("/root", "dir1", "jsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
(path) => path === pathToJsconfig
);
// assert.equal(res, pathToTsconfig);
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root/dir1") {
return ["jsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToJsconfig);
});

it("tsconfig.json take precedence over jsconfig.json when both exist", () => {
const pathToTsconfig = join("/root/dir1", "tsconfig.json");
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root/dir1") {
return ["jsconfig.json", "tsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should find tsconfig in parent directory", () => {
const pathToTsconfig = join("/root", "tsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
(path) => path === pathToTsconfig
);
// assert.equal(res, pathToTsconfig);
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root") {
return ["tsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should find jsconfig in parent directory", () => {
const pathToTsconfig = join("/root", "jsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
(path) => path === pathToTsconfig
);
// assert.equal(res, pathToTsconfig);
const res = walkForTsConfig(join("/root", "dir1"), (path) => {
if (path === "/root") {
return ["jsconfig.json"];
} else {
return [];
}
});
expect(res).toBe(pathToTsconfig);
});

it("should return undefined when reaching the top", () => {
const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => false);
// assert.equal(res, undefined);
const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => []);
expect(res).toBeUndefined();
});
});
Expand Down
22 changes: 9 additions & 13 deletions src/tsconfig-loader.ts
Expand Up @@ -90,30 +90,26 @@ function resolveConfigPath(cwd: string, filename?: string): string | undefined {
const configAbsolutePath = walkForTsConfig(cwd);
return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
}

export function walkForTsConfig(
directory: string,
// eslint-disable-next-line no-shadow
existsSync: (path: string) => boolean = fs.existsSync
readdirSync: (path: string) => string[] = fs.readdirSync
): string | undefined {
let configPath = path.join(directory, "./tsconfig.json");
if (existsSync(configPath)) {
return configPath;
}

configPath = path.join(directory, "./jsconfig.json");
if (existsSync(configPath)) {
return configPath;
const files = readdirSync(directory);
const filesToCheck = ["tsconfig.json", "jsconfig.json"];
for (const fileToCheck of filesToCheck) {
if (files.indexOf(fileToCheck) !== -1) {
return path.join(directory, fileToCheck);
}
}

const parentDirectory = path.join(directory, "../");
const parentDirectory = path.dirname(directory);

// If we reached the top
if (directory === parentDirectory) {
return undefined;
}

return walkForTsConfig(parentDirectory, existsSync);
return walkForTsConfig(parentDirectory, readdirSync);
}

export function loadTsconfig(
Expand Down

0 comments on commit 4a5c0c0

Please sign in to comment.