Last active
November 24, 2018 09:04
-
-
Save BlackHole1/7a2c9d7af460631db58bae2e223b4ca9 to your computer and use it in GitHub Desktop.
get cpu
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
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