|
1
|
import ci from "ci-info" |
|
2
|
|
|
3
|
const CI_DEFINITIONS = [ |
|
4
|
getEnvDetect({ key: `NOW_BUILDER_ANNOTATE`, name: `ZEIT Now` }), |
|
5
|
getEnvDetect({ key: `NOW_REGION`, name: `ZEIT Now v1` }), |
|
6
|
getEnvDetect({ key: `VERCEL_URL`, name: `Vercel Now` }), |
|
7
|
getEnvDetect({ key: `NOW_BUILDER`, name: `Vercel Now` }), |
|
8
|
getEnvDetect({ key: `VERCEL_BUILDER`, name: `Vercel Now` }), |
|
9
|
getEnvDetect({ key: `CODESANDBOX_SSE`, name: `CodeSandbox` }), |
|
10
|
getEnvDetect({ key: `GITHUB_ACTIONS`, name: `GitHub Actions` }), |
|
11
|
getEnvDetect({ key: `CIRCLE_BRANCH`, name: `CircleCI` }), |
|
12
|
getEnvDetect({ key: `CIRCLECI`, name: `CircleCI` }), |
|
13
|
envFromCIAndCIName, |
|
14
|
herokuDetect, |
|
15
|
getEnvFromCIInfo, |
|
16
|
envFromCIWithNoName, |
|
17
|
] |
|
18
|
|
|
19
|
function lookupCI() { |
|
20
|
for (const fn of CI_DEFINITIONS) { |
|
21
|
try { |
|
22
|
const res = fn() |
|
23
|
if (res) { |
|
24
|
return res |
|
25
|
} |
|
26
|
} catch (e) { |
|
27
|
// ignore |
|
28
|
} |
|
29
|
} |
|
30
|
return null |
|
31
|
} |
|
32
|
const CIName = lookupCI() |
|
33
|
|
|
34
|
/** |
|
35
|
* Determines whether the environment where the code is running is in CI |
|
36
|
* @return true if the environment is in CI, false otherwise |
|
37
|
*/ |
|
38
|
|
|
39
|
export function isCI() { |
|
40
|
return !!CIName |
|
41
|
} |
|
42
|
|
|
43
|
/** |
|
44
|
* Gets the name of the CI environment (e.g. "Vercel", "Heroku", etc.) |
|
45
|
* @return The name of the CI if available. Defaults to null if not in CI |
|
46
|
*/ |
|
47
|
|
|
48
|
export function getCIName() { |
|
49
|
if (!isCI()) { |
|
50
|
return null |
|
51
|
} |
|
52
|
return CIName |
|
53
|
} |
|
54
|
|
|
55
|
function getEnvFromCIInfo() { |
|
56
|
if (ci.isCI) return ci.name || `ci-info detected w/o name` |
|
57
|
return null |
|
58
|
} |
|
59
|
|
|
60
|
function getEnvDetect({ key, name }) { |
|
61
|
return function() { |
|
62
|
if (process.env[key]) { |
|
63
|
return name |
|
64
|
} |
|
65
|
return null |
|
66
|
} |
|
67
|
} |
|
68
|
|
|
69
|
function herokuDetect() { |
|
70
|
return ( |
|
71
|
typeof process.env.NODE === `string` && |
|
72
|
/\.heroku\/node\/bin\/node/.test(process.env.NODE) && |
|
73
|
`Heroku` |
|
74
|
) |
|
75
|
} |
|
76
|
|
|
77
|
function envFromCIAndCIName() { |
|
78
|
if (process.env.CI_NAME && process.env.CI) { |
|
79
|
return process.env.CI_NAME |
|
80
|
} |
|
81
|
return null |
|
82
|
} |
|
83
|
|
|
84
|
function envFromCIWithNoName() { |
|
85
|
if (process.env.CI) { |
|
86
|
return `CI detected without name` |
|
87
|
} |
|
88
|
return null |
|
89
|
} |