-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathspawnHelper.js
More file actions
157 lines (143 loc) · 4.22 KB
/
spawnHelper.js
File metadata and controls
157 lines (143 loc) · 4.22 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { spawn as nodeSpawn } from 'child_process';
import R from 'ramda';
import {
createCredRequestId,
clearUsernameAndPassword,
ensureAuthServer,
getAuthServerPort,
getNodeBinaryPath,
storeUsernameAndPassword,
getGitAskPassPath,
getGitAskPassClientPath
} from './authService';
import {
regex
} from '../constants';
import { combineShellOptions } from './shellOptions';
const parseUrlFromErrorMessage = (errorMessage) => {
let url = null;
const matches = regex.CREDENTIALS_NOT_FOUND.exec(errorMessage);
if (matches && matches.length > 1) {
([, url] = matches);
}
return url;
};
const spawnCommand = (command, opts, stdin = '') => new Promise((resolve, reject) => {
const [cmd, ...args] = command.trim().split(' ');
const childProcess = nodeSpawn(cmd, args, R.mergeDeepRight(opts, { stdio: 'pipe' }));
const stdoutData = [];
const stderrData = [];
const makeDataAccumulator = (accumulator) => (data) => {
accumulator.push(data);
};
childProcess.stdout.on('data', makeDataAccumulator(stdoutData));
childProcess.stderr.on('data', makeDataAccumulator(stderrData));
childProcess.on('error', () => {
reject({ status: null, stdout: '', stderr: '' });
});
childProcess.on('close', (status) => {
const stdout = Buffer.concat(stdoutData);
const stderr = Buffer.concat(stderrData);
resolve({ status, stdout, stderr });
});
childProcess.stdin.write(stdin);
childProcess.stdin.end();
});
const spawn = async (command, stdin, opts = {}, credentialsCallback, repoPath = null) => {
const resolvedStdin = stdin || '';
const resolvedRepoPath = repoPath || R.path('cwd', opts);
const noAuthResult = await spawnCommand(
command,
combineShellOptions(
opts,
{
env: {
GIT_TERMINAL_PROMPT: 0
}
}
),
resolvedStdin
);
if (noAuthResult.status === 0) {
// then we're done, return the data
return { stdout: noAuthResult.stdout };
}
const errorMessage = noAuthResult.stderr.toString();
if (!regex.CREDENTIALS_ERROR.test(errorMessage)) {
throw new Error(errorMessage);
}
await ensureAuthServer();
const url = parseUrlFromErrorMessage(errorMessage);
const credRequestId = createCredRequestId(resolvedRepoPath);
const tryCredentialsUntilCanceled = async () => {
const { username, password } = await credentialsCallback({
type: 'CREDS_REQUESTED',
credRequestId,
repoPath: resolvedRepoPath,
url
});
storeUsernameAndPassword(credRequestId, username, password);
try {
const authResult = await spawnCommand(
command,
combineShellOptions(
opts,
{
env: {
ELECTRON_RUN_AS_NODE: 1,
GIT_TERMINAL_PROMPT: 0,
GIT_ASKPASS: getGitAskPassPath(),
NODEGIT_LFS_ASKPASS_STATE: credRequestId,
NODEGIT_LFS_ASKPASS_PORT: getAuthServerPort(),
NODEGIT_LFS_ASKPASS_PATH: getGitAskPassClientPath(),
NODEGIT_LFS_NODE_PATH: getNodeBinaryPath()
}
}
),
resolvedStdin
);
if (authResult.status === 0) {
await credentialsCallback({
type: 'CREDS_SUCCEEDED',
credRequestId,
repoPath: resolvedRepoPath,
verifiedCredentials: { username, password },
url
});
clearUsernameAndPassword(credRequestId);
return { stdout: authResult.stdout };
}
const stderr = authResult.stderr.toString();
if (regex.CREDENTIALS_ERROR.test(stderr)) {
const authError = new Error('Auth error');
authError.isAuthError = true;
throw authError;
}
throw new Error(stderr);
} catch (e) {
if (e.isAuthError) {
clearUsernameAndPassword(credRequestId);
await credentialsCallback({
type: 'CREDS_FAILED',
credRequestId,
repoPath: resolvedRepoPath,
url
});
return tryCredentialsUntilCanceled();
}
throw e;
}
};
try {
return await tryCredentialsUntilCanceled();
} catch (e) {
await credentialsCallback({
type: 'CREDS_SPAWN_FAILED',
error: e,
credRequestId,
repoPath
});
throw e;
}
};
export default spawn;