Skip to content

Commit

Permalink
Add possibility to indicate explicitly tsconfig location (#35)
Browse files Browse the repository at this point in the history
* Add possibility to indicate explicitly tsconfig location

* Add docs to readme
  • Loading branch information
procopenco authored and jonaskello committed Apr 14, 2018
1 parent 215b423 commit 8e49a14
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ Then run with:

`node -r ./tsconfig-paths-bootstrap.js main.js`

## Configuration Options

You can set options by passing them before the script path, via programmatic usage or via environment variables.

```bash
ts-node --project customLocation/tsconfig.json -r tsconfig-paths/register "test/**/*.ts"
```

### CLI and Programmatic Options

_Environment variable denoted in parentheses._

* `-P, --project [path]` Path to TypeScript JSON project file (`TS_NODE_PROJECT`)

## Config loading process
1. Use explicit params passed to register
2. Use `process.env.TS_NODE_PROJECT` to resolve tsConfig.json and the specified baseUrl and paths.
Expand Down
3 changes: 3 additions & 0 deletions example/project/lib1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hello1(): void {
console.log("Hello1");
}
3 changes: 3 additions & 0 deletions example/project/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { hello1 } from "lib1";

hello1();
10 changes: 10 additions & 0 deletions example/project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "js_out",
"noImplicitAny": false,
"sourceMap": false,
"baseUrl": "."
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as TsConfigLoader from "./tsconfig-loader";
import * as path from "path";
import { options } from "./options";

export interface ExplicitParams {
baseUrl: string;
Expand Down Expand Up @@ -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 });
}

Expand Down
16 changes: 16 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -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()
};
3 changes: 2 additions & 1 deletion src/register.ts
Original file line number Diff line number Diff line change
@@ -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
});

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8e49a14

Please sign in to comment.