macos-version
Advanced tools
Comparing version 3.0.0 to 4.0.0
59
index.js
'use strict'; | ||
const execa = require('execa'); | ||
const fs = require('fs'); | ||
const semver = require('semver'); | ||
module.exports = () => execa.stdout('sw_vers', ['-productVersion']); | ||
const isMacos = process.platform === 'darwin'; | ||
let version; | ||
const getVersion = () => { | ||
if (!isMacos) { | ||
throw new Error('Requires macOS'); | ||
} | ||
if (!version) { | ||
const file = fs.readFileSync('/System/Library/CoreServices/SystemVersion.plist', 'utf8'); | ||
const matches = /<key>ProductVersion<\/key>[\s\S]*<string>([\d.]+)<\/string>/.exec(file); | ||
if (!matches) { | ||
throw new Error('Couldn\'t find the macOS version'); | ||
} | ||
version = matches[1]; | ||
} | ||
return version; | ||
}; | ||
module.exports = getVersion; | ||
const clean = version => version.split('.').length === 2 ? `${version}.0` : version; | ||
const x = module.exports; | ||
x.is = input => { | ||
if (!isMacos) { | ||
return false; | ||
} | ||
return semver.satisfies(getVersion(), clean(input)); | ||
}; | ||
x.isGreaterThanOrEqualTo = input => { | ||
if (!isMacos) { | ||
return false; | ||
} | ||
return semver.gte(getVersion(), clean(input)); | ||
}; | ||
x.assert = input => { | ||
if (!x.is(input)) { | ||
throw new Error(`Requires macOS ${input}`); | ||
} | ||
}; | ||
x.assertGreaterThanOrEqualTo = input => { | ||
if (!x.isGreaterThanOrEqualTo(input)) { | ||
throw new Error(`Requires macOS ${input} or later`); | ||
} | ||
}; |
{ | ||
"name": "macos-version", | ||
"version": "3.0.0", | ||
"description": "Get the macOS version of the current system", | ||
"version": "4.0.0", | ||
"description": "Get or check the current macOS version", | ||
"license": "MIT", | ||
@@ -22,12 +22,17 @@ "repository": "sindresorhus/macos-version", | ||
"keywords": [ | ||
"macos", | ||
"os", | ||
"macos", | ||
"osx", | ||
"darwin", | ||
"operating", | ||
"system", | ||
"platform", | ||
"version" | ||
"version", | ||
"release", | ||
"semver", | ||
"check", | ||
"assert", | ||
"condition" | ||
], | ||
"dependencies": { | ||
"execa": "^0.4.0" | ||
"semver": "^5.3.0" | ||
}, | ||
@@ -37,6 +42,3 @@ "devDependencies": { | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
# macos-version [![Build Status](https://travis-ci.org/sindresorhus/macos-version.svg?branch=master)](https://travis-ci.org/sindresorhus/macos-version) | ||
> Get the macOS version of the current system. Example: `10.9.3` | ||
> Get or check the current macOS version | ||
@@ -18,13 +18,42 @@ | ||
macosVersion().then(version => { | ||
console.log(version); | ||
//=> '10.9.3' | ||
}); | ||
macosVersion(); | ||
//=> '10.12.3' | ||
macosVersion.is('>10.10'); | ||
//=> true | ||
macosVersion.assertGreaterThanOrEqualTo('10.12.5'); | ||
//=> [Error: Requires macOS 10.10.5 or later] | ||
``` | ||
## API | ||
### macosVersion() | ||
Returns the macOS version. | ||
### macosVersion.is(semverRange) | ||
Returns a `boolean` of whether the specified [semver range](https://github.com/npm/node-semver#ranges) matches the macOS version. | ||
### macosVersion.isGreaterThanOrEqualTo(version) | ||
Returns a `boolean` of whether the macOS version is greater than or equal to the specified version. | ||
### macosVersion.assert(semverRange) | ||
Throws an error if the specified [semver range](https://github.com/npm/node-semver#ranges) does not match the macOS version. | ||
### macosVersion.assertGreaterThanOrEqualTo(version) | ||
Throws an error if the macOS version is not greater than or equal to the specified version. | ||
*Prefer this over `.assert()` whenever possible as it outputs a more user-friendly error message.* | ||
## Related | ||
- [macos-version-cli](https://github.com/sindresorhus/macos-version-cli) - CLI for this module | ||
- [osx-release](https://github.com/sindresorhus/osx-release) - Get the name and version of a macOS release from the Darwin version | ||
- [macos-release](https://github.com/sindresorhus/macos-release) - Get the name and version of a macOS release from the Darwin version | ||
@@ -31,0 +60,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
4580
44
63
0
2
+ Addedsemver@^5.3.0
+ Addedsemver@5.7.2(transitive)
- Removedexeca@^0.4.0
- Removedcross-spawn-async@2.2.5(transitive)
- Removedexeca@0.4.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedlru-cache@4.1.5(transitive)
- Removednpm-run-path@1.0.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedpath-key@1.0.0(transitive)
- Removedpseudomap@1.0.2(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedwhich@1.3.1(transitive)
- Removedyallist@2.1.2(transitive)