* Get project search path from args.
( cwd?: string, scriptMode?: boolean, cwdMode?: boolean, scriptPath?: string )
| 687 | * Get project search path from args. |
| 688 | */ |
| 689 | function getProjectSearchDir( |
| 690 | cwd?: string, |
| 691 | scriptMode?: boolean, |
| 692 | cwdMode?: boolean, |
| 693 | scriptPath?: string |
| 694 | ) { |
| 695 | // Validate `--script-mode` / `--cwd-mode` / `--cwd` usage is correct. |
| 696 | if (scriptMode && cwdMode) { |
| 697 | throw new TypeError('--cwd-mode cannot be combined with --script-mode'); |
| 698 | } |
| 699 | if (scriptMode && !scriptPath) { |
| 700 | throw new TypeError( |
| 701 | '--script-mode must be used with a script name, e.g. `ts-node --script-mode <script.ts>`' |
| 702 | ); |
| 703 | } |
| 704 | const doScriptMode = |
| 705 | scriptMode === true ? true : cwdMode === true ? false : !!scriptPath; |
| 706 | if (doScriptMode) { |
| 707 | // Use node's own resolution behavior to ensure we follow symlinks. |
| 708 | // scriptPath may omit file extension or point to a directory with or without package.json. |
| 709 | // This happens before we are registered, so we tell node's resolver to consider ts, tsx, and jsx files. |
| 710 | // In extremely rare cases, is is technically possible to resolve the wrong directory, |
| 711 | // because we do not yet know preferTsExts, jsx, nor allowJs. |
| 712 | // See also, justification why this will not happen in real-world situations: |
| 713 | // https://github.com/TypeStrong/ts-node/pull/1009#issuecomment-613017081 |
| 714 | const exts = ['.js', '.jsx', '.ts', '.tsx']; |
| 715 | const extsTemporarilyInstalled: string[] = []; |
| 716 | for (const ext of exts) { |
| 717 | if (!hasOwnProperty(require.extensions, ext)) { |
| 718 | extsTemporarilyInstalled.push(ext); |
| 719 | require.extensions[ext] = function () {}; |
| 720 | } |
| 721 | } |
| 722 | try { |
| 723 | return dirname(requireResolveNonCached(scriptPath!)); |
| 724 | } finally { |
| 725 | for (const ext of extsTemporarilyInstalled) { |
| 726 | delete require.extensions[ext]; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | return cwd; |
| 732 | } |
| 733 | |
| 734 | const guaranteedNonexistentDirectoryPrefix = resolve(__dirname, 'doesnotexist'); |
| 735 | let guaranteedNonexistentDirectorySuffix = 0; |
no test coverage detected
searching dependent graphs…