Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 23, 2023
1 parent 435f4e2 commit d417dc0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 38 deletions.
6 changes: 2 additions & 4 deletions example.js
Original file line number Diff line number Diff line change
@@ -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());
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
})();

Expand Down
10 changes: 5 additions & 5 deletions lib/linux.js
Original file line number Diff line number Diff line change
@@ -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));

Expand Down Expand Up @@ -39,21 +39,21 @@ 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);
}
}
};

const clipboard = {
copy: async options => {
async copy(options) {
await xselWithFallback(copyArguments, options);
},
copySync: options => {
copySync(options) {
xselWithFallbackSync(copyArguments, options);
},
paste: options => xselWithFallback(pasteArguments, options),
Expand Down
10 changes: 5 additions & 5 deletions lib/macos.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 7 additions & 7 deletions lib/termux.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import execa from 'execa';
import {execa, execaSync} from 'execa';

const handler = error => {
if (error.code === 'ENOENT') {
Expand All @@ -9,31 +9,31 @@ 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;
} catch (error) {
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);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/windows.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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
Expand Down

0 comments on commit d417dc0

Please sign in to comment.