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): migrate to TypeScript #22040

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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"
}
}
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 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,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() {
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 physicalCpuCount from "./physical-cpu-count"

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

if (ignoreEnvVar) {
// Return the physical CPU count,
Expand Down Expand Up @@ -48,4 +47,4 @@ const cpuCoreCount = ignoreEnvVar => {
}
}

module.exports = cpuCoreCount
export default 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,20 @@ 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 => {
const createContentDigest = (
input: BinaryLike | string | Parameters<typeof hasher.hash>[0]
): string => {
if (typeof input === `object`) {
return hasher.hash(input)
}

return hashPrimitive(input)
}

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

This file was deleted.

22 changes: 22 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,22 @@
import Module from "module"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"allowSyntheticDefaultImports": true is needed for this to work with import * as Module from "module"

import path from "path"

// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
const createRequire = (filename: string): NodeRequire => {
if (`createRequire` in Module) {
return Module.createRequire(filename)
}
if (`createRequireFromPath` in Module) {
return Module.createRequireFromPath(filename)
}

const mod = new Module(filename, null)

mod.filename = filename
mod.paths = Module._nodeModulePaths(path.dirname(filename))
mod._compile(`module.exports = require;`, filename)

return mod.exports
}

export default createRequire
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 = () => {
const getConfigStore = (): Configstore => {
if (!config) {
config = new Configstore(
`gatsby`,
Expand All @@ -15,3 +15,5 @@ module.exports = () => {

return config
}

export default getConfigStore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path"

module.exports = () => {
const getGatsbyVersion = (): string => {
try {
return require(path.join(
process.cwd(),
Expand All @@ -12,3 +12,5 @@ module.exports = () => {
return ``
}
}

export default getGatsbyVersion
11 changes: 0 additions & 11 deletions packages/gatsby-core-utils/src/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/gatsby-core-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { default as createContentDigest } from "./create-content-digest"
export { joinPath, isNodeInternalModulePath, slash } from "./path"
export { default as cpuCoreCount } from "./cpu-core-count"
export { resolve as urlResolve } from "./url"
export { isCI, getCIName } from "./ci"
export { default as createRequireFromPath } from "./create-require-from-path"
export { default as getConfigStore } from "./get-config-store"
export { default as getGatsbyVersion } from "./get-gatsby-version"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const path = require(`path`)
const os = require(`os`)
import path from "path"
import os from "os"

/**
* @type {import('../index').joinPath}
*/
export function joinPath(...paths) {
export function joinPath(...paths: string[]): string {
const joinedPath = path.join(...paths)
if (os.platform() === `win32`) {
return joinedPath.replace(/\\/g, `\\\\`)
Expand Down Expand Up @@ -72,7 +72,7 @@ const nodePaths = [
/**
* @type {import('../index').isNodeInternalModulePath}
*/
export const isNodeInternalModulePath = fileName =>
export const isNodeInternalModulePath = (fileName: string): boolean =>
nodePaths.some(regTest => regTest.test(fileName))

/**
Expand All @@ -84,7 +84,7 @@ export const isNodeInternalModulePath = fileName =>
* @param {String} path
* @return {String} slashed path
*/
export function slash(path) {
export function slash(path: string): string {
const isExtendedLengthPath = /^\\\\\?\\/.test(path)

if (isExtendedLengthPath) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Forked from physical-cpu-count package from npm
const os = require(`os`)
const childProcess = require(`child_process`)
import os from "os"
import childProcess from "child_process"

function exec(command) {
function exec(command: string): string {
const output = childProcess.execSync(command, { encoding: `utf8` })
return output
}

/*
* Fallback if child process fails to receive CPU count
*/
function fallbackToNodeJSCheck() {
function fallbackToNodeJSCheck(): number {
const cores = os.cpus().filter(function(cpu, index) {
const hasHyperthreading = cpu.model.includes(`Intel`)
const isOdd = index % 2 === 1
Expand All @@ -22,7 +22,7 @@ function fallbackToNodeJSCheck() {

const platform = os.platform()

function getPhysicalCpuCount() {
export default function getPhysicalCpuCount(): number {
try {
if (platform === `linux`) {
const output = exec(
Expand All @@ -48,8 +48,5 @@ function getPhysicalCpuCount() {
} catch (err) {
// carry on
}

return fallbackToNodeJSCheck()
}

module.exports = getPhysicalCpuCount()
Empty file.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const path = require(`path`)
const os = require(`os`)
import path from "path"
import os from "os"

/**
* @type {import('../index').urlResolve}
*/
export function resolve(...segments) {
export function resolve(...segments: string[]): string {
const joinedPath = path.join(...segments)
if (os.platform() === `win32`) {
return joinedPath.replace(/\\/g, `/`)
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