MCPcopy
hub / github.com/TypeStrong/ts-node / getProjectSearchDir

Function getProjectSearchDir

src/bin.ts:689–732  ·  view source on GitHub ↗

* Get project search path from args.

(
  cwd?: string,
  scriptMode?: boolean,
  cwdMode?: boolean,
  scriptPath?: string
)

Source from the content-addressed store, hash-verified

687 * Get project search path from args.
688 */
689function 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
734const guaranteedNonexistentDirectoryPrefix = resolve(__dirname, 'doesnotexist');
735let guaranteedNonexistentDirectorySuffix = 0;

Callers 1

phase3Function · 0.85

Calls 2

hasOwnPropertyFunction · 0.90
requireResolveNonCachedFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…