1
|
|
|
// # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey |
2
|
|
|
|
3
|
|
|
import { Platform } from '../platform-adapters/_base'; |
4
|
|
|
|
5
|
|
|
export type OSPaths = { |
6
|
|
|
new (): OSPaths; |
7
|
|
|
(): OSPaths; |
8
|
|
|
readonly home: () => string | undefined; |
9
|
|
|
readonly temp: () => string; |
10
|
|
|
}; |
11
|
|
|
|
12
|
|
|
function isEmpty(s: string | null | undefined): boolean { |
13
|
|
|
return !s; // reminder: JS "falsey" == [undefined, null, NaN, 0, '', false] |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
// eslint-disable-next-line @typescript-eslint/no-namespace |
17
|
|
|
namespace Adapt { |
18
|
|
|
export const isWinOS = (adapter_: Platform.Adapter) => /^win/i.test(adapter_.process.platform); |
19
|
|
|
|
20
|
|
|
export const normalizePath = (adapter_: Platform.Adapter) => { |
21
|
|
|
return (path_: string | undefined): string | undefined => { |
22
|
|
|
return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0; |
23
|
|
|
}; |
24
|
|
|
}; |
25
|
|
|
|
26
|
|
|
export const home = (adapter_: Platform.Adapter) => { |
27
|
|
|
const { env, os, path } = adapter_; |
28
|
|
|
|
29
|
|
|
const isWinOS = Adapt.isWinOS(adapter_); |
30
|
|
|
const normalizePath = Adapt.normalizePath(adapter_); |
31
|
|
|
|
32
|
|
|
const posix = () => |
33
|
|
|
normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.get('HOME')); |
34
|
|
|
|
35
|
|
|
const windows = () => { |
36
|
|
|
const priorityList = [ |
37
|
|
|
typeof os.homedir === 'function' ? os.homedir() : void 0, |
38
|
|
|
env.get('USERPROFILE'), |
39
|
|
|
env.get('HOME'), |
40
|
|
|
env.get('HOMEDRIVE') || env.get('HOMEPATH') |
41
|
|
|
? path.join(env.get('HOMEDRIVE') || '', env.get('HOMEPATH') || '') |
42
|
|
|
: void 0, |
43
|
|
|
]; |
44
|
|
|
return normalizePath(priorityList.find((v) => !isEmpty(v))); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
return isWinOS ? windows : posix; |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
export const temp = (adapter_: Platform.Adapter) => { |
51
|
|
|
const { env, os, path } = adapter_; |
52
|
|
|
|
53
|
|
|
const isWinOS = Adapt.isWinOS(adapter_); |
54
|
|
|
const normalizePath = Adapt.normalizePath(adapter_); |
55
|
|
|
|
56
|
|
|
function joinPathToBase(base: string | undefined, segments: readonly string[]) { |
57
|
|
|
return base ? path.join(base, ...segments) : void 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
const posix = () => { |
61
|
|
|
const fallback = '/tmp'; |
62
|
|
|
const priorityList = [ |
63
|
|
|
typeof os.tmpdir === 'function' ? os.tmpdir() : void 0, |
64
|
|
|
env.get('TMPDIR'), |
65
|
|
|
env.get('TEMP'), |
66
|
|
|
env.get('TMP'), |
67
|
|
|
]; |
68
|
|
|
return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback; |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
const windows = () => { |
72
|
|
|
const fallback = 'C:\\Temp'; |
73
|
|
|
const priorityListLazy = [ |
74
|
|
|
os.tmpdir, |
75
|
|
|
() => env.get('TEMP'), |
76
|
|
|
() => env.get('TMP'), |
77
|
|
|
() => joinPathToBase(env.get('LOCALAPPDATA'), ['Temp']), |
78
|
|
|
() => joinPathToBase(Adapt.home(adapter_)(), ['AppData', 'Local', 'Temp']), |
79
|
|
|
() => joinPathToBase(env.get('ALLUSERSPROFILE'), ['Temp']), |
80
|
|
|
() => joinPathToBase(env.get('SystemRoot'), ['Temp']), |
81
|
|
|
() => joinPathToBase(env.get('windir'), ['Temp']), |
82
|
|
|
() => joinPathToBase(env.get('SystemDrive'), ['\\', 'Temp']), |
83
|
|
|
]; |
84
|
|
|
const v = priorityListLazy.find((v) => v && !isEmpty(v())); |
85
|
|
|
return (v && normalizePath(v())) || fallback; |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
return isWinOS ? windows : posix; |
89
|
|
|
}; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
export function OSPathsAdaptionBuilder_(adapter_: Platform.Adapter): OSPaths { |
93
|
|
|
// eslint-disable-next-line functional/no-class |
94
|
|
|
class OSPaths_ { |
95
|
|
|
constructor() { |
96
|
|
|
const OSPaths = function () { |
97
|
|
|
return new OSPaths_() as OSPaths; |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
OSPaths.home = Adapt.home(adapter_); |
101
|
|
|
OSPaths.temp = Adapt.temp(adapter_); |
102
|
|
|
|
103
|
|
|
return OSPaths; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return new OSPaths_() as OSPaths; |
108
|
|
|
} |
109
|
|
|
|