Skip to content

Commit

Permalink
Add error messages on missing compileroptions #6
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmourtada committed Mar 17, 2017
1 parent 43a8351 commit 49b64b9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
38 changes: 34 additions & 4 deletions src/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@ interface ConfigLoaderParams {
cwd: string,
}

export interface ConfigLoaderSuccessResult {
resultType: "success",
absoluteBaseUrl: string,
paths: { [key: string]: Array<string> }
}

export interface ConfigLoaderFailResult {
resultType: "failed",
message: string
}

export function configLoader({
tsConfigLoader = TsConfigLoader.tsConfigLoader,
explicitParams,
cwd
}: ConfigLoaderParams) {
}: ConfigLoaderParams): ConfigLoaderSuccessResult | ConfigLoaderFailResult {

if (explicitParams) {

const absoluteBaseUrl = path.isAbsolute(explicitParams.baseUrl)
? explicitParams.baseUrl
: path.join(cwd, explicitParams.baseUrl);
? explicitParams.baseUrl
: path.join(cwd, explicitParams.baseUrl);

return {
resultType: "success",
absoluteBaseUrl,
paths: explicitParams.paths
};
Expand All @@ -37,13 +49,31 @@ export function configLoader({
});

if (!loadResult.tsConfigPath) {
throw new Error("Couldn't find tsconfig");
return {
resultType: "failed",
message: "Couldn't find tsconfig.json"
};
}

if(!loadResult.baseUrl) {
return {
resultType: "failed",
message: "Missing baseUrl in compilerOptions"
};
}

if(!loadResult.paths) {
return {
resultType: "failed",
message: "Missing paths in compilerOptions"
};
}

const tsConfigDir = path.dirname(loadResult.tsConfigPath);
const absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl);

return {
resultType: "success",
absoluteBaseUrl,
paths: loadResult.paths
};
Expand Down
13 changes: 9 additions & 4 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { createMatchPath } from "./match-path";
import {configLoader, ExplicitParams} from "./config-loader";
import { configLoader, ExplicitParams } from "./config-loader";

/**
* Installs a custom module load function that can adhere to paths in tsconfig.
*/
export function register(explicitParams: ExplicitParams) {
const { absoluteBaseUrl, paths } = configLoader({
const configLoaderResult = configLoader({
cwd: process.cwd(),
explicitParams,
});

if (configLoaderResult.resultType === "failed") {
throw new Error(configLoaderResult.message);
}

const matchPath = createMatchPath(
absoluteBaseUrl,
paths
configLoaderResult.absoluteBaseUrl,
configLoaderResult.paths
);

// Patch node's module loading
Expand Down
4 changes: 2 additions & 2 deletions src/tsconfig-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as Tsconfig from "tsconfig";

export interface TsConfigLoaderResult {
tsConfigPath: string | undefined,
baseUrl: string,
paths: { [key: string]: Array<string> }
baseUrl: string | undefined,
paths: { [key: string]: Array<string> } | undefined
}

export interface TsConfigLoaderParams {
Expand Down
50 changes: 44 additions & 6 deletions tests/config-loader-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from "chai";
import { configLoader } from "../src/config-loader";
import { configLoader, ConfigLoaderFailResult, ConfigLoaderSuccessResult } from "../src/config-loader";

describe('config-loader', function () {

Expand All @@ -14,8 +14,10 @@ describe('config-loader', function () {
cwd: "/baz"
});

assert.equal(result.absoluteBaseUrl, "/foo/bar");
assert.equal(result.paths["asd"][0], "asd");
const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.absoluteBaseUrl, "/foo/bar");
assert.equal(successResult.paths["asd"][0], "asd");
});

it('should use explicitParams when set and add cwd when path is relative', () => {
Expand All @@ -29,7 +31,9 @@ describe('config-loader', function () {
cwd: "/baz"
});

assert.equal(result.absoluteBaseUrl, "/baz/bar/");
const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.absoluteBaseUrl, "/baz/bar/");
});

it('should fallback to tsConfigLoader when explicitParams is not set', () => {
Expand All @@ -39,11 +43,45 @@ describe('config-loader', function () {
tsConfigLoader: (_: any) => ({
tsConfigPath: "/baz/tsconfig.json",
baseUrl: "./src",
paths: { }
paths: {}
})
});

assert.equal(result.absoluteBaseUrl, "/baz/src");
const successResult = result as ConfigLoaderSuccessResult;
assert.equal(successResult.resultType, "success");
assert.equal(successResult.absoluteBaseUrl, "/baz/src");
});

it('should show an error message when baseUrl is missing', () => {
const result = configLoader({
explicitParams: undefined,
cwd: "/baz",
tsConfigLoader: (_: any) => ({
tsConfigPath: "/baz/tsconfig.json",
baseUrl: undefined,
paths: {}
})
});

const failResult = result as ConfigLoaderFailResult
assert.equal(failResult.resultType, "failed");
assert.isTrue(failResult.message.indexOf("baseUrl") > -1);
});

it('should show an error message when paths is missing', () => {
const result = configLoader({
explicitParams: undefined,
cwd: "/baz",
tsConfigLoader: (_: any) => ({
tsConfigPath: "/baz/tsconfig.json",
baseUrl: "./src",
paths: undefined
})
});

const failResult = result as ConfigLoaderFailResult
assert.equal(failResult.resultType, "failed");
assert.isTrue(failResult.message.indexOf("paths") > -1);
});

});

0 comments on commit 49b64b9

Please sign in to comment.