From 9f52b0c8a7f54aa311ff214dc8231c3a4f51ea5a Mon Sep 17 00:00:00 2001 From: Amod Malviya Date: Sat, 31 Mar 2018 12:00:11 +0530 Subject: [PATCH] Fixes #31: Problem when using a custom configFile name --- src/tsconfig-loader.ts | 4 ++++ test/config-loader-tests.ts | 14 ++++++++++++++ test/tsconfig-named.json | 10 ++++++++++ 3 files changed, 28 insertions(+) create mode 100644 test/tsconfig-named.json diff --git a/src/tsconfig-loader.ts b/src/tsconfig-loader.ts index 48f0b73..d0bd2bc 100644 --- a/src/tsconfig-loader.ts +++ b/src/tsconfig-loader.ts @@ -73,6 +73,10 @@ export function resolveConfigPath( return absolutePath; } + if (fs.statSync(cwd).isFile()) { + return path.resolve(cwd); + } + const configAbsolutePath = walkForTsConfig(cwd); return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined; } diff --git a/test/config-loader-tests.ts b/test/config-loader-tests.ts index 6dbb17c..483ba43 100644 --- a/test/config-loader-tests.ts +++ b/test/config-loader-tests.ts @@ -1,6 +1,7 @@ import { assert } from "chai"; import { configLoader, + loadConfig, ConfigLoaderFailResult, ConfigLoaderSuccessResult } from "../src/config-loader"; @@ -73,4 +74,17 @@ describe("config-loader", (): void => { assert.equal(failResult.resultType, "failed"); assert.isTrue(failResult.message.indexOf("baseUrl") > -1); }); + + 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; + assert.equal(successResult.resultType, "success"); + assert.equal(successResult.configFileAbsolutePath, configFile); + }); }); diff --git a/test/tsconfig-named.json b/test/tsconfig-named.json new file mode 100644 index 0000000..a337580 --- /dev/null +++ b/test/tsconfig-named.json @@ -0,0 +1,10 @@ +{ + "extends": "../base-tsconfig.json", + "compilerOptions": { + "baseUrl": ".", + "module": "commonjs", + "target": "es6", + "sourceMap": true, + "outDir": "./js_out" + } +}