Skip to content

Commit

Permalink
Fix #1389: Make --showConfig moduleTypes log correct, relative pa…
Browse files Browse the repository at this point in the history
…ths same as include (#1619)

* render proper relative moduleTypes paths and omit empty require array in --showConfig output

* lint-fix

* rewrite error message to mention --showConfig instead of --show-config

* fix

* Update index.spec.ts
  • Loading branch information
cspotcode authored Feb 2, 2022
1 parent 6e48ef2 commit 32aaffe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { join, resolve, dirname, parse as parsePath } from 'path';
import { join, resolve, dirname, parse as parsePath, relative } from 'path';
import { inspect } from 'util';
import Module = require('module');
import arg = require('arg');
Expand Down Expand Up @@ -331,13 +331,31 @@ Options:
const ts = service.ts as any as TSInternal;
if (typeof ts.convertToTSConfig !== 'function') {
console.error(
'Error: --show-config requires a typescript versions >=3.2 that support --showConfig'
'Error: --showConfig requires a typescript versions >=3.2 that support --showConfig'
);
process.exit(1);
}
let moduleTypes = undefined;
if (service.options.moduleTypes) {
// Assumption: this codepath requires CLI invocation, so moduleTypes must have come from a tsconfig, not API.
const showRelativeTo = dirname(service.configFilePath!);
moduleTypes = {} as Record<string, string>;
for (const [key, value] of Object.entries(service.options.moduleTypes)) {
moduleTypes[
relative(
showRelativeTo,
resolve(service.options.optionBasePaths?.moduleTypes!, key)
)
] = value;
}
}
const json = {
['ts-node']: {
...service.options,
require: service.options.require?.length
? service.options.require
: undefined,
moduleTypes,
optionBasePaths: undefined,
compilerOptions: undefined,
project: service.configFilePath ?? service.options.project,
Expand Down
5 changes: 2 additions & 3 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ test.suite('ts-node', (test) => {
});

if (semver.gte(ts.version, '3.2.0')) {
test('--show-config should log resolved configuration', async (t) => {
test('--showConfig should log resolved configuration', async (t) => {
function native(path: string) {
return path.replace(/\/|\\/g, pathSep);
}
Expand All @@ -908,7 +908,6 @@ test.suite('ts-node', (test) => {
cwd: native(`${ROOT_DIR}/tests`),
projectSearchDir: native(`${ROOT_DIR}/tests`),
project: native(`${ROOT_DIR}/tests/tsconfig.json`),
require: [],
},
compilerOptions: {
target: 'es6',
Expand Down Expand Up @@ -938,7 +937,7 @@ test.suite('ts-node', (test) => {
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --showConfig`
);
expect(err).not.toBe(null);
expect(stderr).toMatch('Error: --show-config requires');
expect(stderr).toMatch('Error: --showConfig requires');
});
}

Expand Down

0 comments on commit 32aaffe

Please sign in to comment.