/
ci.ts
92 lines (81 loc) · 1.96 KB
/
ci.ts
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
import ci from "ci-info"
const CI_DEFINITIONS = [
envFromCIAndCIName,
getEnvDetect({ key: `NOW_BUILDER_ANNOTATE`, name: `ZEIT Now` }),
getEnvDetect({ key: `NOW_REGION`, name: `ZEIT Now v1` }),
getEnvDetect({ key: `VERCEL_URL`, name: `Vercel Now` }),
getEnvDetect({ key: `NOW_BUILDER`, name: `Vercel Now` }),
getEnvDetect({ key: `VERCEL_BUILDER`, name: `Vercel Now` }),
getEnvDetect({ key: `CODESANDBOX_SSE`, name: `CodeSandbox` }),
herokuDetect,
getEnvFromCIInfo,
envFromCIWithNoName,
]
function lookupCI(): string | null {
for (const fn of CI_DEFINITIONS) {
try {
const res = fn()
if (res) {
return res
}
} catch (e) {
// ignore
}
}
return null
}
const CIName = lookupCI()
/**
* Determines whether the environment where the code is running is in CI
* @return true if the environment is in CI, false otherwise
*/
export function isCI(): boolean {
return !!CIName
}
/**
* Gets the name of the CI environment (e.g. "Vercel", "Heroku", etc.)
* @return The name of the CI if available. Defaults to null if not in CI
*/
export function getCIName(): string | null {
if (!isCI()) {
return null
}
return CIName
}
function getEnvFromCIInfo(): string | null {
if (ci.isCI) return ci.name || `ci-info detected w/o name`
return null
}
function getEnvDetect({
key,
name,
}: {
key: string
name: string
}): () => string | null {
return function (): string | null {
if (process.env[key]) {
return name
}
return null
}
}
function herokuDetect(): false | "Heroku" {
return (
typeof process.env.NODE === `string` &&
/\.heroku\/node\/bin\/node/.test(process.env.NODE) &&
`Heroku`
)
}
function envFromCIAndCIName(): string | null {
if (process.env.CI_NAME && process.env.CI) {
return process.env.CI_NAME
}
return null
}
function envFromCIWithNoName(): "CI detected without name" | null {
if (process.env.CI) {
return `CI detected without name`
}
return null
}