-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup-package-scope.mts
56 lines (49 loc) · 1.53 KB
/
lookup-package-scope.mts
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
/**
* @file lookupPackageScope
* @module mlly/lib/lookupPackageScope
*/
import isFile from '#lib/is-file'
import root from '#lib/root'
import type { FileSystem, ModuleId } from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
/**
* Get the package scope URL for the given module `url`.
*
* Implements the `LOOKUP_PACKAGE_SCOPE` algorithm.
*
* @see {@linkcode FileSystem}
* @see {@linkcode ModuleId}
*
* @param {ModuleId} url
* URL of module to get package scope for
* @param {ModuleId | null | undefined} [end]
* URL of directory to end search, defaults to {@linkcode root}
* @param {FileSystem | null | undefined} [fs]
* File system API
* @return {URL | null}
* URL of nearest directory containing `package.json` file or `null`
*/
function lookupPackageScope(
url: ModuleId,
end?: ModuleId | null | undefined,
fs?: FileSystem | null | undefined
): URL | null {
/**
* Scope URL.
*
* @var {URL} scopeUrl
*/
let scopeUrl: URL = new URL(url)
while (String(scopeUrl) !== String(end ?? root)) {
// set scopeUrl to URL of parent directory
scopeUrl = /[/\\]$/.test(scopeUrl.pathname)
? new URL(pathe.dot + pathe.dot, scopeUrl)
: new URL(pathe.dot, scopeUrl)
// exit early if scopeUrl ends in "node_modules" path segment
if (/node_modules[/\\]$/.test(scopeUrl.pathname)) break
// return scopeUrl if `package.json` file exists in directory
if (isFile(new URL('package.json', scopeUrl), fs)) return scopeUrl
}
return null
}
export default lookupPackageScope