-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
parse_config.js
103 lines (83 loc) · 2.31 KB
/
parse_config.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
'use strict';
const rRepoURL = /^(?:(git|https?|git\+https|git\+ssh):\/\/)?(?:[^@]+@)?([^\/]+?)[\/:](.+?)\.git$/; // eslint-disable-line no-useless-escape
const rGithubPage = /\.github\.(io|com)$/;
const { URL } = require('url');
function parseObjRepo(repo) {
let url = repo.url;
let branch = repo.branch;
const configToken = repo.token;
if (!branch) {
branch = testBranch(url);
}
if (rRepoURL.test(url)) {
const match = url.match(rRepoURL);
const scheme = match[1];
if (configToken && (scheme === 'http' || scheme === 'https')) {
let repoUrl, userToken;
try {
repoUrl = new URL(url);
} catch (e) {
throw new TypeError('Fail to parse your repo url, check your config!');
}
if (configToken.startsWith('$')) {
userToken = process.env[configToken.substring(1)];
if (!userToken) throw new TypeError('Fail to read environment varable: ' + configToken + ', check your config!');
} else {
userToken = configToken;
}
repoUrl.username = userToken;
url = repoUrl.href;
}
}
return {
url: url,
branch: branch || 'master'
};
}
function parseStrRepo(repo) {
const split = repo.split(',');
const url = split.shift();
let branch = split[0];
if (!branch) {
branch = testBranch(url);
}
return {
url: url,
branch: branch || 'master'
};
}
function testBranch(repoUrl) {
let branch;
if (rRepoURL.test(repoUrl)) {
const match = repoUrl.match(rRepoURL);
const host = match[2];
const path = match[3];
if (host === 'github.com') {
branch = rGithubPage.test(path) ? 'master' : 'gh-pages';
} else if (host === 'coding.net') {
branch = 'coding-pages';
}
}
return branch;
}
module.exports = function(args) {
const repo = args.repo || args.repository;
if (!repo) throw new TypeError('repo is required!');
if (typeof repo === 'string') {
const data = parseStrRepo(repo);
data.branch = args.branch || data.branch;
return [data];
}
const keys = Object.keys(repo);
if (keys.includes('url')) {
return [parseObjRepo(repo)];
}
return keys.map(key => {
const repoItem = repo[key];
if (typeof repoItem === 'string') {
const data = parseStrRepo(repoItem);
return data;
}
return parseObjRepo(repo[key]);
});
};