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