-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.mjs
More file actions
62 lines (53 loc) · 2.04 KB
/
Copy pathplatform.mjs
File metadata and controls
62 lines (53 loc) · 2.04 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
import path from 'node:path';
export function isWindowsPlatform(platform = process.platform) {
return platform === 'win32';
}
export function getPathModule(platform = process.platform) {
return isWindowsPlatform(platform) ? path.win32 : path.posix;
}
export function getHomeDirectory(env = process.env, platform = process.platform) {
if (isWindowsPlatform(platform)) {
// Respect HOME when provided by Git Bash/CI on Windows.
if (env?.HOME) return String(env.HOME);
if (env?.USERPROFILE) return String(env.USERPROFILE);
const homeDrive = String(env?.HOMEDRIVE || '');
const homePath = String(env?.HOMEPATH || '');
if (homeDrive || homePath) {
return `${homeDrive}${homePath}`;
}
}
return String(env?.HOME || env?.USERPROFILE || '');
}
export function getPromptIdentity(env = process.env) {
const user = String(env?.USER || env?.USERNAME || 'root');
const host = String(env?.HOSTNAME || env?.COMPUTERNAME || 'dev');
return { user, host };
}
export function getShellLaunchers(platform = process.platform) {
if (!isWindowsPlatform(platform)) {
return [
{ file: '/bin/sh', args: ['-lc'] },
];
}
return [
{ file: 'pwsh', args: ['-NoLogo', '-NoProfile', '-Command'] },
{ file: 'powershell.exe', args: ['-NoLogo', '-NoProfile', '-Command'] },
{ file: 'cmd.exe', args: ['/d', '/s', '/c'] },
];
}
export function isMissingLauncherError(error) {
return error?.code === 'ENOENT';
}
export function resolveUserPath(input, cwd, { env = process.env, platform = process.platform } = {}) {
const pathApi = getPathModule(platform);
const home = getHomeDirectory(env, platform);
const rawTarget = String(input || home || cwd || '');
const expanded = rawTarget.startsWith('~')
? rawTarget.replace(/^~(?=$|[\\/])/, home || cwd || '')
: rawTarget;
return pathApi.isAbsolute(expanded) ? expanded : pathApi.resolve(cwd, expanded);
}
export function normalizeDisplayPath(value, platform = process.platform) {
const pathApi = getPathModule(platform);
return pathApi.normalize(String(value ?? ''));
}