Skip to content

Commit d97bee6

Browse files
RobinCslGatsbyJS Bot
authored and
GatsbyJS Bot
committed
feat(gatsby-core-utils): Add isCI and getCIName (#19039)
Fixes #19018
1 parent 54565f0 commit d97bee6

File tree

14 files changed

+64
-20
lines changed

14 files changed

+64
-20
lines changed

packages/gatsby-cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"better-opn": "^0.1.4",
1717
"bluebird": "^3.7.1",
1818
"chalk": "^2.4.2",
19-
"ci-info": "^2.0.0",
2019
"clipboardy": "^2.1.0",
2120
"common-tags": "^1.8.0",
2221
"configstore": "^5.0.0",
@@ -26,6 +25,7 @@
2625
"execa": "^2.1.0",
2726
"fs-exists-cached": "^1.0.0",
2827
"fs-extra": "^8.1.0",
28+
"gatsby-core-utils": "^1.0.15",
2929
"gatsby-telemetry": "^1.1.34",
3030
"hosted-git-info": "^3.0.2",
3131
"is-valid-path": "^0.1.1",

packages/gatsby-cli/src/reporter/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22

33
const semver = require(`semver`)
4-
const { isCI } = require(`ci-info`)
4+
const { isCI } = require(`gatsby-core-utils`)
55
const signalExit = require(`signal-exit`)
66
const reporterActions = require(`./redux/actions`)
77

@@ -17,7 +17,7 @@ if (!process.env.GATSBY_LOGGER) {
1717
if (
1818
inkExists &&
1919
semver.satisfies(process.version, `>=8`) &&
20-
!isCI &&
20+
!isCI() &&
2121
typeof jest === `undefined`
2222
) {
2323
process.env.GATSBY_LOGGER = `ink`

packages/gatsby-cli/src/util/__tests__/is-tty.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ describe(`isTTY`, () => {
1111

1212
it(`returns true if not on ci & TTY is enabled`, () => {
1313
process.stdout.isTTY = true
14-
jest.mock(`ci-info`, () => {
15-
return { isCI: false }
14+
jest.mock(`gatsby-core-utils`, () => {
15+
return { isCI: () => false }
1616
})
1717
const isTTY = require(`../is-tty`)
1818
expect(isTTY()).toBe(true)
1919
})
2020

2121
it(`returns false if not on ci & TTY is disabled`, () => {
2222
process.stdout.isTTY = false
23-
jest.mock(`ci-info`, () => {
24-
return { isCI: false }
23+
jest.mock(`gatsby-core-utils`, () => {
24+
return { isCI: () => false }
2525
})
2626
const isTTY = require(`../is-tty`)
2727
expect(isTTY()).toBe(false)
2828
})
2929

3030
it(`returns false if on ci & TTY is enabled`, () => {
3131
process.stdout.isTTY = true
32-
jest.mock(`ci-info`, () => {
33-
return { isCI: true }
32+
jest.mock(`gatsby-core-utils`, () => {
33+
return { isCI: () => true }
3434
})
3535
const isTTY = require(`../is-tty`)
3636
expect(isTTY()).toBe(false)
3737
})
3838

3939
it(`returns false if on ci & TTY is disabled`, () => {
4040
process.stdout.isTTY = false
41-
jest.mock(`ci-info`, () => {
42-
return { isCI: true }
41+
jest.mock(`gatsby-core-utils`, () => {
42+
return { isCI: () => true }
4343
})
4444
const isTTY = require(`../is-tty`)
4545
expect(isTTY()).toBe(false)
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const isCI = require(`ci-info`).isCI
1+
const { isCI } = require(`gatsby-core-utils`)
22

33
// Some CI pipelines incorrectly report process.stdout.isTTY status,
44
// which causes unwanted lines in the output. An additional check for isCI helps.
55
// @see https://github.com/prettier/prettier/blob/36aeb4ce4f620023c8174e826d7208c0c64f1a0b/src/utils/is-tty.js
6-
module.exports = () => process.stdout.isTTY && !isCI
6+
module.exports = () => process.stdout.isTTY && !isCI()

packages/gatsby-core-utils/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,30 @@ const pathname = "./gatsby/is/awesome"
6363
const url = joinPath(BASEPATH, pathname)
6464
// ...
6565
```
66+
67+
### isCI
68+
69+
A utility that enhances `isCI` from 'ci-info` with support for ZEIT's Now and Heroku detection
70+
71+
```js
72+
const { isCI } = require("gatsby-core-utils")
73+
74+
if (isCI()) {
75+
// execute CI-specific code
76+
}
77+
// ...
78+
```
79+
80+
### getCIName
81+
82+
A utility that returns the name of the current CI environment if available, `null` otherwise
83+
84+
```js
85+
const { getCIName } = require("gatsby-core-utils")
86+
87+
const CI_NAME = getCIName()
88+
console.log({ CI_NAME })
89+
// {CI_NAME: null}, or
90+
// {CI_NAME: "ZEIT Now"}
91+
// ...
92+
```

packages/gatsby-core-utils/index.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ export declare function cpuCoreCount(ignoreEnvVar?: boolean): number
3131
* @param {string[]} segments A sequence of segments
3232
*/
3333
export declare function urlResolve(...segments: string[]): string
34+
35+
/**
36+
* Determines whether the environment where the code is running is in CI
37+
* @return {boolean} true if the environment is in CI, false otherwise
38+
*/
39+
export declare function isCI(): boolean
40+
41+
/**
42+
* Gets the name of the CI environment (e.g. "ZEIT Now", "Heroku", etc.)
43+
* @return {string | null} The name of the CI if available. Defaults to null if not in CI
44+
*/
45+
export declare function getCIName(): string | null

packages/gatsby-core-utils/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"index.d.ts"
2929
],
3030
"types": "index.d.ts",
31+
"dependencies": {
32+
"ci-info": "2.0.0"
33+
},
3134
"devDependencies": {
3235
"@babel/cli": "^7.6.4",
3336
"@babel/core": "^7.6.4",

packages/gatsby-core-utils/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ exports.joinPath = require(`./path`).joinPath
33
exports.isNodeInternalModulePath = require(`./path`).isNodeInternalModulePath
44
exports.cpuCoreCount = require(`./cpu-core-count`)
55
exports.urlResolve = require(`./url`).resolve
6+
exports.isCI = require(`./ci`).isCI
7+
exports.getCIName = require(`./ci`).getCIName

packages/gatsby-telemetry/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
"@babel/runtime": "^7.6.3",
1212
"bluebird": "^3.7.1",
1313
"boxen": "^3.2.0",
14-
"ci-info": "2.0.0",
1514
"configstore": "^5.0.0",
1615
"envinfo": "^5.12.1",
1716
"fs-extra": "^8.1.0",
17+
"gatsby-core-utils": "^1.0.15",
1818
"git-up": "4.0.1",
1919
"is-docker": "2.0.0",
2020
"lodash": "^4.17.15",

packages/gatsby-telemetry/src/postinstall.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ try {
22
const showAnalyticsNotification = require(`./showAnalyticsNotification`)
33
const EventStorage = require(`./event-storage`)
44

5-
const ci = require(`ci-info`)
5+
const { isCI } = require(`gatsby-core-utils`)
66
const eventStorage = new EventStorage()
77
const disabled = eventStorage.disabled
88
const enabledInConfig = eventStorage.getConfig(`telemetry.enabled`)
9-
if (enabledInConfig === undefined && !disabled && !ci.isCI) {
9+
if (enabledInConfig === undefined && !disabled && !isCI()) {
1010
showAnalyticsNotification()
1111
}
1212
} catch (e) {

packages/gatsby-telemetry/src/telemetry.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const uuidv4 = require(`uuid/v4`)
22
const EventStorage = require(`./event-storage`)
33
const { cleanPaths } = require(`./error-helpers`)
4-
const ci = require(`./ci`)
4+
const { isCI, getCIName } = require(`gatsby-core-utils`)
55
const os = require(`os`)
66
const { join, sep } = require(`path`)
77
const isDocker = require(`is-docker`)
@@ -208,7 +208,7 @@ module.exports = class AnalyticsTracker {
208208
}
209209
let enabled = this.store.getConfig(`telemetry.enabled`)
210210
if (enabled === undefined || enabled === null) {
211-
if (!ci.isCI()) {
211+
if (!isCI()) {
212212
showAnalyticsNotification()
213213
}
214214
enabled = true
@@ -229,8 +229,8 @@ module.exports = class AnalyticsTracker {
229229
release: os.release(),
230230
cpus: (cpus && cpus.length > 0 && cpus[0].model) || undefined,
231231
arch: os.arch(),
232-
ci: ci.isCI(),
233-
ciName: ci.getCIName(),
232+
ci: isCI(),
233+
ciName: getCIName(),
234234
docker: isDocker(),
235235
}
236236
this.osInfo = osInfo

0 commit comments

Comments
 (0)