-
Notifications
You must be signed in to change notification settings - Fork 36
/
init.git.js
263 lines (255 loc) · 10.9 KB
/
init.git.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
'use strict';
import File from './file.js';
import inquirer from 'inquirer';
import { Util } from './util.js';
import commandExists from 'command-exists';
import { simpleGit } from 'simple-git';
const git = simpleGit();
/**
* CLI helper class
*/
const Init = {
/**
* check if git repo exists and otherwise create one
*
* @returns {Promise.<{status: string, repoName: string}>} success flag
*/
async initGitRepo() {
const result = { status: null, repoName: null };
// check if git is installed (https://www.npmjs.com/package/command-exists)
if (!commandExists.sync('git')) {
Util.logger.error('Git installation not found.');
Util.logger.error(
'Please follow our tutorial on installing Git: https://github.com/Accenture/sfmc-devtools#212-install-the-git-command-line'
);
result.status = 'error';
return result;
}
// 3. test if in git repo
const gitRepoFoundInCWD = await File.pathExists('.git');
let newRepoInitialized = null;
if (gitRepoFoundInCWD) {
Util.logger.info(`✔️ Git repository found`);
newRepoInitialized = false;
} else {
Util.logger.warn('No Git repository found. Initializing git:');
Util.execSync('git', ['init', '--initial-branch=' + Util.defaultGitBranch]);
if (await File.pathExists('.git')) {
newRepoInitialized = true;
} else {
Util.logger.error(
'We detected a problem initializing your Git repository. Please run "git init" manually'
);
result.status = 'error';
return result;
}
}
Util.logger.info('Ensuring long file paths are not causing issues with git:');
try {
Util.execSync('git', ['config', '--local', 'core.longpaths', 'true']);
} catch {
Util.logger.warn(
`Updating your git config failed. We recommend running the above command manually yourself to avoid issues.`
);
}
Util.logger.info('Ensuring checkout (git pull) as-is and commit Unix-style line endings:');
try {
Util.execSync('git', ['config', '--local', 'core.autocrlf', 'input']);
} catch {
Util.logger.warn(
`Updating your git config failed. We recommend running the above command manually yourself to avoid issues.`
);
}
// offer to update local user.name and user.email
await this._updateGitConfigUser();
if (newRepoInitialized) {
// offer to insert git remote url now
result.repoName = await this._addGitRemote();
}
Util.logger.info('✔️ Git initialization done.');
result.status = newRepoInitialized ? 'init' : 'update';
return result;
},
/**
* offer to push the new repo straight to the server
*
* @returns {Promise.<void>} -
*/
async gitPush() {
const skipInteraction = Util.skipInteraction;
const gitRemotes = (await git.getRemotes(true)).filter((item) => item.name === 'origin');
if (gitRemotes.length && gitRemotes[0].refs.push) {
// check if remote repo is still empty (otherwise to risky to blindly push)
let remoteBranchesExist;
Util.logger.info('Checking remote Git repository for existing branches...');
try {
// First, we need to update our local copy of the repo
await git.fetch();
// Then, we can check how many remote branches 'git fetch' has found
remoteBranchesExist = (await git.branch(['-r'])).all.length > 0;
} catch (ex) {
Util.logger.error('Could not contact remote git server: ' + ex.message);
}
if (remoteBranchesExist === false) {
// offer git push if no remote branches found
Util.logger.info(
`Your remote Git repository is still empty and ready to store your initial backup. Hint: This is the server version of the repo which you share with your team.`
);
let responses;
if (!skipInteraction) {
responses = await inquirer.prompt([
{
type: 'confirm',
name: 'gitPush',
message: `Would you like to 'push' your backup to the remote Git repo?`,
default: true,
},
]);
}
if (skipInteraction?.gitPush === 'true' || responses?.gitPush) {
Util.execSync('git', ['push', '-u', 'origin', 'master']);
}
} else if (remoteBranchesExist === true) {
Util.logger.info(
'Your remote Git repository already contains data. Please execute a git push manually.'
);
}
}
},
/**
* offers to add the git remote origin
*
* @returns {Promise.<string>} repo name (optionally)
*/
async _addGitRemote() {
const skipInteraction = Util.skipInteraction;
// #1 ask if the user wants to do it now
let responses;
if (!skipInteraction) {
responses = await inquirer.prompt([
{
type: 'confirm',
name: 'gitOriginKnown',
message: `Do you know the remote/clone URL of your Git repo (starts with ssh:// or http:// and ends on '.git')?`,
default: true,
},
]);
}
if (skipInteraction || responses.gitOriginKnown) {
// #2 if yes, guide the user to input the right url
/* eslint-disable unicorn/prefer-ternary */
if (skipInteraction) {
responses = skipInteraction;
} else {
responses = await inquirer.prompt([
{
type: 'input',
name: 'gitRemoteUrl',
message: 'Git Remote URL',
validate: (value) => {
value = value.trim();
if (!value || value.length < 10) {
return 'Please enter a valid remote URL';
} else if (!value.startsWith('http') && !value.startsWith('ssh')) {
return `Your Git Remote URL should start with 'http' or 'ssh'`;
} else if (value.endsWith('.git')) {
// all good
return true;
} else {
return `Your Git Remote URL should end with '.git'`;
}
},
},
]);
}
/* eslint-enable unicorn/prefer-ternary */
if (typeof responses.gitRemoteUrl === 'string') {
responses.gitRemoteUrl = responses.gitRemoteUrl.trim();
Util.execSync('git', ['remote', 'add', 'origin', responses.gitRemoteUrl]);
return responses.gitRemoteUrl.split('/').pop().split('.')[0];
}
}
},
/**
* checks global config and ask to config the user info and then store it locally
*
* @returns {Promise.<void>} -
*/
async _updateGitConfigUser() {
const skipInteraction = Util.skipInteraction;
const gitUser = (await this._getGitConfigUser()) || {};
Util.logger.info(
`Please confirm your Git user name & email. It should be in the format 'FirstName LastName' and 'your.email@accenture.com'. The current (potentially wrong) values are provided as default. If correct, confirm with ENTER, otherwise please update:`
);
let responses;
/* eslint-disable unicorn/prefer-ternary */
if (skipInteraction) {
responses = {
name: gitUser['user.name'],
email: gitUser['user.email'],
};
} else {
responses = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Git user.name',
default: gitUser['user.name'] || null,
// eslint-disable-next-line jsdoc/require-jsdoc
validate: function (value) {
if (
!value ||
value.trim().length < 4 ||
value.includes('"') ||
value.includes("'")
) {
return 'Please enter valid name';
}
return true;
},
},
{
type: 'input',
name: 'email',
message: 'Git user.email',
default: gitUser['user.email'] || null,
// eslint-disable-next-line jsdoc/require-jsdoc
validate: function (value) {
value = value.trim();
const regex =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!value || !regex.test(String(value).toLowerCase())) {
return 'Please enter valid email';
}
return true;
},
},
]);
}
/* eslint-enable unicorn/prefer-ternary */
if (responses.name && responses.email) {
// name can contain spaces - wrap it in quotes
const name = `"${responses.name.trim()}"`;
const email = responses.email.trim();
try {
Util.execSync('git', ['config', '--local', 'user.name', name]);
Util.execSync('git', ['config', '--local', 'user.email', email]);
} catch (ex) {
// if project folder is not a git folder then using --local will lead to a fatal error
Util.logger.warn('- Could not update git user name and email');
Util.logger.debug(ex.message);
}
}
},
/**
* retrieves the global user.name and user.email values
*
* @returns {Promise.<{'user.name': string, 'user.email': string}>} user.name and user.email
*/
async _getGitConfigUser() {
const names = await git.getConfig('user.name');
const emails = await git.getConfig('user.email');
return { 'user.name': names.value || '', 'user.email': emails.value || '' };
},
};
export default Init;