From fc7cd600f3be3635b92ad8d510fbf31faa78fcad Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Thu, 21 Apr 2022 01:57:38 -0400 Subject: [PATCH] feat(register): add `cwd` param (#205) * feat(register): add `cwd` param * chore: update changelog and readme --- CHANGELOG.md | 4 ++++ README.md | 1 + src/register.ts | 14 +++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49613b9..a480126 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Add support for `jsconfig.json`. See PR [#199](https://github.com/dividab/tsconfig-paths/pull/199). Thanks to [@F3n67u](https://github.com/F3n67u) for this PR! +### Added + +- Add `cwd` option to `register` function that overrides where the `tsconfig.json` search begins. See PR [#205](https://github.com/dividab/tsconfig-paths/pull/205). + ## [3.14.1] - 2022-03-22 ### Fixed diff --git a/README.md b/README.md index 4a749af..dd2c214 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ export interface ExplicitParams { paths: { [key: string]: Array }; mainFields?: Array; addMatchAll?: boolean; + cwd?: string; } /** diff --git a/src/register.ts b/src/register.ts index 929b7a0..8812b51 100644 --- a/src/register.ts +++ b/src/register.ts @@ -45,14 +45,22 @@ function getCoreModules( return coreModules; } +export interface RegisterParams extends ExplicitParams { + /** + * Defaults to `--project` CLI flag or `process.cwd()` + */ + cwd?: string; +} + /** * Installs a custom module load function that can adhere to paths in tsconfig. * Returns a function to undo paths registration. */ -export function register(explicitParams: ExplicitParams): () => void { +export function register(params?: RegisterParams): () => void { const configLoaderResult = configLoader({ - cwd: options.cwd, - explicitParams, + cwd: params?.cwd ?? options.cwd, + explicitParams: + params && (params.baseUrl || params.paths) ? params : undefined, }); if (configLoaderResult.resultType === "failed") {