-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand-paths.js
40 lines (32 loc) · 1.22 KB
/
expand-paths.js
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
"use strict";
const path = require("path");
const fs = require("fs");
// =====================================================
// Quick way to get paths to %USERPROFILE%, %APPDATA% and others.
// Works on linux and windows
// =====================================================
const { platform } = process;
let windows = platform.startsWith("win");
const maybeReal = (p, ensureExists) => (ensureExists ? fs.realpathSync(p) : p);
// prettier-ignore
const expandAnyPath =
source =>
envKey =>
( relativeChildPath = ".", ensureExists = true ) =>
!(envKey in source)
? null
: maybeReal(path.join(source[envKey], relativeChildPath), ensureExists);
// prettier-ignore
const expandUserPath =
(relativeChildPath = ".", ensureExists = true ) =>
expandAnyPath(process.env)
(windows === true ? "USERPROFILE" : "HOME")
(relativeChildPath, ensureExists);
const expandAppData = expandAnyPath(process.env)("APPDATA");
module.exports = {
expandAppData,
expandUserPath,
expandAnyPath,
maybeReal
};
// expandUserPath(".") is platform agnostic. Works on linux, mac and windows