Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: avoid minimist if register receives explicit options #206

Merged
merged 6 commits into from May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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