-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.js
74 lines (61 loc) · 1.85 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module.exports = { createOptions };
function createOptions(passedOptions) {
const options = extendPassedOptions(passedOptions);
validateOptions(options);
return options;
}
function extendPassedOptions(options) {
return extendWithDefaultOptions(
extendWithGithubTokenVariable(
extendWithCircleVariablesIfCircle(extendWithGithubActionsVariablesIfGithubActions(options)),
),
);
}
function extendWithDefaultOptions(options) {
return {
directory: 'public',
branch: 'master',
defaultBranch: 'master',
dotfiles: false,
verbose: false,
beforeAdd: null,
...options,
};
}
function extendWithGithubTokenVariable(options) {
return process.env.GITHUB_TOKEN ? { token: process.env.GITHUB_TOKEN, ...options } : options;
}
function extendWithGithubActionsVariablesIfGithubActions(options) {
if (!process.env.GITHUB_WORKFLOW) {
return options;
}
const [owner, ...repoParts] = process.env.GITHUB_REPOSITORY.split('/');
const repo = repoParts.join('/');
const branch = process.env.GITHUB_REF.replace('refs/heads/', '');
const buildUrl = `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
return {
owner,
repo,
branch,
buildUrl,
...options,
};
}
function extendWithCircleVariablesIfCircle(options) {
return process.env.CIRCLECI
? {
owner: process.env.CIRCLE_PROJECT_USERNAME,
repo: process.env.CIRCLE_PROJECT_REPONAME,
branch: process.env.CIRCLE_BRANCH,
buildUrl: process.env.CIRCLE_BUILD_URL,
...options,
}
: options;
}
function validateOptions(options) {
const requiredOptions = ['token', 'owner', 'repo', 'branch'];
const missingOptions = requiredOptions.filter(name => !options[name]);
if (missingOptions.length > 0) {
throw new Error(`Missing options: ${missingOptions.join(', ')}`);
}
}