index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var cp = require('child_process')
  2. var fs = require('fs')
  3. var path = require('path')
  4. /**
  5. * Returns the operating system's CPU architecture. This is different than
  6. * `process.arch` or `os.arch()` which returns the architecture the Node.js (or
  7. * Electron) binary was compiled for.
  8. */
  9. module.exports = function arch () {
  10. /**
  11. * The running binary is 64-bit, so the OS is clearly 64-bit.
  12. */
  13. if (process.arch === 'x64') {
  14. return 'x64'
  15. }
  16. /**
  17. * All recent versions of Mac OS are 64-bit.
  18. */
  19. if (process.platform === 'darwin') {
  20. return 'x64'
  21. }
  22. /**
  23. * On Windows, the most reliable way to detect a 64-bit OS from within a 32-bit
  24. * app is based on the presence of a WOW64 file: %SystemRoot%\SysNative.
  25. * See: https://twitter.com/feross/status/776949077208510464
  26. */
  27. if (process.platform === 'win32') {
  28. var useEnv = false
  29. try {
  30. useEnv = !!(process.env.SYSTEMROOT && fs.statSync(process.env.SYSTEMROOT))
  31. } catch (err) {}
  32. var sysRoot = useEnv ? process.env.SYSTEMROOT : 'C:\\Windows'
  33. // If %SystemRoot%\SysNative exists, we are in a WOW64 FS Redirected application.
  34. var isWOW64 = false
  35. try {
  36. isWOW64 = !!fs.statSync(path.join(sysRoot, 'sysnative'))
  37. } catch (err) {}
  38. return isWOW64 ? 'x64' : 'x86'
  39. }
  40. /**
  41. * On Linux, use the `getconf` command to get the architecture.
  42. */
  43. if (process.platform === 'linux') {
  44. var output = cp.execSync('getconf LONG_BIT', {encoding: 'utf8'})
  45. return output === '64\n' ? 'x64' : 'x86'
  46. }
  47. /**
  48. * If none of the above, assume the architecture is 32-bit.
  49. */
  50. return 'x86'
  51. }