Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

var 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
2 changes: 1 addition & 1 deletion tests/mockdata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{ "desc": "OS X", "platform": "darwin", "expected": { "os": "darwin" } }
{ "desc": "OS X", "platform": "darwin", "exec": { "sw_vers": "ProductName:\tmacOS\nProductVersion:\t11.4\nBuildVersion:\t20F71\n"}, "expected": { "os": "darwin", "release": "11.4", "name": "macOS" } }
, { "desc": "Windows", "platform": "win32", "expected": { "os": "win32" } }
, { "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" } }
, { "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" } }
Expand Down
8 changes: 8 additions & 0 deletions tests/mocktests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var test = require('tape')
var fs = require('fs')
var os = require('os')
var cp = require('child_process')
var mockdata = require('./mockdata')

var currentData
Expand All @@ -23,6 +24,13 @@ fs.readFile = function (file, enc, callback) {
})
}

cp.exec = function (command, callback) {
process.nextTick(function () {
if (!currentData.exec[command]) { return callback(new Error()) }
callback(null, currentData.exec[command])
})
}

mockdata.forEach(function (data) {
test('test ' + data.desc, function (t) {
// reload each time to avoid internal caching
Expand Down