Completed
Branch qa (fc007d)
by Roy
01:36
created

OSPaths.ts ➔ normalizePath   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
1
// # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey
2
3
import * as os from 'os';
4
import * as path from 'path';
5
6
export type OSPaths = {
7
	new (): OSPaths;
8
	(): OSPaths;
9
	readonly home: () => string | undefined;
10
	readonly temp: () => string;
11
};
12
13
const isWinOS = /^win/i.test(process.platform);
14
15
function isEmpty(s: string | null | undefined): boolean {
16
	return !s; // reminder: JS "falsey" == [undefined, null, NaN, 0, '', false]
17
}
18
19
function normalizePath(path_: string | undefined): string | undefined {
20
	return path_ ? path.normalize(path.join(path_, '.')) : void 0;
21
}
22
23
const base = () => {
24
	const { env } = process;
25
26
	const home = () =>
27
		normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.HOME);
28
29
	const temp = () => {
30
		const fallback = '/tmp';
31
		const priorityList = [
32
			typeof os.tmpdir === 'function' ? os.tmpdir() : void 0,
33
			env.TMPDIR,
34
			env.TEMP,
35
			env.TMP,
36
		];
37
		return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback;
38
	};
39
40
	return { home, temp };
41
};
42
43
const windows = () => {
44
	const { env } = process;
45
46
	const home = () => {
47
		const priorityList = [
48
			typeof os.homedir === 'function' ? os.homedir() : void 0,
49
			env.USERPROFILE,
50
			env.HOME,
51
			env.HOMEDRIVE || env.HOMEPATH ? path.join(env.HOMEDRIVE || '', env.HOMEPATH || '') : void 0,
52
		];
53
		return normalizePath(priorityList.find((v) => !isEmpty(v)));
54
	};
55
56
	const temp = () => {
57
		const fallback = 'C:\\Temp';
58
		const priorityList = [
59
			typeof os.tmpdir === 'function' ? os.tmpdir() : void 0,
60
			env.TEMP,
61
			env.TMP,
62
			env.LOCALAPPDATA ? path.join(env.LOCALAPPDATA, 'Temp') : void 0,
63
			(function (s) {
64
				return s ? path.join(s, 'AppData', 'Local', 'Temp') : void 0;
65
			})(home()),
66
			env.ALLUSERSPROFILE ? path.join(env.ALLUSERSPROFILE, 'Temp') : void 0,
67
			env.SystemRoot ? path.join(env.SystemRoot, 'Temp') : void 0,
68
			env.windir ? path.join(env.windir, 'Temp') : void 0,
69
			env.SystemDrive ? path.join(env.SystemDrive + '\\', 'Temp') : void 0,
70
		];
71
		return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback;
72
	};
73
74
	return { home, temp };
75
};
76
77
// eslint-disable-next-line functional/no-class
78
class _OSPaths {
79
	constructor() {
80
		const OSPaths = function () {
81
			return new _OSPaths();
82
		};
83
84
		// Connect to platform-specific API functions by extension
85
		const extension = isWinOS ? windows() : base();
86
		OSPaths.home = extension.home;
87
		OSPaths.temp = extension.temp;
88
89
		return OSPaths;
90
	}
91
}
92
93
const default_ = new _OSPaths() as OSPaths;
94
export default default_;
95