Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2021
1 parent a4d2601 commit b42707c
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 200 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 6
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
193 changes: 93 additions & 100 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,93 @@
declare const macosVersion: {
/**
@returns The macOS version or `undefined` if the platform is not macOS.
@example
```
import macosVersion = require('macos-version');
macosVersion();
//=> '10.12.3'
```
*/
(): string | undefined;

/**
@returns Whether the specified [semver range](https://github.com/npm/node-semver#ranges) matches the macOS version.
@example
```
import macosVersion = require('macos-version');
macosVersion.is('>10.10');
//=> true
```
*/
is(semverRange: string): boolean;

/**
@returns Whether the macOS version is greater than or equal to the specified version.
@example
```
import macosVersion = require('macos-version');
macosVersion.isGreaterThanOrEqualTo('10.10');
//=> true
```
*/
isGreaterThanOrEqualTo(version: string): boolean;

/**
Throws an error if the specified [semver range](https://github.com/npm/node-semver#ranges) does not match the macOS version.
@example
```
import macosVersion = require('macos-version');
macosVersion.assert('>=10.12.5');
//=> [Error: Requires macOS >=10.12.5]
```
*/
assert(semverRange: string): void;

/**
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._
@example
```
import macosVersion = require('macos-version');
macosVersion.assertGreaterThanOrEqualTo('10.12.5');
//=> [Error: Requires macOS 10.12.5 or later]
```
*/
assertGreaterThanOrEqualTo(version: string): void;

/**
Throws an error if platform is not macOS.
@example
```
import macosVersion = require('macos-version');
macosVersion.assertMacOS();
//=> [Error: Requires macOS]
```
*/
assertMacOS(): void;

/**
Whether the platform is macOS.
@example
```
import macosVersion = require('macos-version');
if (macosVersion.isMacOS) {
console.log('macOS');
}
```
*/
readonly isMacOS: boolean;

// TODO: remove this in the next major version
default: typeof macosVersion;
};

export = macosVersion;
/**
@returns The macOS version or `undefined` if the platform is not macOS.
@example
```
import {macOSVersion} from 'macos-version';
macOSVersion();
//=> '10.12.3'
```
*/
export function macOSVersion(): string | undefined;

/**
@returns Whether the specified [semver range](https://github.com/npm/node-semver#ranges) matches the macOS version.
@example
```
import {isMacOSVersion} from 'macos-version';
isMacOSVersion('>10.10');
//=> true
```
*/
export function isMacOSVersion(semverRange: string): boolean;

/**
@returns Whether the macOS version is greater than or equal to the specified version.
@example
```
import {isMacOSVersionGreaterThanOrEqualTo} from 'macos-version';
isMacOSVersionGreaterThanOrEqualTo('10.10');
//=> true
```
*/
export function isMacOSVersionGreaterThanOrEqualTo(version: string): boolean;

/**
Throws an error if the specified [semver range](https://github.com/npm/node-semver#ranges) does not match the macOS version.
@example
```
import {assertMacOSVersion} from 'macos-version';
assertMacOSVersion('>=10.12.5');
//=> [Error: Requires macOS >=10.12.5]
```
*/
export function assertMacOSVersion(semverRange: string): void;

/**
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._
@example
```
import {assertMacOSVersionGreaterThanOrEqualTo} from 'macos-version';
assertMacOSVersionGreaterThanOrEqualTo('10.12.5');
//=> [Error: Requires macOS 10.12.5 or later]
```
*/
export function assertMacOSVersionGreaterThanOrEqualTo(version: string): void;

/**
Throws an error if platform is not macOS.
@example
```
import {assertMacOS} from 'macos-version';
assertMacOS();
//=> [Error: Requires macOS]
```
*/
export function assertMacOS(): void;

/**
Whether the platform is macOS.
@example
```
import {isMacOS} from 'macos-version';
if (isMacOS) {
console.log('macOS');
}
```
*/
export const isMacOS: boolean;
71 changes: 33 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
const fs = require('fs');
const semver = require('semver');
import process from 'node:process';
import fs from 'node:fs';
import semver from 'semver';

export const isMacOS = process.platform === 'darwin';

const isMacOS = process.platform === 'darwin';
let version;

