-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-helpers.js
126 lines (109 loc) · 3.5 KB
/
config-helpers.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
const proxyProc = require('child_process');
const exec = require('shelljs').exec;
const urlSafeString = require('url-safe-string');
const camelify = require('camelify');
const safeCamelString = (str) => {
return camelify(new urlSafeString().generate(str));
}
const getGitBranch = async (branchName) => {
if (branchName) {
branchName = branchName.replace('refs/heads/', '').replace('refs/tags/', '');
return Promise.resolve(safeCamelString(branchName));
}
return new Promise((resolve, reject) => {
proxyProc.exec('git rev-parse --abbrev-ref HEAD', (err, stdout, stderr) => {
if (err) {
reject(err);
return;
}
if (typeof stdout === 'string') {
const result = safeCamelString(stdout.trim());
resolve(result);
}
});
});
}
const runProcess = async (command, suppressExit = false) => {
return new Promise((resolve, _reject) => {
const proc = exec(command, { async:true });
proc.on('exit', function (code) {
if (!suppressExit){
console.log('child process exited with code ' + code.toString());
}
resolve(code);
});
});
}
const getEnvironmentBonafides = async (options, resolveVariable) => {
const cliStage = (await resolveVariable('self:provider.stage')) || '';
let result;
switch (cliStage.toLowerCase()) {
case 'dev':
case 'stg':
case 'tst':
case 'uat':
case 'prod':
result = {
isEphemeral: false, // is path-to-production
isProd: cliStage.toLowerCase() === 'prod',
key: cliStage.toLowerCase(),
};
break;
default:
result = {
isEphemeral: true, // is branch
isProd: false,
key: 'nonprod'
}
break;
}
return Promise.resolve(result);
}
const getUsername = () => {
return safeCamelString(process.env.GITHUB_ACTOR || require('os').userInfo().username).toLowerCase();
}
const getStackName = async (options, resolveVariable) => {
const environment = await getEnvironmentBonafides(options, resolveVariable);
let cliStage = await resolveVariable('self:provider.stage');
const service = await resolveVariable('self:service');
const username = getUsername();
const branchName = await getGitBranch(process.env.GITHUB_REF);
let stackSuffix = branchName.toLowerCase();
const segments = [service];
if (environment.isEphemeral) {
segments.push(username);
segments.push(stackSuffix);
} else {
segments.push(cliStage);
}
return segments.join('-').toLowerCase();
}
const getFQDN = async (options, resolveVariable) => {
const environment = await getEnvironmentBonafides(options, resolveVariable);
const branchName = await getGitBranch(process.env.GITHUB_REF);
const domainName = await resolveVariable('self:custom.domain-name');
const prodCNAME = await resolveVariable('self:custom.prod-CNAME')
// non-master branch
if (environment.isEphemeral) {
return Promise.resolve(`${getUsername()}-${branchName}.${domainName}`.toLowerCase());
}
// long-lived non-prod branch
if (!environment.isProd) {
return Promise.resolve(`${environment.key}.${domainName}`.toLowerCase());
}
// prod
return Promise.resolve(`${prodCNAME}.${domainName}`.toLowerCase());
}
const getEnvironmentKey = async (options, resolveVariable) => {
const environment = await getEnvironmentBonafides(options, resolveVariable);
return environment.key;
}
module.exports = {
getUsername,
getEnvironmentKey,
getFQDN,
getGitBranch,
getEnvironmentBonafides,
runProcess,
getStackName,
}