Skip to content

Instantly share code, notes, and snippets.

@BlackHole1
Last active November 24, 2018 09:04
Show Gist options
  • Save BlackHole1/7a2c9d7af460631db58bae2e223b4ca9 to your computer and use it in GitHub Desktop.
Save BlackHole1/7a2c9d7af460631db58bae2e223b4ca9 to your computer and use it in GitHub Desktop.
get cpu
const existsSync = require('fs').existsSync;
const execSync = require('child_process').execSync;
const isWin = process.platform === 'win32';
let winWmic = '';
const wmicFilePath = process.env.WINDIR + '\\system32\\wbem\\wmic.exe';
try {
const getWmic = (() => {
if (winWmic) return winWmic;
winWmic = existsSync(wmicFilePath) ? wmicFilePath : 'wmic';
return winWmic;
})()
const winCommand = `${getWmic} cpu get name`;
const macCommand = 'sysctl machdep.cpu';
const commandResult = execSync(isWin ? winCommand : macCommand).toString().split(isWin ? '\r\n' : '\n');
if (isWin) {
console.log(commandResult[1].trim())
return commandResult[1].trim();
} else {
for (let i = 0; i < commandResult.length; i++) {
const item = commandResult[i];
const [ key, value ] = item.split(':');
if (key === 'machdep.cpu.brand_string') {
console.log(value.trim())
return value.trim();
}
}
}
} catch (e) {
console.log(e);
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment