From 10815971abd947212383f6ae1b690334f97c5540 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Mon, 2 May 2022 13:42:09 -0400 Subject: [PATCH] refactor: avoid minimist if `register` receives explicit options (#206) * chore: update changelog and readme * refactor: move minimist usage into register.js * Revert "refactor: move minimist usage into register.js" This reverts commit 3bc36fbfd00770faeabd4eea38cd86994744fa4c. * refactor: avoid minimist if `register` receives explicit options * chore: update changelog * chore: clean up changelog --- CHANGELOG.md | 9 ++++++++- src/config-loader.ts | 5 ++--- src/options.ts | 19 ------------------- src/register.ts | 26 ++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 27 deletions(-) delete mode 100644 src/options.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a480126..abf034b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] -- Ability to use absolute paths. [#184](https://github.com/dividab/tsconfig-paths/pull/184) +### 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). - 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! +- Let `paths` mappings be absolute paths. See PR [#184](https://github.com/dividab/tsconfig-paths/pull/184). + +### Changed + +- Ignore `--project`/`-P` CLI flag when explicit options are passed to `register`. See PR [#206](https://github.com/dividab/tsconfig-paths/pull/206). ### Added diff --git a/src/config-loader.ts b/src/config-loader.ts index 3b5c7dc..5c2185d 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -1,6 +1,5 @@ import * as TsConfigLoader2 from "./tsconfig-loader"; import * as path from "path"; -import { options } from "./options"; export interface ExplicitParams { baseUrl: string; @@ -38,8 +37,8 @@ export type ConfigLoaderResult = | ConfigLoaderSuccessResult | ConfigLoaderFailResult; -export function loadConfig(cwd: string = options.cwd): ConfigLoaderResult { - return configLoader({ cwd: cwd }); +export function loadConfig(cwd: string = process.cwd()): ConfigLoaderResult { + return configLoader({ cwd }); } export function configLoader({ diff --git a/src/options.ts b/src/options.ts deleted file mode 100644 index 877e8fd..0000000 --- a/src/options.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as minimist from "minimist"; - -const argv = minimist(process.argv.slice(2), { - // eslint-disable-next-line id-denylist - string: ["project"], - alias: { - project: ["P"], - }, -}); - -const project = argv && argv.project; - -export interface Options { - cwd: string; -} - -export const options: Options = { - cwd: project || process.cwd(), -}; diff --git a/src/register.ts b/src/register.ts index 8812b51..bf81052 100644 --- a/src/register.ts +++ b/src/register.ts @@ -1,6 +1,5 @@ import { createMatchPath } from "./match-path-sync"; import { configLoader, ExplicitParams } from "./config-loader"; -import { options } from "./options"; const noOp = (): void => void 0; @@ -57,10 +56,29 @@ export interface RegisterParams extends ExplicitParams { * Returns a function to undo paths registration. */ export function register(params?: RegisterParams): () => void { + let cwd: string | undefined; + let explicitParams: ExplicitParams | undefined; + if (params) { + cwd = params.cwd; + if (params.baseUrl || params.paths) { + explicitParams = params; + } + } else { + // eslint-disable-next-line + const minimist = require("minimist"); + const argv = minimist(process.argv.slice(2), { + // eslint-disable-next-line id-denylist + string: ["project"], + alias: { + project: ["P"], + }, + }); + cwd = argv.project; + } + const configLoaderResult = configLoader({ - cwd: params?.cwd ?? options.cwd, - explicitParams: - params && (params.baseUrl || params.paths) ? params : undefined, + cwd: cwd ?? process.cwd(), + explicitParams, }); if (configLoaderResult.resultType === "failed") {