Skip to content

Commit

Permalink
chore(gatsby-core-utils): Convert package to TS (#22122)
Browse files Browse the repository at this point in the history
* chore(gatsby-core-utils): get-config-store to TS (#22051)

* chore(gatsby-core-utils): get-config-store to TS

* chore(gatsby-core-utils): change getConfigStore to named export

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

* 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

* chore(gatsby-core-utils): cpu-core-count to TS (#22048)

* chore(gatsby-core-utils): create-content-digest to TS (#22049)

* chore(gatsby-core-utils): tests create-content-digest to TS

* chore(gatsby-core-utils): create-content-digest to TS

* chore(gatsby-core-utils): create-require-from-path to TS (#22050)

* chore(gatsby-core-utils): get-gatsby-version to TS (#22052)

* chore(gatsby-core-utils): index to TS (#22053)

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

* chore(gatby-core-utils): changed getConfigStore to named export in #22051

* chore(gatsby-core-utils): path to ts (#22054)

* chore(gatsby-core-utils): tests path to TS

* chore(gatsby-core-utils): path to ts

* chore(gatsby-core-utils): url to TS (#22056)

* chore(gatsby-core-utils): tests url to TS

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

* migrate more files

* fix the rest

* better exports

* fix lints

* fix building

* fix test

Co-authored-by: Daniel Emod Kovacs <[email protected]>
  • Loading branch information
blainekasten and danielkov authored Mar 10, 2020
1 parent e031fbe commit cc5fb21
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 125 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ module.exports = {
plugins: ["@typescript-eslint/eslint-plugin"],
rules: {
...TSEslint.configs.recommended.rules,
// This rule tries to prevent using `require()`. However in node code,
// there are times where this makes sense. And it specifically is causing
// problems in our tests where we often want this functionality for module
// mocking. At this point it's easier to have it off and just encouarge
// using top-level imports via code reviews.
"@typescript-eslint/no-var-requires": "off",
// This rule ensures that typescript types do not have semicolons
// at the end of their lines, since our prettier setup is to have no semicolons
// e.g.,
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-core-utils/.babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"presets": [
["babel-preset-gatsby-package"]
"presets": [["babel-preset-gatsby-package"]],
"overrides": [
{
"test": "**/*.ts",
"plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]]
}
]
}
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\""
},
"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
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/src/__mocks__/ci-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function setIsCI(value) {
function setName(name) {
ciInfo.name = name
}

ciInfo.setIsCI = setIsCI
ciInfo.setName = setName
module.exports = 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
22 changes: 0 additions & 22 deletions packages/gatsby-core-utils/src/__tests__/cpu-core-count.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/cpu-core-count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
jest.mock(`../physical-cpu-count`, () => {
return { getPhysicalCpuCount: (): number => 1 }
})
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(cpuCoreCount(false)).toBe(1)
})

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

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

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

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
@@ -1,7 +1,7 @@
jest.mock(`child_process`)
jest.mock(`os`)
let os
function mockPlatform(platform) {
function mockPlatform(platform: string): void {
os.platform.mockImplementation(() => platform)
}

Expand All @@ -14,23 +14,23 @@ describe(`physical-cpu-count`, () => {

it.each([`linux`, `darwin`])(
`should return correct CPU count on %s`,
platform => {
(platform: string): void => {
const cProc = require(`child_process`)
cProc.execSync.mockImplementation(() => `4`)
cProc.execSync.mockImplementation((): string => `4`)
mockPlatform(platform)
const pcc = require(`../physical-cpu-count`)
expect(pcc).toBe(4)
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
expect(getPhysicalCpuCount()).toBe(4)
}
)

it.each([`linux`, `darwin`])(
`should return fallback CPU count on %s when childProcess fails`,
platform => {
(platform: string): void => {
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,10 +1,10 @@
const { resolve } = require(`../url`)
import { urlResolve } from "../url"

describe(`url`, () => {
describe(`resolve`, () => {
describe(`urlResolve`, () => {
it(`resolves segments into valid url pathname`, () => {
const paths = [`/`, ``, `./foo`, `bar`, `baz`]
const actual = resolve(...paths)
const actual = urlResolve(...paths)
expect(actual).toBe(`/foo/bar/baz`)
})
})
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* @type {import('../index').cpuCoreCount}
*/
const cpuCoreCount = ignoreEnvVar => {
import { getPhysicalCpuCount } from "./physical-cpu-count"

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 +46,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.

Loading

0 comments on commit cc5fb21

Please sign in to comment.