Skip to content

Commit

Permalink
Traverse up to find first tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
kulshekhar committed Oct 6, 2017
1 parent 63f85f3 commit 2c034bc
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/utils.ts
Expand Up @@ -113,9 +113,34 @@ function readCompilerOptions(configPath: string) {
return parsedConfig.options;
}

function getPathToClosestTSConfig(
startDir?: string,
previousDir?: string,
): string {
// Starting with the startDir directory and moving on to the
// parent directory recursively (going no further than the root directory)
// find and return the path to the first encountered tsconfig.json file

if (!startDir) {
return getPathToClosestTSConfig('.');
}

const tsConfigPath = path.join(startDir, 'tsconfig.json');

const startDirPath = path.resolve(startDir);
const previousDirPath = path.resolve(previousDir || '/');

if (startDirPath === previousDirPath || fs.existsSync(tsConfigPath)) {
return tsConfigPath;
}

return getPathToClosestTSConfig(path.join(startDir, '..'), startDir);
}

export function getTSConfigOptionFromConfig(globals: any) {
const defaultTSConfigFile = getPathToClosestTSConfig();
if (!globals) {
return 'tsconfig.json';
return defaultTSConfigFile;
}

const tsJestConfig = getTSJestConfig(globals);
Expand All @@ -129,7 +154,7 @@ More information at https://github.com/kulshekhar/ts-jest#tsconfig`);
return tsJestConfig.tsConfigFile;
}

return 'tsconfig.json';
return defaultTSConfigFile;
}

export function mockGlobalTSConfigSchema(globals: any) {
Expand Down

0 comments on commit 2c034bc

Please sign in to comment.