diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..6344655 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,33 @@ +/** +Write (copy) to the clipboard asynchronously. + +@param text - The text to write to the clipboard. +*/ +export function write(text: string): Promise; + +/** +Write (copy) to the clipboard synchronously. + +@param text - The text to write to the clipboard. + +@example +``` +import * as clipboardy from 'clipboardy'; + +clipboardy.writeSync('🦄'); + +clipboardy.readSync(); +//=> '🦄' +``` +*/ +export function writeSync(text: string): void; + +/** +Read (paste) from the clipboard asynchronously. +*/ +export function read(): Promise; + +/** +Read (paste) from the clipboard synchronously. +*/ +export function readSync(): string; diff --git a/index.js b/index.js index a8d6dce..6c53bff 100644 --- a/index.js +++ b/index.js @@ -16,28 +16,29 @@ function getPlatformLib() { 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: return linux; } } -exports.write = input => { - if (typeof input !== 'string') { - return Promise.reject(new TypeError(`Expected a string, got ${typeof input}`)); +exports.write = text => { + if (typeof text !== 'string') { + return Promise.reject(new TypeError(`Expected a string, got ${typeof text}`)); } - return platformLib.copy({input}).then(() => {}); + return platformLib.copy({input: text}).then(() => {}); }; exports.read = () => platformLib.paste({stripEof: false}); -exports.writeSync = input => { - if (typeof input !== 'string') { - throw new TypeError(`Expected a string, got ${typeof input}`); +exports.writeSync = text => { + if (typeof text !== 'string') { + throw new TypeError(`Expected a string, got ${typeof text}`); } - platformLib.copySync({input}); + platformLib.copySync({input: text}); }; exports.readSync = () => platformLib.pasteSync({stripEof: false}).stdout; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..83a918a --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,7 @@ +import {expectType} from 'tsd'; +import * as clipboardy from '.'; + +clipboardy.writeSync('🦄'); +expectType>(clipboardy.write('🦄')); +expectType(clipboardy.readSync()); +expectType>(clipboardy.read()); diff --git a/package.json b/package.json index 8d21115..fce4f0a 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ "index.js", + "index.d.ts", "lib", "fallbacks" ], @@ -34,11 +35,12 @@ "xsel" ], "dependencies": { - "arch": "^2.1.0", - "execa": "^0.10.0" + "arch": "^2.1.1", + "execa": "^1.0.0" }, "devDependencies": { - "ava": "*", - "xo": "*" + "ava": "^1.4.1", + "tsd": "^0.7.2", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index a917924..ce036b6 100644 --- a/readme.md +++ b/readme.md @@ -28,26 +28,30 @@ clipboardy.readSync(); ### clipboardy -#### .write(input) +#### .write(text) Write (copy) to the clipboard asynchronously. Returns a `Promise`. -##### input +##### text Type: `string` +The text to write to the clipboard. + #### .read() Read (paste) from the clipboard asynchronously. Returns a `Promise`. -#### .writeSync(input) +#### .writeSync(text) Write (copy) to the clipboard synchronously. -##### input +##### text Type: `string` +The text to write to the clipboard. + #### .readSync() Read (paste) from the clipboard synchronously. diff --git a/test.js b/test.js index 392c2f5..d3aff55 100644 --- a/test.js +++ b/test.js @@ -1,15 +1,15 @@ import {EOL} from 'os'; import {serial as test} from 'ava'; -import m from '.'; +import clipboardy from '.'; const writeRead = async input => { - await m.write(input); - return m.read(); + await clipboardy.write(input); + return clipboardy.read(); }; const writeReadSync = input => { - m.writeSync(input); - return m.readSync(); + clipboardy.writeSync(input); + return clipboardy.readSync(); }; test('async', async t => {