-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
156 lines (138 loc) · 4.88 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
import path from 'path';
import customResolvePackagePath from './resolve-package-path';
import rethrowUnlessCode from './rethrow-unless-code';
const ALLOWED_ERROR_CODES = [
// resolve package error codes
'MODULE_NOT_FOUND',
// Yarn PnP Error Codes
'UNDECLARED_DEPENDENCY',
'MISSING_PEER_DEPENDENCY',
'MISSING_DEPENDENCY'
];
import CacheGroup from './cache-group';
import Cache from './cache';
const getRealFilePath = customResolvePackagePath._getRealFilePath;
const getRealDirectoryPath = customResolvePackagePath._getRealDirectoryPath;
const __findUpPackagePath = customResolvePackagePath._findUpPackagePath;
let CACHE = new CacheGroup();
let FIND_UP_CACHE = new Cache();
let pnp: any;
try {
// eslint-disable-next-line node/no-missing-require
pnp = require('pnpapi');
} catch (error) {
// not in Yarn PnP; not a problem
}
/**
* Search each directory in the absolute path `baseDir`, from leaf to root, for
* a `package.json`, and return the first match, or `null` if no `package.json`
* was found.
*
* @public
* @param {string} baseDir - an absolute path in which to search for a `package.json`
* @param {CacheGroup|boolean} [_cache] (optional)
* * if true: will choose the default global cache
* * if false: will not cache
* * if undefined or omitted, will choose the default global cache
* * otherwise we assume the argument is an external cache of the form provided by resolve-package-path/lib/cache-group.js
*
* @return {string|null} a full path to the resolved package.json if found or null if not
*/
function _findUpPackagePath(baseDir: string, _cache?: Cache | boolean) {
let cache;
if (_cache === undefined || _cache === null || _cache === true) {
// if no cache specified, or if cache is true then use the global cache
cache = FIND_UP_CACHE;
} else if (_cache === false) {
// if cache is explicity false, create a throw-away cache;
cache = new Cache();
} else {
// otherwise, assume the user has provided an alternative cache for the following form:
// provided by resolve-package-path/lib/cache-group.js
cache = _cache;
}
let absoluteStart = path.resolve(baseDir);
return __findUpPackagePath(cache, absoluteStart);
}
/*
* @public
*
* @method resolvePackagePathSync
* @param {string} name name of the dependency module.
* @param {string} baseDir root dir to run the resolve from
* @param {Boolean|CustomCache} (optional)
* * if true: will choose the default global cache
* * if false: will not cache
* * if undefined or omitted, will choose the default global cache
* * otherwise we assume the argument is an external cache of the form provided by resolve-package-path/lib/cache-group.js
*
* @return {string|null} a full path to the resolved package.json if found or null if not
*/
export = resolvePackagePath;
function resolvePackagePath(
target: string,
baseDir: string,
_cache?: CacheGroup | boolean,
): string | null {
let cache;
if (_cache === undefined || _cache === null || _cache === true) {
// if no cache specified, or if cache is true then use the global cache
cache = CACHE;
} else if (_cache === false) {
// if cache is explicity false, create a throw-away cache;
cache = new CacheGroup();
} else {
// otherwise, assume the user has provided an alternative cache for the following form:
// provided by resolve-package-path/lib/cache-group.js
cache = _cache;
}
if (baseDir.charAt(baseDir.length - 1) !== path.sep) {
baseDir = `${baseDir}${path.sep}`;
}
const key = target + '\x00' + baseDir;
let pkgPath;
if (cache.PATH.has(key)) {
pkgPath = cache.PATH.get(key);
} else {
try {
// the custom `pnp` code here can be removed when yarn 1.13 is the
// current release. This is due to Yarn 1.13 and resolve interoperating
// together seamlessly.
pkgPath = pnp
? pnp.resolveToUnqualified(target + '/package.json', baseDir)
: customResolvePackagePath(cache, target, baseDir);
} catch (e) {
rethrowUnlessCode(e, ...ALLOWED_ERROR_CODES);
pkgPath = null;
}
cache.PATH.set(key, pkgPath);
}
return pkgPath;
}
resolvePackagePath._resetCache = function () {
CACHE = new CacheGroup();
FIND_UP_CACHE = new Cache();
};
// eslint-disable-next-line no-redeclare
module resolvePackagePath {
export let _CACHE: CacheGroup;
export let _FIND_UP_CACHE = FIND_UP_CACHE;
export let findUpPackagePath = _findUpPackagePath;
}
Object.defineProperty(resolvePackagePath, '_CACHE', {
get: function () {
return CACHE;
},
});
Object.defineProperty(resolvePackagePath, '_FIND_UP_CACHE', {
get: function () {
return FIND_UP_CACHE;
},
});
resolvePackagePath.getRealFilePath = function (filePath: string) {
return getRealFilePath(CACHE.REAL_FILE_PATH, filePath);
};
resolvePackagePath.getRealDirectoryPath = function (directoryPath: string) {
return getRealDirectoryPath(CACHE.REAL_DIRECTORY_PATH, directoryPath);
};