From 90396222ed5f82300cf201c35ec8d2e05bc1a7b8 Mon Sep 17 00:00:00 2001 From: Maxim Procopenco Date: Fri, 13 Apr 2018 16:49:21 +0300 Subject: [PATCH] Add possibility to indicate explicitly tsconfig location --- example/project/lib1/index.ts | 3 +++ example/project/main.ts | 3 +++ example/project/tsconfig.json | 10 ++++++++++ package.json | 5 ++++- src/config-loader.ts | 3 ++- src/options.ts | 16 ++++++++++++++++ src/register.ts | 3 ++- yarn.lock | 4 ++++ 8 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 example/project/lib1/index.ts create mode 100644 example/project/main.ts create mode 100644 example/project/tsconfig.json create mode 100644 src/options.ts diff --git a/example/project/lib1/index.ts b/example/project/lib1/index.ts new file mode 100644 index 0000000..6f31f8b --- /dev/null +++ b/example/project/lib1/index.ts @@ -0,0 +1,3 @@ +export function hello1(): void { + console.log("Hello1"); +} diff --git a/example/project/main.ts b/example/project/main.ts new file mode 100644 index 0000000..3c37ab2 --- /dev/null +++ b/example/project/main.ts @@ -0,0 +1,3 @@ +import { hello1 } from "lib1"; + +hello1(); diff --git a/example/project/tsconfig.json b/example/project/tsconfig.json new file mode 100644 index 0000000..b0e6ba0 --- /dev/null +++ b/example/project/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "outDir": "js_out", + "noImplicitAny": false, + "sourceMap": false, + "baseUrl": "." + } +} diff --git a/package.json b/package.json index a2ccc3a..ace785e 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "devDependencies": { "@types/chai": "^4.0.10", "@types/deepmerge": "^1.3.2", + "@types/minimist": "^1.2.0", "@types/mocha": "^2.2.45", "@types/node": "^6.0.54", "@types/strip-bom": "^3.0.0", @@ -29,12 +30,14 @@ }, "dependencies": { "deepmerge": "^2.0.1", + "minimist": "^1.2.0", "strip-bom": "^3.0.0", "strip-json-comments": "^2.0.1" }, "scripts": { "start": "cd src && ts-node index.ts", - "example:node": "yarn build && cd example/node && ts-node -r ../register.js main.ts", + "example:node": "yarn build && cd ./example/node && ts-node -r ../register.js main.ts", + "example:project": "yarn build && ts-node -r ./register.js -P ./example/project/tsconfig.json ./example/project/main.ts", "example:api": "cd example/api && ts-node main.ts", "example:perf": "cd example/perf && ts-node main.ts", "test": "mocha", diff --git a/src/config-loader.ts b/src/config-loader.ts index 0ba9d34..7d2489e 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -1,5 +1,6 @@ import * as TsConfigLoader from "./tsconfig-loader"; import * as path from "path"; +import { options } from "./options"; export interface ExplicitParams { baseUrl: string; @@ -33,7 +34,7 @@ export type ConfigLoaderResult = | ConfigLoaderSuccessResult | ConfigLoaderFailResult; -export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult { +export function loadConfig(cwd: string = options.cwd): ConfigLoaderResult { return configLoader({ cwd: cwd }); } diff --git a/src/options.ts b/src/options.ts new file mode 100644 index 0000000..daf7056 --- /dev/null +++ b/src/options.ts @@ -0,0 +1,16 @@ +import * as minimist from "minimist"; + +const argv = minimist(process.argv.slice(2), { + string: ["project"], + alias: { + project: ["P"] + } +}); + +export interface Options { + cwd: string; +} + +export const options: Options = { + cwd: argv.project || process.cwd() +}; diff --git a/src/register.ts b/src/register.ts index 4a462a4..ebd3a5a 100644 --- a/src/register.ts +++ b/src/register.ts @@ -1,12 +1,13 @@ import { createMatchPath } from "./match-path-sync"; import { configLoader, ExplicitParams } from "./config-loader"; +import { options } from "./options"; /** * Installs a custom module load function that can adhere to paths in tsconfig. */ export function register(explicitParams: ExplicitParams): void { const configLoaderResult = configLoader({ - cwd: process.cwd(), + cwd: options.cwd, explicitParams }); diff --git a/yarn.lock b/yarn.lock index ef32359..24e2c02 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10,6 +10,10 @@ version "1.3.2" resolved "https://registry.yarnpkg.com/@types/deepmerge/-/deepmerge-1.3.2.tgz#a87837384624d63e8c3df3ae85693d574ea6b5db" +"@types/minimist@^1.2.0": + version "1.2.0" + resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6" + "@types/mocha@^2.2.45": version "2.2.45" resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.45.tgz#816572b6e45164526a36d4faa123e8267d6d5d0a"