@netlify/content-engine
Version:
51 lines • 1.82 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPhysicalCpuCount = getPhysicalCpuCount;
// Forked from physical-cpu-count package from npm
const os_1 = __importDefault(require("os"));
const child_process_1 = __importDefault(require("child_process"));
function exec(command) {
const output = child_process_1.default.execSync(command, { encoding: `utf8` });
return output;
}
/*
* Fallback if child process fails to receive CPU count
*/
function fallbackToNodeJSCheck() {
const cores = os_1.default.cpus().filter(function (cpu, index) {
const hasHyperthreading = cpu.model.includes(`Intel`);
const isOdd = index % 2 === 1;
return !hasHyperthreading || isOdd;
});
return cores.length;
}
function getPhysicalCpuCount() {
const platform = os_1.default.platform();
try {
if (platform === `linux`) {
const output = exec(`lscpu -p | grep -E -v "^#" | sort -u -t, -k 2,4 | wc -l`);
return Number(output.trim());
}
if (platform === `darwin`) {
const output = exec(`sysctl -n hw.physicalcpu_max`);
return Number(output.trim());
}
if (platform === `win32`) {
const output = exec(`WMIC CPU Get NumberOfCores`);
return output
.replace(/\r/g, ``)
.split(`\n`)
.map((line) => Number(line))
.filter((value) => !isNaN(value))
.reduce((sum, number) => sum + number, 0);
}
}
catch (err) {
// carry on
}
return fallbackToNodeJSCheck();
}
//# sourceMappingURL=physical-cpu-count.js.map
;