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

Ts/gatsby core utils #22122

Merged
merged 15 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 10 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,9 +16,9 @@
"directory": "packages/gatsby-core-utils"
},
"scripts": {
"build": "babel src --out-dir dist/ --ignore **/__tests__",
"build": "babel src --out-dir dist/ --ignore **/__tests__ --ignore **/__mocks__ --extensions \".ts\"",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__"
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__ --extensions \".ts,.js\""
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
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
15 changes: 0 additions & 15 deletions packages/gatsby-core-utils/src/__mocks__/ci-info.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/gatsby-core-utils/src/__mocks__/ci-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ci from "ci-info"
const ciInfo: typeof ci = jest.genMockFromModule(`ci-info`)

// @ts-ignore
pieh marked this conversation as resolved.
Show resolved Hide resolved
ciInfo.isCI = false
// @ts-ignore
ciInfo.name = `bad default`

function setIsCI(value: boolean): void {
// @ts-ignore
ciInfo.isCI = value
}

function setName(name: string): void {
// @ts-ignore
ciInfo.name = name
}

// @ts-ignore
ciInfo.setIsCI = setIsCI
// @ts-ignore
ciInfo.setName = setName

export default ciInfo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe(`CI detection`, () => {
})

it(`Detects Now v2`, () => {
process.env.NOW_BUILDER_ANNOTATE = 1
process.env.NOW_BUILDER_ANNOTATE = `1`
const { isCI, getCIName } = require(`../ci`)

expect(isCI()).toBeTruthy()
Expand All @@ -53,15 +53,15 @@ describe(`CI detection`, () => {
expect(getCIName()).toEqual(`Heroku`)
})
it(`Detects CI and CI_NAME`, () => {
process.env.CI = true
process.env.CI = `true`
process.env.CI_NAME = `test CI`
const { isCI, getCIName } = require(`../ci`)

expect(isCI()).toBeTruthy()
expect(getCIName()).toEqual(`test CI`)
})
it(`Detects CI and no CI_NAME`, () => {
process.env.CI = true
process.env.CI = `true`
delete process.env.CI_NAME
const { isCI, getCIName } = require(`../ci`)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
jest.mock(`../physical-cpu-count`, () => 1)
const getCPUCoreCount = require(`../cpu-core-count`)
import { cpuCoreCount } from "../cpu-core-count"

beforeEach(() => {
delete process.env.GATSBY_CPU_COUNT
})

test(`it defaults to physical CPU count, if override not detected`, () => {
expect(getCPUCoreCount()).toBe(1)
expect(cpuCoreCount(false)).toBe(1)
})

test(`it does not use env far override, if ignoreEnvVar is true`, () => {
process.env.GATSBY_CPU_COUNT = 9001
process.env.GATSBY_CPU_COUNT = `9001`

expect(getCPUCoreCount(true)).not.toBe(Number(process.env.GATSBY_CPU_COUNT))
expect(cpuCoreCount(true)).not.toBe(Number(process.env.GATSBY_CPU_COUNT))
})

test(`uses env var override, if exists`, () => {
process.env.GATSBY_CPU_COUNT = 9001
process.env.GATSBY_CPU_COUNT = `9001`

expect(getCPUCoreCount()).toBe(Number(process.env.GATSBY_CPU_COUNT))
expect(cpuCoreCount(false)).toBe(Number(process.env.GATSBY_CPU_COUNT))
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createContentDigest = require(`../create-content-digest`)
import { createContentDigest } from "../create-content-digest"

describe(`Create content digest`, () => {
it(`returns the content digest when the input is a string`, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { joinPath, isNodeInternalModulePath, slash } = require(`../path`)
const os = require(`os`)
import { joinPath, isNodeInternalModulePath, slash } from "../path"
import os from "os"

describe(`paths`, () => {
describe(`joinPath`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe(`physical-cpu-count`, () => {
const cProc = require(`child_process`)
cProc.execSync.mockImplementation(() => `4`)
mockPlatform(platform)
const pcc = require(`../physical-cpu-count`)
expect(pcc).toBe(4)
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
expect(getPhysicalCpuCount()).toBe(4)
}
)

Expand All @@ -29,8 +29,8 @@ describe(`physical-cpu-count`, () => {
const cProc = require(`child_process`)
cProc.execSync.mockImplementation(() => `4`)
mockPlatform(platform)
const pcc = require(`../physical-cpu-count`)
expect(pcc).toBe(4)
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
expect(getPhysicalCpuCount()).toBe(4)
}
)

Expand All @@ -43,8 +43,8 @@ describe(`physical-cpu-count`, () => {
`
)
mockPlatform(`win32`)
const pcc = require(`../physical-cpu-count`)
expect(pcc).toBe(4)
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
expect(getPhysicalCpuCount()).toBe(4)
})

it(`should return fallback CPU count on Windows when childProcess fails`, () => {
Expand All @@ -53,8 +53,8 @@ describe(`physical-cpu-count`, () => {
throw new Error(`Fail!`)
})
mockPlatform(`win32`)
const pcc = require(`../physical-cpu-count`)
expect(pcc).toBe(1)
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
expect(getPhysicalCpuCount()).toBe(1)
})

it(`should check for hyperthreading when intel is the processor`, () => {
Expand All @@ -65,7 +65,7 @@ describe(`physical-cpu-count`, () => {

os.cpus.mockImplementation(() => [{ model: `Intel` }, { model: `Intel` }])
mockPlatform(`linux`)
const pcc = require(`../physical-cpu-count`)
expect(pcc).toBe(1)
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
expect(getPhysicalCpuCount()).toBe(1)
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { resolve } = require(`../url`)
import { resolve } from "../url"

describe(`url`, () => {
describe(`resolve`, () => {
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() {
interface IGetEnvDetect {
// @prettier-ignore
key: string
name: string
}

function getEnvDetect({ key, name }: IGetEnvDetect): () => 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
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @type {import('../index').cpuCoreCount}
*/
const cpuCoreCount = ignoreEnvVar => {
import getPhysicalCpuCount from "./physical-cpu-count"
console.log(getPhysicalCpuCount)
pieh marked this conversation as resolved.
Show resolved Hide resolved

export const cpuCoreCount = (ignoreEnvVar: boolean): number => {
try {
let coreCount = require(`./physical-cpu-count`) || 1
let coreCount = getPhysicalCpuCount() || 1

if (ignoreEnvVar) {
// Return the physical CPU count,
Expand Down Expand Up @@ -47,5 +47,3 @@ const cpuCoreCount = ignoreEnvVar => {
throw new Error(`There has been a problem counting the number of CPU cores`)
}
}

module.exports = cpuCoreCount
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const crypto = require(`crypto`)
const objectHash = require(`node-object-hash`)
import crypto, { BinaryLike } from "crypto"
import objectHash from "node-object-hash"

const hasher = objectHash({
coerce: false,
Expand All @@ -13,21 +13,18 @@ const hasher = objectHash({
},
})

const hashPrimitive = input =>
const hashPrimitive = (input: BinaryLike | string): string =>
crypto
.createHash(`md5`)
.update(input)
.digest(`hex`)

/**
* @type {import('../index').createContentDigest}
*/
const createContentDigest = input => {
export const createContentDigest = (
input: BinaryLike | string | any
): string => {
if (typeof input === `object`) {
return hasher.hash(input)
}

return hashPrimitive(input)
}

module.exports = createContentDigest
16 changes: 0 additions & 16 deletions packages/gatsby-core-utils/src/create-require-from-path.js

This file was deleted.

18 changes: 18 additions & 0 deletions packages/gatsby-core-utils/src/create-require-from-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Module from "module"
import path from "path"

const fallback = (filename: string): NodeRequire => {
// @ts-ignore
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
const mod = new Module(filename, null)

mod.filename = filename
// @ts-ignore
mod.paths = Module._nodeModulePaths(path.dirname(filename))
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
mod._compile(`module.exports = require;`, filename)

return mod.exports
}

// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
export const createRequireFromPath =
Module.createRequire || Module.createRequireFromPath || fallback
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Configstore from "configstore"

let config
let config: Configstore

module.exports = () => {
export const getConfigStore = (): Configstore => {
if (!config) {
config = new Configstore(
`gatsby`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path"

module.exports = () => {
export const getGatsbyVersion = (): string => {
try {
return require(path.join(
process.cwd(),
Expand Down
11 changes: 0 additions & 11 deletions packages/gatsby-core-utils/src/index.js

This file was deleted.

Loading