|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {dirname, resolve} from 'path'; |
| 9 | +import * as fs from 'graceful-fs'; |
| 10 | +import type {Config} from '@jest/types'; |
| 11 | +import {tryRealpath} from 'jest-util'; |
| 12 | + |
| 13 | +export function clearFsCache(): void { |
| 14 | + checkedPaths.clear(); |
| 15 | + checkedRealpathPaths.clear(); |
| 16 | + packageContents.clear(); |
| 17 | +} |
| 18 | + |
| 19 | +enum IPathType { |
| 20 | + FILE = 1, |
| 21 | + DIRECTORY = 2, |
| 22 | + OTHER = 3, |
| 23 | +} |
| 24 | +const checkedPaths = new Map<string, IPathType>(); |
| 25 | +function statSyncCached(path: string): IPathType { |
| 26 | + const result = checkedPaths.get(path); |
| 27 | + if (result != null) { |
| 28 | + return result; |
| 29 | + } |
| 30 | + |
| 31 | + let stat; |
| 32 | + try { |
| 33 | + stat = fs.statSync(path); |
| 34 | + } catch (e: any) { |
| 35 | + if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) { |
| 36 | + throw e; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (stat) { |
| 41 | + if (stat.isFile() || stat.isFIFO()) { |
| 42 | + checkedPaths.set(path, IPathType.FILE); |
| 43 | + return IPathType.FILE; |
| 44 | + } else if (stat.isDirectory()) { |
| 45 | + checkedPaths.set(path, IPathType.DIRECTORY); |
| 46 | + return IPathType.DIRECTORY; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + checkedPaths.set(path, IPathType.OTHER); |
| 51 | + return IPathType.OTHER; |
| 52 | +} |
| 53 | + |
| 54 | +const checkedRealpathPaths = new Map<string, string>(); |
| 55 | +function realpathCached(path: Config.Path): Config.Path { |
| 56 | + let result = checkedRealpathPaths.get(path); |
| 57 | + |
| 58 | + if (result != null) { |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + result = tryRealpath(path); |
| 63 | + |
| 64 | + checkedRealpathPaths.set(path, result); |
| 65 | + |
| 66 | + if (path !== result) { |
| 67 | + // also cache the result in case it's ever referenced directly - no reason to `realpath` that as well |
| 68 | + checkedRealpathPaths.set(result, result); |
| 69 | + } |
| 70 | + |
| 71 | + return result; |
| 72 | +} |
| 73 | + |
| 74 | +export type PkgJson = Record<string, unknown>; |
| 75 | + |
| 76 | +const packageContents = new Map<string, PkgJson>(); |
| 77 | +export function readPackageCached(path: Config.Path): PkgJson { |
| 78 | + let result = packageContents.get(path); |
| 79 | + |
| 80 | + if (result != null) { |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + result = JSON.parse(fs.readFileSync(path, 'utf8')) as PkgJson; |
| 85 | + |
| 86 | + packageContents.set(path, result); |
| 87 | + |
| 88 | + return result; |
| 89 | +} |
| 90 | + |
| 91 | +// adapted from |
| 92 | +// https://github.com/lukeed/escalade/blob/2477005062cdbd8407afc90d3f48f4930354252b/src/sync.js |
| 93 | +// to use cached `fs` calls |
| 94 | +export function findClosestPackageJson( |
| 95 | + start: Config.Path, |
| 96 | +): Config.Path | undefined { |
| 97 | + let dir = resolve('.', start); |
| 98 | + if (!isDirectory(dir)) { |
| 99 | + dir = dirname(dir); |
| 100 | + } |
| 101 | + |
| 102 | + while (true) { |
| 103 | + const pkgJsonFile = resolve(dir, './package.json'); |
| 104 | + const hasPackageJson = isFile(pkgJsonFile); |
| 105 | + |
| 106 | + if (hasPackageJson) { |
| 107 | + return pkgJsonFile; |
| 108 | + } |
| 109 | + |
| 110 | + const prevDir = dir; |
| 111 | + dir = dirname(dir); |
| 112 | + |
| 113 | + if (prevDir === dir) { |
| 114 | + return undefined; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/* |
| 120 | + * helper functions |
| 121 | + */ |
| 122 | +export function isFile(file: Config.Path): boolean { |
| 123 | + return statSyncCached(file) === IPathType.FILE; |
| 124 | +} |
| 125 | + |
| 126 | +export function isDirectory(dir: Config.Path): boolean { |
| 127 | + return statSyncCached(dir) === IPathType.DIRECTORY; |
| 128 | +} |
| 129 | + |
| 130 | +export function realpathSync(file: Config.Path): Config.Path { |
| 131 | + return realpathCached(file); |
| 132 | +} |
0 commit comments