From 09870d0b725e4b31331d5f5209e539ef273fe549 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 12 Aug 2021 19:12:37 +0200 Subject: [PATCH] Require Node.js 12.20 and move to ESM --- .github/workflows/main.yml | 8 ++---- index.d.ts | 55 ++++++++++++++++---------------------- index.js | 20 +++++++------- index.test-d.ts | 6 ++--- package.json | 10 ++++--- readme.md | 4 +-- test.js | 6 ++--- 7 files changed, 48 insertions(+), 61 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b85fc2a..441975c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index b963835..8c0e97a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,40 +1,31 @@ -declare const macosRelease: { - /** - Get the name and version of a macOS release from the Darwin version. +/** +Get the name and version of a macOS release from the Darwin version. - @param release - By default, the current operating system is used, but you can supply a custom [Darwin kernel version](https://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history), which is the output of [`os.release()`](https://nodejs.org/api/os.html#os_os_release). +@param release - By default, the current operating system is used, but you can supply a custom [Darwin kernel version](https://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history), which is the output of [`os.release()`](https://nodejs.org/api/os.html#os_os_release). - @example - ``` - import * as os from 'os'; - import macosRelease = require('macos-release'); +@example +``` +import os from 'node:os'; +import macosRelease from 'macos-release'; - // On a macOS Sierra system +// On a macOS Sierra system - macosRelease(); - //=> {name: 'Sierra', version: '10.12'} +macosRelease(); +//=> {name: 'Sierra', version: '10.12'} - os.release(); - //=> 13.2.0 - // This is the Darwin kernel version +os.release(); +//=> 13.2.0 +// This is the Darwin kernel version - macosRelease(os.release()); - //=> {name: 'Sierra', version: '10.12'} +macosRelease(os.release()); +//=> {name: 'Sierra', version: '10.12'} - macosRelease('14.0.0'); - //=> {name: 'Yosemite', version: '10.10'} +macosRelease('14.0.0'); +//=> {name: 'Yosemite', version: '10.10'} - macosRelease('20.0.0'); - //=> {name: 'Big Sur', version: '11'} - ``` - */ - (): {name: string, version: string} - (release: string): {name: string, version: string} | undefined; - - // TODO: remove this in the next major version, refactor the whole definition to: - // declare function macosRelease(release?: string): {name: string, version: string}; - // export = macosRelease; - default: typeof macosRelease; -}; - -export = macosRelease; +macosRelease('20.0.0'); +//=> {name: 'Big Sur', version: '11'} +``` +*/ +export default function macosRelease(): {name: string; version: string}; +export default function macosRelease(release: string): {name: string; version: string} | undefined; diff --git a/index.js b/index.js index eeb444a..8db9845 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ -'use strict'; -const os = require('os'); +import os from 'node:os'; const nameMap = new Map([ [21, ['Monterey', '12']], @@ -18,20 +17,19 @@ const nameMap = new Map([ [8, ['Tiger', '10.4']], [7, ['Panther', '10.3']], [6, ['Jaguar', '10.2']], - [5, ['Puma', '10.1']] + [5, ['Puma', '10.1']], ]); -const macosRelease = release => { +export default function macosRelease(release) { release = Number((release || os.release()).split('.')[0]); - const [name, version] = nameMap.get(release); + const [name, version] = nameMap.get(release) || { + name: 'Unknown', + version: '', + }; return { name, - version + version, }; -}; - -module.exports = macosRelease; -// TODO: remove this in the next major version -module.exports.default = macosRelease; +} diff --git a/index.test-d.ts b/index.test-d.ts index f6b5ea8..1fc83e9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import macosRelease = require('.'); +import macosRelease from './index.js'; -expectType<{name: string, version: string}>(macosRelease()); -expectType<{name: string, version: string} | undefined>(macosRelease('foo')); +expectType<{name: string; version: string}>(macosRelease()); +expectType<{name: string; version: string} | undefined>(macosRelease('foo')); diff --git a/package.json b/package.json index a2777a3..741830e 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "email": "sindresorhus@gmail.com", "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" @@ -33,8 +35,8 @@ "version" ], "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" } } diff --git a/readme.md b/readme.md index 4dd2320..ef70b94 100644 --- a/readme.md +++ b/readme.md @@ -12,8 +12,8 @@ $ npm install macos-release ## Usage ```js -const os = require('os'); -const macosRelease = require('macos-release'); +import os from 'node:os'; +import macosRelease from 'macos-release'; // On a macOS Sierra system diff --git a/test.js b/test.js index 88bfdbd..c9b7db7 100644 --- a/test.js +++ b/test.js @@ -1,14 +1,14 @@ import test from 'ava'; -import macosRelease from '.'; +import macosRelease from './index.js'; test('main', t => { t.deepEqual(macosRelease('13.2.0'), { name: 'Mavericks', - version: '10.9' + version: '10.9', }); t.deepEqual(macosRelease('20.0.0'), { name: 'Big Sur', - version: '11' + version: '11', }); });