From d417dc02f973e64a700f6582bb8906740ef27a4b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 23 Oct 2023 03:30:51 +0700 Subject: [PATCH] Require Node.js 18 --- example.js | 6 ++---- index.js | 15 +++++++++++---- lib/linux.js | 10 +++++----- lib/macos.js | 10 +++++----- lib/termux.js | 14 +++++++------- lib/windows.js | 12 ++++++------ package.json | 16 +++++++++------- 7 files changed, 45 insertions(+), 38 deletions(-) diff --git a/example.js b/example.js index 14cca99..76631c1 100644 --- a/example.js +++ b/example.js @@ -1,6 +1,4 @@ import clipboard from './index.js'; -(async () => { - clipboard.write('你好🦄'); - console.log(await clipboard.read()); -})(); +await clipboard.write('你好🦄'); +console.log(await clipboard.read()); diff --git a/index.js b/index.js index 0b33b95..f00dbb8 100644 --- a/index.js +++ b/index.js @@ -7,23 +7,30 @@ import windows from './lib/windows.js'; const platformLib = (() => { switch (process.platform) { - case 'darwin': + case 'darwin': { return macos; - case 'win32': + } + + case 'win32': { return windows; - case 'android': + } + + case 'android': { if (process.env.PREFIX !== '/data/data/com.termux/files/usr') { throw new Error('You need to install Termux for this module to work on Android: https://termux.com'); } return termux; - default: + } + + default: { // `process.platform === 'linux'` for WSL. if (isWSL) { return windows; } return linux; + } } })(); diff --git a/lib/linux.js b/lib/linux.js index 4d1b65f..47b2252 100644 --- a/lib/linux.js +++ b/lib/linux.js @@ -1,6 +1,6 @@ import path from 'node:path'; import {fileURLToPath} from 'node:url'; -import execa from 'execa'; +import {execa, execaSync} from 'execa'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -39,10 +39,10 @@ const xselWithFallback = async (argumentList, options) => { const xselWithFallbackSync = (argumentList, options) => { try { - return execa.sync(xsel, argumentList, options).stdout; + return execaSync(xsel, argumentList, options).stdout; } catch (xselError) { try { - return execa.sync(xselFallback, argumentList, options).stdout; + return execaSync(xselFallback, argumentList, options).stdout; } catch (fallbackError) { throw makeError(xselError, fallbackError); } @@ -50,10 +50,10 @@ const xselWithFallbackSync = (argumentList, options) => { }; const clipboard = { - copy: async options => { + async copy(options) { await xselWithFallback(copyArguments, options); }, - copySync: options => { + copySync(options) { xselWithFallbackSync(copyArguments, options); }, paste: options => xselWithFallback(pasteArguments, options), diff --git a/lib/macos.js b/lib/macos.js index 0ed542b..7a18ebf 100644 --- a/lib/macos.js +++ b/lib/macos.js @@ -1,17 +1,17 @@ -import execa from 'execa'; +import {execa, execaSync} from 'execa'; const env = { - LC_CTYPE: 'UTF-8', + LC_CTYPE: 'UTF-8', // eslint-disable-line unicorn/text-encoding-identifier-case }; const clipboard = { copy: async options => execa('pbcopy', {...options, env}), - paste: async options => { + async paste(options) { const {stdout} = await execa('pbpaste', {...options, env}); return stdout; }, - copySync: options => execa.sync('pbcopy', {...options, env}), - pasteSync: options => execa.sync('pbpaste', {...options, env}).stdout, + copySync: options => execaSync('pbcopy', {...options, env}), + pasteSync: options => execaSync('pbpaste', {...options, env}).stdout, }; export default clipboard; diff --git a/lib/termux.js b/lib/termux.js index 39906fc..53156f2 100644 --- a/lib/termux.js +++ b/lib/termux.js @@ -1,4 +1,4 @@ -import execa from 'execa'; +import {execa, execaSync} from 'execa'; const handler = error => { if (error.code === 'ENOENT') { @@ -9,14 +9,14 @@ const handler = error => { }; const clipboard = { - copy: async options => { + async copy(options) { try { await execa('termux-clipboard-set', options); } catch (error) { handler(error); } }, - paste: async options => { + async paste(options) { try { const {stdout} = await execa('termux-clipboard-get', options); return stdout; @@ -24,16 +24,16 @@ const clipboard = { handler(error); } }, - copySync: options => { + copySync(options) { try { - execa.sync('termux-clipboard-set', options); + execaSync('termux-clipboard-set', options); } catch (error) { handler(error); } }, - pasteSync: options => { + pasteSync(options) { try { - return execa.sync('termux-clipboard-get', options).stdout; + return execaSync('termux-clipboard-get', options).stdout; } catch (error) { handler(error); } diff --git a/lib/windows.js b/lib/windows.js index acda914..9b48a22 100644 --- a/lib/windows.js +++ b/lib/windows.js @@ -1,23 +1,23 @@ import path from 'node:path'; import {fileURLToPath} from 'node:url'; -import execa from 'execa'; -import arch from 'arch'; +import {execa, execaSync} from 'execa'; +import {is64bitSync} from 'is64bit'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const binarySuffix = arch() === 'x64' ? 'x86_64' : 'i686'; +const binarySuffix = is64bitSync() ? 'x86_64' : 'i686'; // Binaries from: https://github.com/sindresorhus/win-clipboard const windowBinaryPath = path.join(__dirname, `../fallbacks/windows/clipboard_${binarySuffix}.exe`); const clipboard = { copy: async options => execa(windowBinaryPath, ['--copy'], options), - paste: async options => { + async paste(options) { const {stdout} = await execa(windowBinaryPath, ['--paste'], options); return stdout; }, - copySync: options => execa.sync(windowBinaryPath, ['--copy'], options), - pasteSync: options => execa.sync(windowBinaryPath, ['--paste'], options).stdout, + copySync: options => execaSync(windowBinaryPath, ['--copy'], options), + pasteSync: options => execaSync(windowBinaryPath, ['--paste'], options).stdout, }; export default clipboard; diff --git a/package.json b/package.json index a99b331..1b875cb 100644 --- a/package.json +++ b/package.json @@ -12,12 +12,14 @@ }, "type": "module", "exports": { + "types": "./index.d.ts", "node": "./index.js", "default": "./browser.js" }, "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + "node": ">=18" }, + "sideEffects": false, "scripts": { "test": "xo && ava && tsd" }, @@ -42,14 +44,14 @@ "xsel" ], "dependencies": { - "arch": "^2.2.0", - "execa": "^5.1.1", - "is-wsl": "^2.2.0" + "execa": "^8.0.1", + "is-wsl": "^3.1.0", + "is64bit": "^2.0.0" }, "devDependencies": { - "ava": "^3.15.0", - "tsd": "^0.18.0", - "xo": "^0.45.0" + "ava": "^5.3.1", + "tsd": "^0.29.0", + "xo": "^0.56.0" }, "ava": { "serial": true