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): port physical cpu count to ts #22031

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// 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() {
const cores = os.cpus().filter(function(cpu, index) {
function fallbackToNodeJSCheck(): number {
const cores = os.cpus().filter((cpu, index) => {
const hasHyperthreading = cpu.model.includes(`Intel`)
const isOdd = index % 2 === 1
return !hasHyperthreading || isOdd
Expand All @@ -22,7 +22,7 @@ function fallbackToNodeJSCheck() {

const platform = os.platform()

function getPhysicalCpuCount() {
function getPhysicalCpuCount(): number {
try {
if (platform === `linux`) {
const output = exec(
Expand Down Expand Up @@ -52,4 +52,4 @@ function getPhysicalCpuCount() {
return fallbackToNodeJSCheck()
}

module.exports = getPhysicalCpuCount()
export default getPhysicalCpuCount()
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use Guidelines (from #21995 description):

Only use named exports, no export default.

Suggested change
export default getPhysicalCpuCount()
export const physicalCpuCount = getPhysicalCpuCount()

and adjust all physical-cpu-count imports?

That should be in

let coreCount = require(`./physical-cpu-count`) || 1

and various tests