-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.ts
More file actions
105 lines (94 loc) · 2.85 KB
/
paths.ts
File metadata and controls
105 lines (94 loc) · 2.85 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
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
/** @fileoverview Path utilities for DLX package installations. */
import { normalizePath } from '../paths/normalize'
import { getSocketDlxDir } from '../paths/socket'
import { StringPrototypeStartsWith } from '../primordials'
let _path: typeof import('node:path') | undefined
/**
* Lazily load the path module to avoid Webpack errors.
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getPath() {
if (_path === undefined) {
// Use non-'node:' prefixed require to avoid Webpack errors.
_path = /*@__PURE__*/ require('node:path')
}
return _path!
}
/**
* Get the installed package directory within DLX node_modules.
*
* @example
* ```typescript
* const dir = getDlxInstalledPackageDir('prettier')
* ```
*/
export function getDlxInstalledPackageDir(packageName: string): string {
const path = getPath()
return normalizePath(
path.join(getDlxPackageNodeModulesDir(packageName), packageName),
)
}
/**
* Get the DLX installation directory for a specific package.
*
* @example
* ```typescript
* const dir = getDlxPackageDir('a1b2c3d4')
* ```
*/
export function getDlxPackageDir(packageName: string): string {
const path = getPath()
return normalizePath(path.join(getSocketDlxDir(), packageName))
}
/**
* Get the package.json path for a DLX installed package.
*
* @example
* ```typescript
* const jsonPath = getDlxPackageJsonPath('prettier')
* ```
*/
export function getDlxPackageJsonPath(packageName: string): string {
const path = getPath()
return normalizePath(
path.join(getDlxInstalledPackageDir(packageName), 'package.json'),
)
}
/**
* Get the node_modules directory for a DLX package installation.
*
* @example
* ```typescript
* const nmDir = getDlxPackageNodeModulesDir('a1b2c3d4')
* ```
*/
export function getDlxPackageNodeModulesDir(packageName: string): string {
const path = getPath()
return normalizePath(path.join(getDlxPackageDir(packageName), 'node_modules'))
}
/**
* Check if a file path is within the Socket DLX directory.
* This is useful for determining if a binary or file is managed by Socket's DLX system.
*
* @param filePath - Absolute or relative path to check
* @returns true if the path is within ~/.socket/_dlx/, false otherwise
*
* @example
* ```typescript
* isInSocketDlx('/home/<user>/.socket/_dlx/abc123/bin/socket') // true
* isInSocketDlx('/usr/local/bin/socket') // false
* isInSocketDlx(process.argv[0]) // Check if current binary is in DLX
* ```
*/
export function isInSocketDlx(filePath: string): boolean {
if (!filePath) {
return false
}
const path = getPath()
const dlxDir = normalizePath(getSocketDlxDir())
const absolutePath = normalizePath(path.resolve(filePath))
// Check if the absolute path starts with the DLX directory.
// Both paths are normalized to use forward slashes for consistent comparison.
return StringPrototypeStartsWith(absolutePath, `${dlxDir}/`)
}