Skip to content

Commit d38eb7c

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 d38eb7c

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
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+
var 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
*/

tests/mockdata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
{ "desc": "OS X", "platform": "darwin", "expected": { "os": "darwin" } }
2+
{ "desc": "OS X", "platform": "darwin", "exec": { "sw_vers": "ProductName:\tmacOS\nProductVersion:\t11.4\nBuildVersion:\t20F71\n"}, "expected": { "os": "darwin", "release": "11.4", "name": "macOS" } }
33
, { "desc": "Windows", "platform": "win32", "expected": { "os": "win32" } }
44
, { "desc": "Ubuntu 14.10", "platform": "linux", "file": { "/etc/lsb-release": "DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=14.10\nDISTRIB_CODENAME=utopic\nDISTRIB_DESCRIPTION=\"Ubuntu 14.10\"\n" }, "expected": { "codename": "utopic", "dist": "Ubuntu", "os": "linux", "release": "14.10" } }
55
, { "desc": "Ubuntu 14.04", "platform": "linux", "file": { "/etc/lsb-release": "DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=14.04\nDISTRIB_CODENAME=trusty\nDISTRIB_DESCRIPTION=\"Ubuntu 14.04.2 LTS\"\n" }, "expected": { "codename": "trusty", "dist": "Ubuntu", "os": "linux", "release": "14.04" } }

tests/mocktests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var test = require('tape')
22
var fs = require('fs')
33
var os = require('os')
4+
var cp = require('child_process')
45
var mockdata = require('./mockdata')
56

67
var currentData
@@ -23,6 +24,13 @@ fs.readFile = function (file, enc, callback) {
2324
})
2425
}
2526

27+
cp.exec = function (command, callback) {
28+
process.nextTick(function () {
29+
if (!currentData.exec[command]) { return callback(new Error()) }
30+
callback(null, currentData.exec[command])
31+
})
32+
}
33+
2634
mockdata.forEach(function (data) {
2735
test('test ' + data.desc, function (t) {
2836
// reload each time to avoid internal caching

0 commit comments

Comments
 (0)