Skip to content

Commit

Permalink
chore(gatsby-core-utils): ci to TS (#22047)
Browse files Browse the repository at this point in the history
* chore(gatsby-core-utils): package.json added type

* chore(gatsby-core-utils): mocks convert ci-info to TS

* chore(gatsby-core-utils): ci to TS

* chore(gatsby-core-utils): add @types/ci-info

* chore(gatsby-core-utils): re-install dependencies to fix order
  • Loading branch information
danielkov committed Mar 9, 2020
1 parent 69fec7b commit 5dafd4f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"directory": "packages/gatsby-core-utils"
},
"scripts": {
"build": "babel src --out-dir dist/ --ignore **/__tests__",
"build": "babel src --out-dir dist/ --ignore **/__tests__ --ignore **/__mocks__",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__"
},
Expand All @@ -36,6 +36,7 @@
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@types/ci-info": "2.0.0",
"babel-preset-gatsby-package": "^0.2.17",
"cross-env": "^5.2.1"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const ciInfo = jest.genMockFromModule(`ci-info`)
const ciInfo: typeof import("ci-info") = jest.genMockFromModule(`ci-info`)

ciInfo.isCI = false
ciInfo.name = `bad default`

function setIsCI(value) {
function setIsCI(value: boolean): void {
ciInfo.isCI = value
}

function setName(name) {
function setName(name: string): void {
ciInfo.name = name
}
ciInfo.setIsCI = setIsCI
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ci = require(`ci-info`)
import ci from "ci-info"

const CI_DEFINITIONS = [
envFromCIandCIName,
Expand All @@ -9,7 +9,7 @@ const CI_DEFINITIONS = [
envFromCIWithNoName,
]

function lookupCI() {
function lookupCI(): string | null {
for (const fn of CI_DEFINITIONS) {
try {
const res = fn()
Expand All @@ -24,45 +24,53 @@ function lookupCI() {
}
const CIName = lookupCI()

function isCI() {
export function isCI(): boolean {
return !!CIName
}

function getCIName() {
export function getCIName(): string | null {
if (!isCI()) {
return null
}
return CIName
}

module.exports = { isCI, getCIName }

function getEnvFromCIInfo() {
function getEnvFromCIInfo(): string | null {
if (ci.isCI) return ci.name || `ci-info detected w/o name`
return null
}

function getEnvDetect({ key, name }) {
return function() {
function getEnvDetect({
key,
name,
}: {
key: string
name: string
}): () => string | null {
return function(): string | null {
if (process.env[key]) {
return name
}
return null
}
}

function herokuDetect() {
return /\.heroku\/node\/bin\/node/.test(process.env.NODE) && `Heroku`
function herokuDetect(): false | "Heroku" {
return (
typeof process.env.NODE === `string` &&
/\.heroku\/node\/bin\/node/.test(process.env.NODE) &&
`Heroku`
)
}

function envFromCIandCIName() {
function envFromCIandCIName(): string | null {
if (process.env.CI_NAME && process.env.CI) {
return process.env.CI_NAME
}
return null
}

function envFromCIWithNoName() {
function envFromCIWithNoName(): `CI detected without name` | null {
if (process.env.CI) {
return `CI detected without name`
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3818,6 +3818,11 @@
resolved "https://registry.yarnpkg.com/@types/cache-manager/-/cache-manager-2.10.1.tgz#c7bc354be7988659e139e10fc7bde4b221f7f128"
integrity sha512-oJhVIOeC8dX9RZ7OtEZvZ/6cHF5aQWoRcuJ7KwK3Xb69hIIdElpWzfJ35fOXvYOgoyRXA34jFrD8lqEjDWz37w==

"@types/[email protected]":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/ci-info/-/ci-info-2.0.0.tgz#51848cc0f5c30c064f4b25f7f688bf35825b3971"
integrity sha512-5R2/MHILQLDCzTuhs1j4Qqq8AaKUf7Ma4KSSkCtc12+fMs47zfa34qhto9goxpyX00tQK1zxB885VCiawZ5Qhg==

"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
Expand Down

0 comments on commit 5dafd4f

Please sign in to comment.