Skip to content

Commit 32aaffe

Browse files
authored
Fix #1389: Make --showConfig moduleTypes log correct, relative paths 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
1 parent 6e48ef2 commit 32aaffe

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/bin.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { join, resolve, dirname, parse as parsePath } from 'path';
3+
import { join, resolve, dirname, parse as parsePath, relative } from 'path';
44
import { inspect } from 'util';
55
import Module = require('module');
66
import arg = require('arg');
@@ -331,13 +331,31 @@ Options:
331331
const ts = service.ts as any as TSInternal;
332332
if (typeof ts.convertToTSConfig !== 'function') {
333333
console.error(
334-
'Error: --show-config requires a typescript versions >=3.2 that support --showConfig'
334+
'Error: --showConfig requires a typescript versions >=3.2 that support --showConfig'
335335
);
336336
process.exit(1);
337337
}
338+
let moduleTypes = undefined;
339+
if (service.options.moduleTypes) {
340+
// Assumption: this codepath requires CLI invocation, so moduleTypes must have come from a tsconfig, not API.
341+
const showRelativeTo = dirname(service.configFilePath!);
342+
moduleTypes = {} as Record<string, string>;
343+
for (const [key, value] of Object.entries(service.options.moduleTypes)) {
344+
moduleTypes[
345+
relative(
346+
showRelativeTo,
347+
resolve(service.options.optionBasePaths?.moduleTypes!, key)
348+
)
349+
] = value;
350+
}
351+
}
338352
const json = {
339353
['ts-node']: {
340354
...service.options,
355+
require: service.options.require?.length
356+
? service.options.require
357+
: undefined,
358+
moduleTypes,
341359
optionBasePaths: undefined,
342360
compilerOptions: undefined,
343361
project: service.configFilePath ?? service.options.project,

src/test/index.spec.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ test.suite('ts-node', (test) => {
889889
});
890890

891891
if (semver.gte(ts.version, '3.2.0')) {
892-
test('--show-config should log resolved configuration', async (t) => {
892+
test('--showConfig should log resolved configuration', async (t) => {
893893
function native(path: string) {
894894
return path.replace(/\/|\\/g, pathSep);
895895
}
@@ -908,7 +908,6 @@ test.suite('ts-node', (test) => {
908908
cwd: native(`${ROOT_DIR}/tests`),
909909
projectSearchDir: native(`${ROOT_DIR}/tests`),
910910
project: native(`${ROOT_DIR}/tests/tsconfig.json`),
911-
require: [],
912911
},
913912
compilerOptions: {
914913
target: 'es6',
@@ -938,7 +937,7 @@ test.suite('ts-node', (test) => {
938937
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --showConfig`
939938
);
940939
expect(err).not.toBe(null);
941-
expect(stderr).toMatch('Error: --show-config requires');
940+
expect(stderr).toMatch('Error: --showConfig requires');
942941
});
943942
}
944943

0 commit comments

Comments
 (0)