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 Oct 13, 2021
1 parent c59f194 commit 661d9da
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 120 deletions.
1 change: 0 additions & 1 deletion .github/funding.yml

This file was deleted.

9 changes: 3 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
os:
- ubuntu-latest
# - ubuntu-latest
- macos-latest
- windows-latest
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
13 changes: 8 additions & 5 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
/* eslint-env browser */
'use strict';

export const write = async text => {
const clipboard = {};

clipboard.write = async text => {
await navigator.clipboard.writeText(text);
};

export const read = async () => navigator.clipboard.readText();
clipboard.read = async () => navigator.clipboard.readText();

export const readSync = () => {
clipboard.readSync = () => {
throw new Error('`.readSync()` is not supported in browsers!');
};

export const writeSync = () => {
clipboard.writeSync = () => {
throw new Error('`.writeSync()` is not supported in browsers!');
};

export default clipboard;
9 changes: 5 additions & 4 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const clipboardy = require('.');
import clipboard from './index.js';

clipboardy.write('你好🦄');
clipboardy.read().then(console.log);
(async () => {
clipboard.write('你好🦄');
console.log(await clipboard.read());
})();
88 changes: 61 additions & 27 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,71 @@
/**
Write (copy) to the clipboard asynchronously.
declare const clipboard: {
/**
Write (copy) to the clipboard asynchronously.
@param text - The text to write to the clipboard.
*/
export function write(text: string): Promise<void>;
@param text - The text to write to the clipboard.
/**
Write (copy) to the clipboard synchronously.
@example
```
import clipboard from 'clipboardy';
Doesn't work in browsers.
await clipboard.write('🦄');
@param text - The text to write to the clipboard.
await clipboard.read();
//=> '🦄'
```
*/
write(text: string): Promise<void>;

@example
```
import * as clipboardy from 'clipboardy';
/**
Read (paste) from the clipboard asynchronously.
clipboardy.writeSync('🦄');
@example
```
import clipboard from 'clipboardy';
clipboardy.readSync();
//=> '🦄'
```
*/
export function writeSync(text: string): void;
await clipboard.write('🦄');
/**
Read (paste) from the clipboard asynchronously.
*/
export function read(): Promise<string>;
await clipboard.read();
//=> '🦄'
```
*/
read(): Promise<string>;

/**
Read (paste) from the clipboard synchronously.
/**
Write (copy) to the clipboard synchronously.
Doesn't work in browsers.
*/
export function readSync(): string;
__Doesn't work in browsers.__
@param text - The text to write to the clipboard.
@example
```
import clipboard from 'clipboardy';
clipboard.writeSync('🦄');
clipboard.readSync();
//=> '🦄'
```
*/
writeSync(text: string): void;

/**
Read (paste) from the clipboard synchronously.
__Doesn't work in browsers.__
@example
```
import clipboard from 'clipboardy';
clipboard.writeSync('🦄');
clipboard.readSync();
//=> '🦄'
```
*/
readSync(): string;
};

export default clipboard;
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
const isWSL = require('is-wsl');
const termux = require('./lib/termux.js');
const linux = require('./lib/linux.js');
const macos = require('./lib/macos.js');
const windows = require('./lib/windows.js');
import process from 'node:process';
import isWSL from 'is-wsl';
import termux from './lib/termux.js';
import linux from './lib/linux.js';
import macos from './lib/macos.js';
import windows from './lib/windows.js';

const platformLib = (() => {
switch (process.platform) {
Expand All @@ -27,22 +27,26 @@ const platformLib = (() => {
}
})();

exports.write = async text => {
const clipboard = {};

clipboard.write = async text => {
if (typeof text !== 'string') {
throw new TypeError(`Expected a string, got ${typeof text}`);
}

await platformLib.copy({input: text});
};

exports.read = async () => platformLib.paste({stripEof: false});
clipboard.read = async () => platformLib.paste({stripFinalNewline: false});

exports.writeSync = text => {
clipboard.writeSync = text => {
if (typeof text !== 'string') {
throw new TypeError(`Expected a string, got ${typeof text}`);
}

platformLib.copySync({input: text});
};

exports.readSync = () => platformLib.pasteSync({stripEof: false}).stdout;
clipboard.readSync = () => platformLib.pasteSync({stripFinalNewline: false});

export default clipboard;
10 changes: 5 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expectType} from 'tsd';
import * as clipboardy from '.';
import clipboard from './index.js';

clipboardy.writeSync('🦄');
expectType<Promise<void>>(clipboardy.write('🦄'));
expectType<string>(clipboardy.readSync());
expectType<Promise<string>>(clipboardy.read());
clipboard.writeSync('🦄');
expectType<Promise<void>>(clipboard.write('🦄'));
expectType<string>(clipboard.readSync());
expectType<Promise<string>>(clipboard.read());
24 changes: 15 additions & 9 deletions lib/linux.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
const path = require('path');
const execa = require('execa');
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import execa from 'execa';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const xsel = 'xsel';
const xselFallback = path.join(__dirname, '../fallbacks/linux/xsel');
Expand All @@ -23,10 +25,12 @@ const makeError = (xselError, fallbackError) => {

const xselWithFallback = async (argumentList, options) => {
try {
return await execa.stdout(xsel, argumentList, options);
const {stdout} = await execa(xsel, argumentList, options);
return stdout;
} catch (xselError) {
try {
return await execa.stdout(xselFallback, argumentList, options);
const {stdout} = await execa(xselFallback, argumentList, options);
return stdout;
} catch (fallbackError) {
throw makeError(xselError, fallbackError);
}
Expand All @@ -35,23 +39,25 @@ const xselWithFallback = async (argumentList, options) => {

const xselWithFallbackSync = (argumentList, options) => {
try {
return execa.sync(xsel, argumentList, options);
return execa.sync(xsel, argumentList, options).stdout;
} catch (xselError) {
try {
return execa.sync(xselFallback, argumentList, options);
return execa.sync(xselFallback, argumentList, options).stdout;
} catch (fallbackError) {
throw makeError(xselError, fallbackError);
}
}
};

module.exports = {
const clipboard = {
copy: async options => {
await xselWithFallback(copyArguments, options);
},
copySync: options => {
xselWithFallbackSync(copyArguments, options);
},
paste: options => xselWithFallback(pasteArguments, options),
pasteSync: options => xselWithFallbackSync(pasteArguments, options)
pasteSync: options => xselWithFallbackSync(pasteArguments, options),
};

export default clipboard;
17 changes: 10 additions & 7 deletions lib/macos.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';
const execa = require('execa');
import execa from 'execa';

const env = {
...process.env,
LC_CTYPE: 'UTF-8'
LC_CTYPE: 'UTF-8',
};

module.exports = {
const clipboard = {
copy: async options => execa('pbcopy', {...options, env}),
paste: async options => execa.stdout('pbpaste', {...options, env}),
paste: async options => {
const {stdout} = await execa('pbpaste', {...options, env});
return stdout;
},
copySync: options => execa.sync('pbcopy', {...options, env}),
pasteSync: options => execa.sync('pbpaste', {...options, env})
pasteSync: options => execa.sync('pbpaste', {...options, env}).stdout,
};

export default clipboard;
14 changes: 8 additions & 6 deletions lib/termux.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const execa = require('execa');
import execa from 'execa';

const handler = error => {
if (error.code === 'ENOENT') {
Expand All @@ -9,7 +8,7 @@ const handler = error => {
throw error;
};

module.exports = {
const clipboard = {
copy: async options => {
try {
await execa('termux-clipboard-set', options);
Expand All @@ -19,7 +18,8 @@ module.exports = {
},
paste: async options => {
try {
return await execa.stdout('termux-clipboard-get', options);
const {stdout} = await execa('termux-clipboard-get', options);
return stdout;
} catch (error) {
handler(error);
}
Expand All @@ -33,9 +33,11 @@ module.exports = {
},
pasteSync: options => {
try {
return execa.sync('termux-clipboard-get', options);
return execa.sync('termux-clipboard-get', options).stdout;
} catch (error) {
handler(error);
}
}
},
};

export default clipboard;
27 changes: 17 additions & 10 deletions lib/windows.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
'use strict';
const path = require('path');
const execa = require('execa');
const arch = require('arch');
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import execa from 'execa';
import arch from 'arch';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const binarySuffix = arch() === 'x64' ? 'x86_64' : 'i686';

// Binaries from: https://github.com/sindresorhus/win-clipboard
const windowBinaryPath = arch() === 'x64' ?
path.join(__dirname, '../fallbacks/windows/clipboard_x86_64.exe') :
path.join(__dirname, '../fallbacks/windows/clipboard_i686.exe');
const windowBinaryPath = path.join(__dirname, `../fallbacks/windows/clipboard_${binarySuffix}.exe`);

module.exports = {
const clipboard = {
copy: async options => execa(windowBinaryPath, ['--copy'], options),
paste: async options => execa.stdout(windowBinaryPath, ['--paste'], options),
paste: async options => {
const {stdout} = await execa(windowBinaryPath, ['--paste'], options);
return stdout;
},
copySync: options => execa.sync(windowBinaryPath, ['--copy'], options),
pasteSync: options => execa.sync(windowBinaryPath, ['--paste'], options)
pasteSync: options => execa.sync(windowBinaryPath, ['--paste'], options).stdout,
};

export default clipboard;
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
Loading

0 comments on commit 661d9da

Please sign in to comment.