1 | // # spell-checker:ignore UserProfile HomeDrive HomePath WinDir |
||
2 | /* eslint-env es6, node */ |
||
3 | 'use strict'; |
||
4 | |||
5 | const os = require('os'); |
||
6 | |||
7 | const isWinOS = /^win/i.test(process.platform); |
||
8 | |||
9 | const base = () => { |
||
10 | const {env} = process; |
||
11 | |||
12 | const object = {}; |
||
13 | |||
14 | object.home = os.homedir ? |
||
15 | () => { |
||
16 | return os.homedir(); |
||
17 | } : |
||
18 | () => { |
||
19 | let path = env.HOME; |
||
20 | if (path.length > 1 && path.endsWith('/')) { |
||
21 | path = path.slice(0, -1); |
||
22 | } |
||
23 | |||
24 | return path; |
||
25 | }; |
||
26 | |||
27 | object.temp = os.tmpdir ? |
||
28 | () => { |
||
29 | return os.tmpdir(); |
||
30 | } : |
||
31 | () => { |
||
32 | let path = env.TMPDIR || |
||
33 | env.TEMP || |
||
34 | env.TMP || |
||
35 | '/tmp'; |
||
36 | if (path.length > 1 && path.endsWith('/')) { |
||
37 | path = path.slice(0, -1); |
||
38 | } |
||
39 | |||
40 | return path; |
||
41 | }; |
||
42 | |||
43 | return object; |
||
44 | }; |
||
45 | |||
46 | const windows = () => { |
||
47 | const {env} = process; |
||
48 | |||
49 | const object = {}; |
||
50 | |||
51 | object.home = os.homedir ? |
||
52 | () => { |
||
53 | return os.homedir(); |
||
54 | } : |
||
55 | () => { |
||
56 | let path = env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || env.HOME; |
||
57 | if (path.length > 1 && ((path.endsWith('\\') && !path.endsWith(':\\')) || (path.endsWith('/') && !path.endsWith(':/')))) { |
||
58 | path = path.slice(0, -1); |
||
59 | } |
||
60 | |||
61 | return path; |
||
62 | }; |
||
63 | |||
64 | object.temp = os.tmpdir ? |
||
65 | () => { |
||
66 | return os.tmpdir(); |
||
67 | } : |
||
68 | () => { |
||
69 | let path = env.TEMP || |
||
70 | env.TMP || |
||
71 | (env.SystemRoot || env.windir) + '\\temp'; |
||
72 | if (path.length > 1 && ((path.endsWith('\\') && !path.endsWith(':\\')) || (path.endsWith('/') && !path.endsWith(':/')))) { |
||
73 | path = path.slice(0, -1); |
||
74 | } |
||
75 | |||
76 | return path; |
||
77 | }; |
||
78 | |||
79 | return object; |
||
80 | }; |
||
81 | |||
82 | class _OSPaths { |
||
83 | constructor() { |
||
84 | const OSPaths = function () { |
||
85 | return new _OSPaths(); |
||
86 | }; |
||
87 | |||
88 | this._fn = OSPaths; |
||
89 | |||
90 | // Connect to platform-specific API functions by extension |
||
91 | const extension = isWinOS ? windows() : base(); |
||
92 | Object.keys(extension).forEach(key => { |
||
93 | this._fn[key] = extension[key]; |
||
94 | }); |
||
95 | |||
96 | return this._fn; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
97 | } |
||
98 | } |
||
99 | |||
100 | module.exports = new _OSPaths(); |
||
101 |