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 45640b8 commit 50c79f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
50 changes: 27 additions & 23 deletions src/__tests__/tsconfig-loader.test.ts
Expand Up @@ -107,47 +107,51 @@ 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("should find tsconfig in parent directory", () => {
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.only("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: 10 additions & 12 deletions src/tsconfig-loader.ts
Expand Up @@ -90,29 +90,27 @@ function resolveConfigPath(cwd: string, filename?: string): string | undefined {
const configAbsolutePath = walkForTsConfig(cwd);
return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
}

export function walkForTsConfig(
directory: string,
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;
console.log("directory", directory);
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 50c79f7

Please sign in to comment.