const clean = version => {
Expand All @@ -20,15 +21,15 @@ const clean = version => {
};

const parseVersion = plist => {
const matches = /<key>ProductVersion<\/key>[\s]*<string>([\d.]+)<\/string>/.exec(plist);
const matches = /<key>ProductVersion<\/key>\s*<string>([\d.]+)<\/string>/.exec(plist);
if (!matches) {
return;
}

return matches[1].replace('10.16', '11');
};

const getVersion = () => {
export function macOSVersion() {
if (!isMacOS) {
return;
}
Expand All @@ -41,60 +42,54 @@ const getVersion = () => {
return;
}

version = matches;
}

if (version) {
return clean(version);
version = clean(matches);
}
};

module.exports = getVersion;
// TODO: remove this in the next major version
module.exports.default = getVersion;

getVersion._parseVersion = parseVersion;
return version;
}

getVersion.isMacOS = isMacOS;
if (process.env.NODE_ENV === 'test') {
macOSVersion._parseVersion = parseVersion;
}

getVersion.is = input => {
export function isMacOSVersion(semverRange) {
if (!isMacOS) {
return false;
}

input = input.replace('10.16', '11');
semverRange = semverRange.replace('10.16', '11');

return semver.satisfies(getVersion(), clean(input));
};
return semver.satisfies(macOSVersion(), clean(semverRange));
}

getVersion.isGreaterThanOrEqualTo = input => {
export function isMacOSVersionGreaterThanOrEqualTo(version) {
if (!isMacOS) {
return false;
}

input = input.replace('10.16', '11');
version = version.replace('10.16', '11');

return semver.gte(getVersion(), clean(input));
};
return semver.gte(macOSVersion(), clean(version));
}

getVersion.assert = input => {
input = input.replace('10.16', '11');
export function assertMacOSVersion(semverRange) {
semverRange = semverRange.replace('10.16', '11');

if (!getVersion.is(input)) {
throw new Error(`Requires macOS ${input}`);
if (!isMacOSVersion(semverRange)) {
throw new Error(`Requires macOS ${semverRange}`);
}
};
}

getVersion.assertGreaterThanOrEqualTo = input => {
input = input.replace('10.16', '11');
export function assertMacOSVersionGreaterThanOrEqualTo(version) {
version = version.replace('10.16', '11');

if (!getVersion.isGreaterThanOrEqualTo(input)) {
throw new Error(`Requires macOS ${input} or later`);
if (!isMacOSVersionGreaterThanOrEqualTo(version)) {
throw new Error(`Requires macOS ${version} or later`);
}
};
}

getVersion.assertMacOS = () => {
export function assertMacOS() {
if (!isMacOS) {
throw new Error('Requires macOS');
}
};
}
24 changes: 16 additions & 8 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {expectType} from 'tsd';
import macosVersion = require('.');
import {
macOSVersion,
isMacOSVersion,
isMacOSVersionGreaterThanOrEqualTo,
assertMacOSVersion,
assertMacOSVersionGreaterThanOrEqualTo,
assertMacOS,
isMacOS,
} from './index.js';

expectType<string | undefined>(macosVersion());
expectType<boolean>(macosVersion.is('>10.10'));
expectType<boolean>(macosVersion.isGreaterThanOrEqualTo('10.12.5'));
expectType<void>(macosVersion.assert('>10.10'));
expectType<void>(macosVersion.assertGreaterThanOrEqualTo('10.10'));
expectType<void>(macosVersion.assertMacOS());
expectType<boolean>(macosVersion.isMacOS);
expectType<string | undefined>(macOSVersion());
expectType<boolean>(isMacOSVersion('>10.10'));
expectType<boolean>(isMacOSVersionGreaterThanOrEqualTo('10.12.5'));
expectType<void>(assertMacOSVersion('>10.10'));
expectType<void>(assertMacOSVersionGreaterThanOrEqualTo('10.10'));
expectType<void>(assertMacOS());
expectType<boolean>(isMacOS);
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=6"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -35,11 +37,11 @@
"condition"
],
"dependencies": {
"semver": "^5.6.0"
"semver": "^7.3.5"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
Loading

0 comments on commit b42707c

Please sign in to comment.