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

chore(gatsby-core-utils): ci to TS #22047

Merged
merged 8 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 3 additions & 2 deletions 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 @@ -37,6 +37,7 @@
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"babel-preset-gatsby-package": "^0.2.17",
"cross-env": "^5.2.1"
"cross-env": "^5.2.1",
"@types/ci-info": "2.0.0"
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
}
}
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`)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this valid? typically import() are async. So this seems off. /shrug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're referring to Webpack's import() function that does async import. In TypeScript you can pull in the export signature this way.


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/ci-info@2.0.0":
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