Skip to content

Commit 68ddc51

Browse files
committed
darwin: return release version and os name
Return more details about macOS version, for example, for current Big Sur it looks like: {"os":"darwin","release":"11.4","name":"macOS"} Signed-off-by: Kyr Shatskyy <[email protected]>
1 parent c57a5c8 commit 68ddc51

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,36 @@ module.exports = function getOs (cb) {
1919
var osName = os.platform()
2020
// Linux is a special case.
2121
if (osName === 'linux') return getLinuxDistro(cb)
22+
if (osName === 'darwin') return getDarwinVersion(cb)
2223
// Else, node's builtin is acceptable.
2324
return cb(null, { os: osName })
2425
}
2526

27+
function getDarwinVersion (cb) {
28+
if (cachedDistro) return cb(null, cachedDistro)
29+
var productVersionRegex = /^ProductVersion:\s+(.*)/m
30+
var productNameRegex = /^ProductName:\s+(.*)/m
31+
var exec = require('child_process').exec
32+
33+
distro = { os: 'darwin' }
34+
35+
exec('sw_vers', function (error, stdout, stderr) {
36+
if (error) {
37+
return cb(error, { os: os.platform() } )
38+
}
39+
var productVersion = stdout.match(productVersionRegex)
40+
if (productVersion && productVersion.length === 2) {
41+
distro.release = productVersion[1]
42+
}
43+
var name = stdout.match(productNameRegex)
44+
if (name && name.length === 2) {
45+
distro.name = name[1]
46+
}
47+
cachedDistro = distro
48+
return cb(null, distro)
49+
})
50+
}
51+
2652
/**
2753
* Identify the actual distribution name on a linux box.
2854
*/

0 commit comments

Comments
 (0)