-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e384b10
commit 85887e1
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// https://www.npmjs.com/package/physical-cpu-count in ESM | ||
|
||
import os from 'os' | ||
import childProcess from 'child_process' | ||
|
||
function exec(command: string) { | ||
const output = childProcess.execSync(command, { encoding: 'utf8' }) | ||
return output | ||
} | ||
|
||
let amount: number | ||
|
||
try { | ||
const platform = os.platform() | ||
|
||
if (platform === 'linux') { | ||
const output = exec('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l') | ||
amount = parseInt(output.trim(), 10) | ||
} else if (platform === 'darwin') { | ||
const output = exec('sysctl -n hw.physicalcpu_max') | ||
amount = parseInt(output.trim(), 10) | ||
// @ts-ignore | ||
} else if (platform === 'windows' || platform === 'win32') { | ||
const output = exec('WMIC CPU Get NumberOfCores') | ||
amount = output | ||
.split(os.EOL) | ||
.map(function parse(line) { | ||
return parseInt(line) | ||
}) | ||
.filter(function numbers(value) { | ||
return !isNaN(value) | ||
}) | ||
.reduce(function add(sum, number) { | ||
return sum + number | ||
}, 0) | ||
} else { | ||
const cores = os.cpus().filter(function (cpu, index) { | ||
const hasHyperthreading = cpu.model.includes('Intel') | ||
const isOdd = index % 2 === 1 | ||
return !hasHyperthreading || isOdd | ||
}) | ||
amount = cores.length | ||
} | ||
} catch { | ||
amount = os.cpus().length | ||
} | ||
|
||
export default amount |