Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-core-utils): Add isCI and getCIName #19039

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"better-opn": "^0.1.4",
"bluebird": "^3.7.1",
"chalk": "^2.4.2",
"ci-info": "^2.0.0",
"clipboardy": "^2.1.0",
"common-tags": "^1.8.0",
"configstore": "^5.0.0",
Expand All @@ -26,6 +25,7 @@
"execa": "^2.1.0",
"fs-exists-cached": "^1.0.0",
"fs-extra": "^8.1.0",
"gatsby-core-utils": "^1.0.15",
"gatsby-telemetry": "^1.1.34",
"hosted-git-info": "^3.0.2",
"is-valid-path": "^0.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-cli/src/reporter/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

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

Expand All @@ -17,7 +17,7 @@ if (!process.env.GATSBY_LOGGER) {
if (
inkExists &&
semver.satisfies(process.version, `>=8`) &&
!isCI &&
!isCI() &&
typeof jest === `undefined`
) {
process.env.GATSBY_LOGGER = `ink`
Expand Down
16 changes: 8 additions & 8 deletions packages/gatsby-cli/src/util/__tests__/is-tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ describe(`isTTY`, () => {

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

it(`returns false if not on ci & TTY is disabled`, () => {
process.stdout.isTTY = false
jest.mock(`ci-info`, () => {
return { isCI: false }
jest.mock(`gatsby-core-utils`, () => {
return { isCI: () => false }
})
const isTTY = require(`../is-tty`)
expect(isTTY()).toBe(false)
})

it(`returns false if on ci & TTY is enabled`, () => {
process.stdout.isTTY = true
jest.mock(`ci-info`, () => {
return { isCI: true }
jest.mock(`gatsby-core-utils`, () => {
return { isCI: () => true }
})
const isTTY = require(`../is-tty`)
expect(isTTY()).toBe(false)
})

it(`returns false if on ci & TTY is disabled`, () => {
process.stdout.isTTY = false
jest.mock(`ci-info`, () => {
return { isCI: true }
jest.mock(`gatsby-core-utils`, () => {
return { isCI: () => true }
})
const isTTY = require(`../is-tty`)
expect(isTTY()).toBe(false)
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-cli/src/util/is-tty.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const isCI = require(`ci-info`).isCI
const { isCI } = require(`gatsby-core-utils`)

// Some CI pipelines incorrectly report process.stdout.isTTY status,
// which causes unwanted lines in the output. An additional check for isCI helps.
// @see https://github.com/prettier/prettier/blob/36aeb4ce4f620023c8174e826d7208c0c64f1a0b/src/utils/is-tty.js
module.exports = () => process.stdout.isTTY && !isCI
module.exports = () => process.stdout.isTTY && !isCI()
27 changes: 27 additions & 0 deletions packages/gatsby-core-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,30 @@ const pathname = "./gatsby/is/awesome"
const url = joinPath(BASEPATH, pathname)
// ...
```

### isCI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We love it when folks update documentation! 🥇 for that! 🤗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My pleasure :)


A utility that enhances `isCI` from 'ci-info` with support for ZEIT's Now and Heroku detection

```js
const { isCI } = require("gatsby-core-utils")

if (isCI()) {
// execute CI-specific code
}
// ...
```

### getCIName

A utility that returns the name of the current CI environment if available, `null` otherwise

```js
const { getCIName } = require("gatsby-core-utils")

const CI_NAME = getCIName()
console.log({ CI_NAME })
// {CI_NAME: null}, or
// {CI_NAME: "ZEIT Now"}
// ...
```
12 changes: 12 additions & 0 deletions packages/gatsby-core-utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ export declare function cpuCoreCount(ignoreEnvVar?: boolean): number
* @param {string[]} segments A sequence of segments
*/
export declare function urlResolve(...segments: string[]): string

/**
* Determines whether the environment where the code is running is in CI
* @return {boolean} true if the environment is in CI, false otherwise
*/
export declare function isCI(): boolean

/**
* Gets the name of the CI environment (e.g. "ZEIT Now", "Heroku", etc.)
* @return {string | null} The name of the CI if available. Defaults to null if not in CI
*/
export declare function getCIName(): string | null
3 changes: 3 additions & 0 deletions packages/gatsby-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"index.d.ts"
],
"types": "index.d.ts",
"dependencies": {
"ci-info": "2.0.0"
},
"devDependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-core-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ exports.joinPath = require(`./path`).joinPath
exports.isNodeInternalModulePath = require(`./path`).isNodeInternalModulePath
exports.cpuCoreCount = require(`./cpu-core-count`)
exports.urlResolve = require(`./url`).resolve
exports.isCI = require(`./ci`).isCI
exports.getCIName = require(`./ci`).getCIName
2 changes: 1 addition & 1 deletion packages/gatsby-telemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"@babel/runtime": "^7.6.3",
"bluebird": "^3.7.1",
"boxen": "^3.2.0",
"ci-info": "2.0.0",
"configstore": "^5.0.0",
"envinfo": "^5.12.1",
"fs-extra": "^8.1.0",
"gatsby-core-utils": "^1.0.15",
"git-up": "4.0.1",
"is-docker": "2.0.0",
"lodash": "^4.17.15",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-telemetry/src/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ try {
const showAnalyticsNotification = require(`./showAnalyticsNotification`)
const EventStorage = require(`./event-storage`)

const ci = require(`ci-info`)
const { isCI } = require(`gatsby-core-utils`)
const eventStorage = new EventStorage()
const disabled = eventStorage.disabled
const enabledInConfig = eventStorage.getConfig(`telemetry.enabled`)
if (enabledInConfig === undefined && !disabled && !ci.isCI) {
if (enabledInConfig === undefined && !disabled && !isCI()) {
showAnalyticsNotification()
}
} catch (e) {
Expand Down
8 changes: 4 additions & 4 deletions packages/gatsby-telemetry/src/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const uuidv4 = require(`uuid/v4`)
const EventStorage = require(`./event-storage`)
const { cleanPaths } = require(`./error-helpers`)
const ci = require(`./ci`)
const { isCI, getCIName } = require(`gatsby-core-utils`)
const os = require(`os`)
const { join, sep } = require(`path`)
const isDocker = require(`is-docker`)
Expand Down Expand Up @@ -208,7 +208,7 @@ module.exports = class AnalyticsTracker {
}
let enabled = this.store.getConfig(`telemetry.enabled`)
if (enabled === undefined || enabled === null) {
if (!ci.isCI()) {
if (!isCI()) {
showAnalyticsNotification()
}
enabled = true
Expand All @@ -229,8 +229,8 @@ module.exports = class AnalyticsTracker {
release: os.release(),
cpus: (cpus && cpus.length > 0 && cpus[0].model) || undefined,
arch: os.arch(),
ci: ci.isCI(),
ciName: ci.getCIName(),
ci: isCI(),
ciName: getCIName(),
docker: isDocker(),
}
this.osInfo = osInfo
Expand Down