Last active
October 23, 2023 04:10
-
-
Save cvan/ef28f73b88b991d4a9705314b2af2e78 to your computer and use it in GitHub Desktop.
[Node.js] get Operating-System Platform (Windows, macOS, Linux) and Architecture (x32, x64, etc.) (without any third-party npm dependencies)
This file contains hidden or 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 os = require('os'); | |
// See docs: https://nodejs.org/api/os.html | |
const usage = {}; | |
usage[`require('os').type()`] = { | |
value: os.type(), | |
otherValues: [ | |
'Linux', 'Darwin', 'Windows_NT' | |
], | |
docs: 'https://nodejs.org/api/os.html#os_os_type' | |
}; | |
usage[`require('os').release()`] = { | |
value: os.release(), | |
otherExampleValues: 'https://en.wikipedia.org/wiki/Uname#Examples', | |
docs: 'https://nodejs.org/api/os.html#os_os_release' | |
}; | |
usage[`require('os').arch()`] = { | |
value: os.arch(), | |
otherValues: [ | |
'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', 'x64' | |
], | |
docs: 'https://nodejs.org/api/os.html#os_os_arch' | |
}; | |
usage[`require('os').platform()`] = { | |
value: os.platform(), | |
otherValues: [ | |
'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32' | |
], | |
docs: 'https://nodejs.org/api/os.html#os_os_platform' | |
}; | |
// For Windows: can also check /^(msys|cygwin)$/.test(process.env.OSTYPE) | |
usage['process.arch'] = { | |
value: process.arch, | |
otherValues: [ | |
'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', 'x64' | |
], | |
docs: 'https://nodejs.org/api/process.html#process_process_arch' | |
}; | |
usage['process.platform'] = { | |
value: process.platform, | |
otherValues: [ | |
'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32' | |
], | |
docs: 'https://nodejs.org/api/process.html#process_process_platform' | |
}; | |
usage['process.platform'] = { | |
value: process.platform, | |
otherValues: [ | |
'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32' | |
], | |
docs: 'https://nodejs.org/api/process.html#process_process_platform' | |
}; | |
console.log(); | |
Object.keys(usage).map((key, idx) => { | |
const data = usage[key]; | |
console.log(`${key}`); | |
console.log(` Value:\n '${data.value}'`); | |
if (data.otherExampleValues && data.otherExampleValues.length) { | |
if (!Array.isArray(data.otherExampleValues)) { | |
data.otherExampleValues = [data.otherExampleValues, 'google.com']; | |
} | |
console.log(` Other example values:\n ${data.otherExampleValues.map(value => `${value}`).join('\n ')}`); | |
} | |
if (data.otherValues && data.otherValues.length) { | |
console.log(` Other possible values:\n ${data.otherValues.map(value => ` '${value}'`).toString().trim()}`); | |
} | |
console.log(` Docs:\n ${data.docs}\n`); | |
}); | |
console.log(); |
This file contains hidden or 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
// Source: https://gist.github.com/cvan/ef28f73b88b991d4a9705314b2af2e78#file-system-js | |
// Author: CVAN <https://cvan.io> | |
// Useful helper snippet for understanding the capabilities of a user's system from Node.js' built-in `process.arch` and `process.platform`. | |
const system = { | |
_arch: process.arch, | |
_platform: process.platform | |
}; | |
system.win = system._platform === 'win32' || process.env.OSTYPE === 'cygwin' || process.env.OSTYPE === 'msys'; | |
system.mac = system._platform === 'darwin'; | |
system.linux = !system.win && !system.mac; | |
system.arch32 = system._arch === 'arm' || system._arch === 'ia32' || system._arch === 'mips' || system._arch === 'ppc' || system._arch === 's390' || system._arch === 'x32'; | |
system.arch64 = system._arch === 'arm64' || system._arch === 'mipsel' || system._arch === 'ppc64' || system._arch === '390x' || system._arch === 'x64'; | |
system.intel = system._arch === 'ia32' || system._arch === 'x32' || system._arch === 'x64'; | |
system.arm = system._arch === 'arm' || system._arch === 'arm64'; | |
if (!module.parent) { | |
console.log(system); | |
} | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = system; | |
} else if (typeof window !== 'undefined') { | |
window.system = system; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment