Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/ts-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,24 @@ function loadTSConfig(root: string): TSConfig | undefined {
tsconfigPath,
fs.readFileSync(tsconfigPath, 'utf8'),
).config
if (!tsconfig || !tsconfig.compilerOptions) {
const tsconfigParsed = typescript.parseJsonConfigFileContent(
tsconfig,
typescript.sys,
root,
)
if (!tsconfigParsed || !tsconfigParsed.options) {
throw new Error(
`Could not read and parse tsconfig.json at ${tsconfigPath}, or it ` +
'did not contain a "compilerOptions" section.')
}
return tsconfig
return {
compilerOptions: {
...tsconfigParsed.options,
target: tsconfigParsed.options.target === undefined ?
tsconfigParsed.options.target :
typescript.ScriptTarget[tsconfigParsed.options.target],
Comment on lines +54 to +56
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is because parseJsonConfigFileContent() transforms "target" into its numerical enum value, so we have to change it back to a string to use with tsnode.register().

},
}
}
}

Expand Down Expand Up @@ -101,12 +113,10 @@ export function tsPath(root: string, orig: string | undefined): string | undefin
registerTSNode(root)
const tsconfig = tsconfigs[root]
if (!tsconfig) return orig
const {rootDir, rootDirs, outDir} = tsconfig.compilerOptions
const rootDirPath = rootDir || (rootDirs || [])[0]
if (!rootDirPath || !outDir) return orig
const {rootDir, rootDirs, outDir: lib} = tsconfig.compilerOptions // ./lib
const src = rootDir || (rootDirs || [])[0] // ./src
if (!src || !lib) return orig
// rewrite path from ./lib/foo to ./src/foo
const lib = path.join(root, outDir) // ./lib
const src = path.join(root, rootDirPath) // ./src
Comment on lines -104 to -109
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseJsonConfigFileContent() resolves rootDir, rootDirs, and outDir to their full paths, so no need to join their path with the root.

const relative = path.relative(lib, orig) // ./commands
const out = path.join(src, relative) // ./src/commands
// this can be a directory of commands or point to a hook file
Expand Down