Skip to content

Commit

Permalink
darwin: return release version and os name
Browse files Browse the repository at this point in the history
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 <kyrylo.shatskyy@gmail.com>
  • Loading branch information
kshtsk committed Jul 6, 2021
1 parent c57a5c8 commit 68ddc51
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,36 @@ module.exports = function getOs (cb) {
var osName = os.platform()
// Linux is a special case.
if (osName === 'linux') return getLinuxDistro(cb)
if (osName === 'darwin') return getDarwinVersion(cb)
// Else, node's builtin is acceptable.
return cb(null, { os: osName })
}

function getDarwinVersion (cb) {
if (cachedDistro) return cb(null, cachedDistro)
var productVersionRegex = /^ProductVersion:\s+(.*)/m
var productNameRegex = /^ProductName:\s+(.*)/m
var exec = require('child_process').exec

distro = { os: 'darwin' }

exec('sw_vers', function (error, stdout, stderr) {
if (error) {
return cb(error, { os: os.platform() } )
}
var productVersion = stdout.match(productVersionRegex)
if (productVersion && productVersion.length === 2) {
distro.release = productVersion[1]
}
var name = stdout.match(productNameRegex)
if (name && name.length === 2) {
distro.name = name[1]
}
cachedDistro = distro
return cb(null, distro)
})
}

/**
* Identify the actual distribution name on a linux box.
*/
Expand Down

0 comments on commit 68ddc51

Please sign in to comment.