Skip to content

Commit

Permalink
refactor: avoid minimist if register receives explicit options (#206)
Browse files Browse the repository at this point in the history
* chore: update changelog and readme

* refactor: move minimist usage into register.js

* Revert "refactor: move minimist usage into register.js"

This reverts commit 3bc36fb.

* refactor: avoid minimist if `register` receives explicit options

* chore: update changelog

* chore: clean up changelog
  • Loading branch information
aleclarson committed May 2, 2022
1 parent fc7cd60 commit 1081597
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions 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;
Expand Down Expand Up @@ -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({
Expand Down
19 changes: 0 additions & 19 deletions src/options.ts

This file was deleted.

26 changes: 22 additions & 4 deletions 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;

Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit 1081597

Please sign in to comment.