Coder Social home page Coder Social logo

macos-version's Introduction

macos-version

Get or check the current macOS version

Install

$ npm install macos-version

Usage

import {
	macOSVersion,
	isMacOSVersion,
	isMacOSVersionGreaterThanOrEqualTo,
	assertMacOSVersion,
	assertMacOSVersionGreaterThanOrEqualTo,
	assertMacOS,
	isMacOS,
} from 'macos-version';

macOSVersion();
//=> '10.12.3'

isMacOSVersion('>10.10');
//=> true

isMacOSVersionGreaterThanOrEqualTo('10.10');
//=> true

assertMacOSVersion('>=10.12.5');
//=> [Error: Requires macOS >=10.12.5]

assertMacOSVersionGreaterThanOrEqualTo('10.12.5');
//=> [Error: Requires macOS 10.12.5 or later]

assertMacOS();
//=> [Error: Requires macOS]

if (isMacOS) {
	console.log('macOS');
}

API

macOSVersion()

Returns the macOS version or undefined if the platform is not macOS.

isMacOSVersion(semverRange)

Returns a boolean of whether the specified semver range matches the macOS version.

isMacOSVersionGreaterThanOrEqualTo(version)

Returns a boolean of whether the macOS version is greater than or equal to the specified version.

assertMacOSVersion(semverRange)

Throws an error if the specified semver range does not match the macOS version.

assertMacOSVersionGreaterThanOrEqualTo(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.

assertMacOS()

Throws an error if the platform is not macOS.

isMacOS

Type: boolean

Whether the platform is macOS.

Related

macos-version's People

Contributors

albinekb avatar arthurvr avatar bendingbender avatar briangonzalez avatar leelow avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

macos-version's Issues

Error on MacOS 10.13

Two lines of code:

const macosVersion = require('macos-version')
console.log(macosVersion.is('>10.10'))

Make this error:

/path/to/project/node_modules/semver/semver.js:293
    throw new TypeError('Invalid Version: ' + version);
    ^

TypeError: Invalid Version: 10.13
    at new SemVer (/path/to/project/node_modules/semver/semver.js:293:11)
    at compare (/path/to/project/node_modules/semver/semver.js:566:10)
    at Function.gte (/path/to/project/node_modules/semver/semver.js:615:10)
    at Function.x.isGreaterThanOrEqualTo.input [as isGreaterThanOrEqualTo] (/path/to/project/node_modules/macos-version/index.js:46:16)
    at Function.x.assertGreaterThanOrEqualTo.input [as assertGreaterThanOrEqualTo] (/path/to/project/node_modules/macos-version/index.js:56:9)
    at new Aperture (/path/to/project/node_modules/aperture/index.js:12:18)
    at module.exports (/path/to/project/node_modules/aperture/index.js:118:24)
    at Object.<anonymous> (/path/to/project/screencapture.js:2:37)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)

As the title says, I'm using MacOS Sierra. I originally reported this at wulkano/Aperture/issues/42

Closes #4

Incorrect version returned for macOS Big Sur

First, thank you for all the amazing open source libraries you've provided and supported over the years. You're a hero.

I think this repo needs to be updated for proper Big Sur support. I'm getting 10.16.0 as macOS version while running Big Sur 11.0.1. I can look into making a PR later tonight but just wanted to point it out first and make sure I wasn't missing something simple.

code

import macosRelease from 'macos-release';
import macosVersion from 'macos-version';

//...

log.info(`macOS Release: ${macosRelease().name} ${macosRelease().version}`);
log.info(`macOS Version: ${macosVersion()}`);

output

macOS Release: Big Sur 11
macOS Version: 10.16.0

versions
macos-release: 2.4.1
macos-version: 5.2.0

my computer
image

(node:21734) Warning: N-API is an experimental feature and could change at any time.

When I use macosVersion.isGreaterThanOrEqualTo('10.15') return this warning on console.log

(node:21734) Warning: N-API is an experimental feature and could change at any time.

but when I use macosVersion() >= '10.15.0', the warning not shown. Why?

Could my code macosVersion() >= '10.15.0' give different result than macosVersion.isGreaterThanOrEqualTo('10.15') ?

I used on node 16.

Is throwing an error in getVersion the right experience?

I am currently using macos-version by way of another dependency, and my tests are failing in CI because of this library, which throws when macosVersion() is called.

Looking at the code, I see that getVersion does more than get the version, it returns the version if on macOS, otherwise it raises an error. In my opinion, this side effect is unexpected.

https://github.com/sindresorhus/macos-version/blob/master/index.js#L11-L13

You may be wondering what my use case is. I am mocking out some macOS functionality in my test suite, and I don't require macOS for my tests to run because of these mocks. However, the tests fail in CI because of the raised error.

A Solution

I believe that getVersion should return undefined if a valid version is not found and not have the side effect mentioned above.

Additionally, I think another function, macosVersion.assertMacOS could serve the use case of throwing if on another platform, and could be used via the end developer's discretion.

If you like this approach, I can help get us there.

Release 6.0.0 breaks Electron apps

Electron renderer process importing macos-version is broken with commit b42707c (b42707c).

The error message is like:

internal/modules/cjs/loader.js:890 Uncaught Error: Cannot find module 'node:process'
Require stack:

  • /index.html
    at Module._resolveFilename (internal/modules/cjs/loader.js:887)
    at Function.o._resolveFilename (electron/js2c/renderer_init.js:33)
    at Module._load (internal/modules/cjs/loader.js:732)
    at Function.f._load (electron/js2c/asar_bundle.js:5)
    at Function.o._load (electron/js2c/renderer_init.js:33)
    at Module.require (internal/modules/cjs/loader.js:959)
    at require (internal/modules/cjs/helpers.js:88)
    at Object.node:process (index.js:2791)
    at webpack_require (index.js:9052)
    at eval (index.js:11)

Removing import of 'macos-version' or revert to older release and the error is gone.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.