-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
140 lines (109 loc) · 2.53 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import process from 'node:process';
import mem from 'mem';
import {execa} from 'execa';
import {passwdUser} from 'passwd-user';
import pAny from 'p-any';
import {includeKeys} from 'filter-obj';
import rc from 'rc';
const environmentVariables = [
'GIT_AUTHOR_NAME',
'GIT_COMMITTER_NAME',
'HGUSER', // Mercurial
'C9_USER', // Cloud9
];
/* eslint-disable unicorn/error-message */
async function checkEnv() {
const {env} = process;
const variableName = environmentVariables.find(variable => env[variable]);
const fullName = variableName && env[variableName];
if (!fullName) {
throw new Error();
}
return fullName;
}
async function checkAuthorName() {
const fullName = rc('npm')['init-author-name'];
if (!fullName) {
throw new Error();
}
return fullName;
}
async function checkPasswd() {
const user = await passwdUser();
if (!user.fullName) {
throw new Error();
}
return user.fullName;
}
async function checkGit() {
const {stdout: fullName} = await execa('git', [
'config',
'--global',
'user.name',
]);
if (!fullName) {
throw new Error();
}
return fullName;
}
async function checkOsaScript() {
const {stdout: fullName} = await execa('osascript', [
'-e',
'long user name of (system info)',
]);
if (!fullName) {
throw new Error();
}
return fullName;
}
async function checkWmic() {
const {stdout} = await execa('wmic', [
'useraccount',
'where',
`name="${process.env.USERNAME}"`,
'get',
'fullname',
]);
const fullName = stdout.replace('FullName', '').trim();
if (!fullName) {
throw new Error();
}
return fullName;
}
async function checkGetEnt() {
const result = await execa('getent passwd $(whoami)', {shell: true});
const fullName = (result.stdout.split(':')[4] || '').replace(/,.*/, '');
if (!fullName) {
throw new Error();
}
return fullName;
}
/* eslint-enable unicorn/error-message */
async function fallback() {
if (process.platform === 'darwin') {
return pAny([checkPasswd(), checkOsaScript()]);
}
if (process.platform === 'win32') {
// The full name is usually not set by default in the system on Windows 7+
return pAny([checkGit(), checkWmic()]);
}
return pAny([checkPasswd(), checkGetEnt(), checkGit()]);
}
async function getFullName() {
try {
return await checkEnv();
} catch {}
try {
return await checkAuthorName();
} catch {}
try {
return await fallback();
} catch {}
}
const fullName = mem(getFullName, {
cachePromiseRejection: false,
cacheKey() {
return JSON.stringify(includeKeys(process.env, environmentVariables));
},
});
export default fullName;