-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackages.ts
More file actions
47 lines (41 loc) · 1.32 KB
/
packages.ts
File metadata and controls
47 lines (41 loc) · 1.32 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
/**
* @fileoverview Package.json path resolution utilities.
*/
import { normalizePath } from './normalize'
import { StringPrototypeEndsWith } from '../primordials/string'
import { getNodePath } from '../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__*/
export function isPackageJsonFile(filepath: string): boolean {
return (
filepath === 'package.json' ||
StringPrototypeEndsWith(filepath, '/package.json') ||
StringPrototypeEndsWith(filepath, '\\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 = getNodePath()
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 = getNodePath()
return normalizePath(path.join(filepath, 'package.json'))
}