-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackages.ts
More file actions
57 lines (51 loc) · 1.48 KB
/
packages.ts
File metadata and controls
57 lines (51 loc) · 1.48 KB
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
/**
* @fileoverview Package.json path resolution utilities.
*/
import { normalizePath } from './normalize'
let _path: typeof import('node:path') | undefined
/**
* Get the path module.
*/
/*@__NO_SIDE_EFFECTS__*/
function getPath() {
if (_path === undefined) {
// Use non-'node:' prefixed require to avoid Webpack errors.
_path = /*@__PURE__*/ require('node:path')
}
return _path as typeof import('node:path')
}
/**
* Whether `filepath`'s final segment is exactly `package.json`. Accepts both
* POSIX and Windows-style separators so paths captured on either platform
* classify the same regardless of the host we're running on.
*/
/*@__NO_SIDE_EFFECTS__*/
function isPackageJsonFile(filepath: string): boolean {
return (
filepath === 'package.json' ||
filepath.endsWith('/package.json') ||
filepath.endsWith('\\package.json')
)
}
/**
* Resolve directory path from a package.json file path.
*/
/*@__NO_SIDE_EFFECTS__*/
export function resolvePackageJsonDirname(filepath: string): string {
if (isPackageJsonFile(filepath)) {
const path = getPath()
return normalizePath(path.dirname(filepath))
}
return normalizePath(filepath)
}
/**
* Resolve full path to package.json from a directory or file path.
*/
/*@__NO_SIDE_EFFECTS__*/
export function resolvePackageJsonPath(filepath: string): string {
if (isPackageJsonFile(filepath)) {
return normalizePath(filepath)
}
const path = getPath()
return normalizePath(path.join(filepath, 'package.json'))
}