-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathoptions.js
57 lines (50 loc) · 1.9 KB
/
options.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
var childProcess = require('child_process');
var gitUrlParse = require('git-url-parse');
var readlineSync = require('readline-sync');
var exec = childProcess.exec;
var execSync = childProcess.execSync;
var projectDir = process.cwd();
var git = {
config: {
get: function (key) {
return execSync('git config --get ' + key + ' || true', { cwd: projectDir }).toString().trim();
},
set: function (key, value) {
execSync('git config --add ' + key + ' ' + value, { cwd: projectDir });
},
}
};
var options = (function () {
var options = {
host: git.config.get('gitlab.url') || process.env.GITLAB_URL,
token: git.config.get('gitlab.token') || process.env.GITLAB_TOKEN,
};
if (!options.host) {
var defaultInput = (function () {
var url = git.config.get('remote.origin.url');
if (!url || url.indexOf('bitbucket') !== -1 || url.indexOf('github') !== -1) {
url = 'https://gitlab.com';
}
return 'https://' + gitUrlParse(url).resource;
})();
var urlQuestion = ('Enter GitLab URL (' + defaultInput + '): ').yellow;
while (!options.host) {
options.host = readlineSync.question(urlQuestion, { defaultInput: defaultInput });
urlQuestion = 'Invalid URL (try again): '.red;
}
git.config.set('gitlab.url', options.host);
}
if (!options.token) {
var url = options.host + '/profile/personal_access_tokens';
console.log('A personal access token is needed to use the GitLab API\n' + url.grey);
var tokenQuestion = 'Enter personal access token: '.yellow;
while (!options.token) {
options.token = readlineSync.question(tokenQuestion);
tokenQuestion = 'Invalid token (try again): '.red;
}
git.config.set('gitlab.token', options.token);
}
options.rejectUnauthorized = false;
return options;
})();
module.exports = options