Skip to content

Commit cc5fb21

Browse files
blainekastendanielkov
andauthoredMar 10, 2020
chore(gatsby-core-utils): Convert package to TS (#22122)
* 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 <kovacsemod@gmail.com>
1 parent e031fbe commit cc5fb21

24 files changed

+149
-125
lines changed
 

‎.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ module.exports = {
8585
plugins: ["@typescript-eslint/eslint-plugin"],
8686
rules: {
8787
...TSEslint.configs.recommended.rules,
88+
// This rule tries to prevent using `require()`. However in node code,
89+
// there are times where this makes sense. And it specifically is causing
90+
// problems in our tests where we often want this functionality for module
91+
// mocking. At this point it's easier to have it off and just encouarge
92+
// using top-level imports via code reviews.
93+
"@typescript-eslint/no-var-requires": "off",
8894
// This rule ensures that typescript types do not have semicolons
8995
// at the end of their lines, since our prettier setup is to have no semicolons
9096
// e.g.,

‎packages/gatsby-core-utils/.babelrc

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"presets": [
3-
["babel-preset-gatsby-package"]
2+
"presets": [["babel-preset-gatsby-package"]],
3+
"overrides": [
4+
{
5+
"test": "**/*.ts",
6+
"plugins": [["@babel/plugin-transform-typescript", { "isTSX": true }]]
7+
}
48
]
59
}

‎packages/gatsby-core-utils/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"directory": "packages/gatsby-core-utils"
1717
},
1818
"scripts": {
19-
"build": "babel src --out-dir dist/ --ignore **/__tests__",
19+
"build": "babel src --out-dir dist/ --ignore **/__tests__ --ignore **/__mocks__ --extensions \".ts\"",
2020
"prepare": "cross-env NODE_ENV=production npm run build",
21-
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__"
21+
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__ --extensions \".ts\""
2222
},
2323
"bugs": {
2424
"url": "https://github.com/gatsbyjs/gatsby/issues"
@@ -36,6 +36,7 @@
3636
"devDependencies": {
3737
"@babel/cli": "^7.7.5",
3838
"@babel/core": "^7.7.5",
39+
"@types/ci-info": "2.0.0",
3940
"babel-preset-gatsby-package": "^0.2.17",
4041
"cross-env": "^5.2.1"
4142
}

‎packages/gatsby-core-utils/src/__mocks__/ci-info.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function setIsCI(value) {
1010
function setName(name) {
1111
ciInfo.name = name
1212
}
13+
1314
ciInfo.setIsCI = setIsCI
1415
ciInfo.setName = setName
1516
module.exports = ciInfo

‎packages/gatsby-core-utils/src/__tests__/ci.js ‎packages/gatsby-core-utils/src/__tests__/ci.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe(`CI detection`, () => {
3030
})
3131

3232
it(`Detects Now v2`, () => {
33-
process.env.NOW_BUILDER_ANNOTATE = 1
33+
process.env.NOW_BUILDER_ANNOTATE = `1`
3434
const { isCI, getCIName } = require(`../ci`)
3535

3636
expect(isCI()).toBeTruthy()
@@ -53,15 +53,15 @@ describe(`CI detection`, () => {
5353
expect(getCIName()).toEqual(`Heroku`)
5454
})
5555
it(`Detects CI and CI_NAME`, () => {
56-
process.env.CI = true
56+
process.env.CI = `true`
5757
process.env.CI_NAME = `test CI`
5858
const { isCI, getCIName } = require(`../ci`)
5959

6060
expect(isCI()).toBeTruthy()
6161
expect(getCIName()).toEqual(`test CI`)
6262
})
6363
it(`Detects CI and no CI_NAME`, () => {
64-
process.env.CI = true
64+
process.env.CI = `true`
6565
delete process.env.CI_NAME
6666
const { isCI, getCIName } = require(`../ci`)
6767

‎packages/gatsby-core-utils/src/__tests__/cpu-core-count.js

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
jest.mock(`../physical-cpu-count`, () => {
2+
return { getPhysicalCpuCount: (): number => 1 }
3+
})
4+
import { cpuCoreCount } from "../cpu-core-count"
5+
6+
beforeEach(() => {
7+
delete process.env.GATSBY_CPU_COUNT
8+
})
9+
10+
test(`it defaults to physical CPU count, if override not detected`, () => {
11+
expect(cpuCoreCount(false)).toBe(1)
12+
})
13+
14+
test(`it does not use env far override, if ignoreEnvVar is true`, () => {
15+
process.env.GATSBY_CPU_COUNT = `9001`
16+
17+
expect(cpuCoreCount(true)).not.toBe(Number(process.env.GATSBY_CPU_COUNT))
18+
})
19+
20+
test(`uses env var override, if exists`, () => {
21+
process.env.GATSBY_CPU_COUNT = `9001`
22+
23+
expect(cpuCoreCount(false)).toBe(Number(process.env.GATSBY_CPU_COUNT))
24+
})

‎packages/gatsby-core-utils/src/__tests__/create-content-digest.js ‎packages/gatsby-core-utils/src/__tests__/create-content-digest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const createContentDigest = require(`../create-content-digest`)
1+
import { createContentDigest } from "../create-content-digest"
22

33
describe(`Create content digest`, () => {
44
it(`returns the content digest when the input is a string`, () => {

‎packages/gatsby-core-utils/src/__tests__/path.js ‎packages/gatsby-core-utils/src/__tests__/path.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { joinPath, isNodeInternalModulePath, slash } = require(`../path`)
2-
const os = require(`os`)
1+
import { joinPath, isNodeInternalModulePath, slash } from "../path"
2+
import os from "os"
33

44
describe(`paths`, () => {
55
describe(`joinPath`, () => {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jest.mock(`child_process`)
22
jest.mock(`os`)
33
let os
4-
function mockPlatform(platform) {
4+
function mockPlatform(platform: string): void {
55
os.platform.mockImplementation(() => platform)
66
}
77

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

1515
it.each([`linux`, `darwin`])(
1616
`should return correct CPU count on %s`,
17-
platform => {
17+
(platform: string): void => {
1818
const cProc = require(`child_process`)
19-
cProc.execSync.mockImplementation(() => `4`)
19+
cProc.execSync.mockImplementation((): string => `4`)
2020
mockPlatform(platform)
21-
const pcc = require(`../physical-cpu-count`)
22-
expect(pcc).toBe(4)
21+
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
22+
expect(getPhysicalCpuCount()).toBe(4)
2323
}
2424
)
2525

2626
it.each([`linux`, `darwin`])(
2727
`should return fallback CPU count on %s when childProcess fails`,
28-
platform => {
28+
(platform: string): void => {
2929
const cProc = require(`child_process`)
3030
cProc.execSync.mockImplementation(() => `4`)
3131
mockPlatform(platform)
32-
const pcc = require(`../physical-cpu-count`)
33-
expect(pcc).toBe(4)
32+
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
33+
expect(getPhysicalCpuCount()).toBe(4)
3434
}
3535
)
3636

@@ -43,8 +43,8 @@ describe(`physical-cpu-count`, () => {
4343
`
4444
)
4545
mockPlatform(`win32`)
46-
const pcc = require(`../physical-cpu-count`)
47-
expect(pcc).toBe(4)
46+
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
47+
expect(getPhysicalCpuCount()).toBe(4)
4848
})
4949

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

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

6666
os.cpus.mockImplementation(() => [{ model: `Intel` }, { model: `Intel` }])
6767
mockPlatform(`linux`)
68-
const pcc = require(`../physical-cpu-count`)
69-
expect(pcc).toBe(1)
68+
const { getPhysicalCpuCount } = require(`../physical-cpu-count`)
69+
expect(getPhysicalCpuCount()).toBe(1)
7070
})
7171
})

‎packages/gatsby-core-utils/src/__tests__/url.js ‎packages/gatsby-core-utils/src/__tests__/url.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const { resolve } = require(`../url`)
1+
import { urlResolve } from "../url"
22

33
describe(`url`, () => {
4-
describe(`resolve`, () => {
4+
describe(`urlResolve`, () => {
55
it(`resolves segments into valid url pathname`, () => {
66
const paths = [`/`, ``, `./foo`, `bar`, `baz`]
7-
const actual = resolve(...paths)
7+
const actual = urlResolve(...paths)
88
expect(actual).toBe(`/foo/bar/baz`)
99
})
1010
})

‎packages/gatsby-core-utils/src/ci.js ‎packages/gatsby-core-utils/src/ci.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ci = require(`ci-info`)
1+
import ci from "ci-info"
22

33
const CI_DEFINITIONS = [
44
envFromCIandCIName,
@@ -9,7 +9,7 @@ const CI_DEFINITIONS = [
99
envFromCIWithNoName,
1010
]
1111

12-
function lookupCI() {
12+
function lookupCI(): string | null {
1313
for (const fn of CI_DEFINITIONS) {
1414
try {
1515
const res = fn()
@@ -24,45 +24,53 @@ function lookupCI() {
2424
}
2525
const CIName = lookupCI()
2626

27-
function isCI() {
27+
export function isCI(): boolean {
2828
return !!CIName
2929
}
3030

31-
function getCIName() {
31+
export function getCIName(): string | null {
3232
if (!isCI()) {
3333
return null
3434
}
3535
return CIName
3636
}
3737

38-
module.exports = { isCI, getCIName }
39-
40-
function getEnvFromCIInfo() {
38+
function getEnvFromCIInfo(): string | null {
4139
if (ci.isCI) return ci.name || `ci-info detected w/o name`
4240
return null
4341
}
4442

45-
function getEnvDetect({ key, name }) {
46-
return function() {
43+
function getEnvDetect({
44+
key,
45+
name,
46+
}: {
47+
key: string
48+
name: string
49+
}): () => string | null {
50+
return function(): string | null {
4751
if (process.env[key]) {
4852
return name
4953
}
5054
return null
5155
}
5256
}
5357

54-
function herokuDetect() {
55-
return /\.heroku\/node\/bin\/node/.test(process.env.NODE) && `Heroku`
58+
function herokuDetect(): false | "Heroku" {
59+
return (
60+
typeof process.env.NODE === `string` &&
61+
/\.heroku\/node\/bin\/node/.test(process.env.NODE) &&
62+
`Heroku`
63+
)
5664
}
5765

58-
function envFromCIandCIName() {
66+
function envFromCIandCIName(): string | null {
5967
if (process.env.CI_NAME && process.env.CI) {
6068
return process.env.CI_NAME
6169
}
6270
return null
6371
}
6472

65-
function envFromCIWithNoName() {
73+
function envFromCIWithNoName(): "CI detected without name" | null {
6674
if (process.env.CI) {
6775
return `CI detected without name`
6876
}

‎packages/gatsby-core-utils/src/cpu-core-count.js ‎packages/gatsby-core-utils/src/cpu-core-count.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/**
2-
* @type {import('../index').cpuCoreCount}
3-
*/
4-
const cpuCoreCount = ignoreEnvVar => {
1+
import { getPhysicalCpuCount } from "./physical-cpu-count"
2+
3+
export const cpuCoreCount = (ignoreEnvVar: boolean): number => {
54
try {
6-
let coreCount = require(`./physical-cpu-count`) || 1
5+
let coreCount = getPhysicalCpuCount() || 1
76

87
if (ignoreEnvVar) {
98
// Return the physical CPU count,
@@ -47,5 +46,3 @@ const cpuCoreCount = ignoreEnvVar => {
4746
throw new Error(`There has been a problem counting the number of CPU cores`)
4847
}
4948
}
50-
51-
module.exports = cpuCoreCount
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const crypto = require(`crypto`)
2-
const objectHash = require(`node-object-hash`)
1+
import crypto, { BinaryLike } from "crypto"
2+
import objectHash from "node-object-hash"
33

44
const hasher = objectHash({
55
coerce: false,
@@ -13,21 +13,18 @@ const hasher = objectHash({
1313
},
1414
})
1515

16-
const hashPrimitive = input =>
16+
const hashPrimitive = (input: BinaryLike | string): string =>
1717
crypto
1818
.createHash(`md5`)
1919
.update(input)
2020
.digest(`hex`)
2121

22-
/**
23-
* @type {import('../index').createContentDigest}
24-
*/
25-
const createContentDigest = input => {
22+
export const createContentDigest = (
23+
input: BinaryLike | string | any
24+
): string => {
2625
if (typeof input === `object`) {
2726
return hasher.hash(input)
2827
}
2928

3029
return hashPrimitive(input)
3130
}
32-
33-
module.exports = createContentDigest

‎packages/gatsby-core-utils/src/create-require-from-path.js

-16
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.