diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-09-29 01:06:01 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-29 01:06:01 +0000 |
commit | 4f4396d0a69d33541844e723cad033b0a927333b (patch) | |
tree | f3a75c0b41d8fe2b1e6ca730501e36cee5701705 /e2e/lib/clipboard.ts | |
parent | 0fc2eea7431649f85c6e5d57cca66457f24bb14d (diff) | |
parent | 9f0bc5732823505c91ce6b5ba3aa8e4b60ac93f6 (diff) |
Merge pull request #648 from ueokande/migrate-to-latest-lanthan
Clean E2E tests
Diffstat (limited to 'e2e/lib/clipboard.ts')
-rw-r--r-- | e2e/lib/clipboard.ts | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/e2e/lib/clipboard.ts b/e2e/lib/clipboard.ts new file mode 100644 index 0000000..c1eddbb --- /dev/null +++ b/e2e/lib/clipboard.ts @@ -0,0 +1,107 @@ +import { spawn } from 'child_process'; + +const readLinux = (): Promise<string> => { + let stdout = '', stderr = ''; + return new Promise((resolve) => { + let xsel = spawn('xsel', ['--clipboard', '--output']); + xsel.stdout.on('data', (data) => { + stdout += data; + }); + xsel.stderr.on('data', (data) => { + stderr += data; + }); + xsel.on('close', (code) => { + if (code !== 0) { + throw new Error(`xsel returns ${code}: ${stderr}`) + } + resolve(stdout); + }); + }); +}; + +const writeLinux = (data: string): Promise<string> => { + let stderr = ''; + return new Promise((resolve) => { + let xsel = spawn('xsel', ['--clipboard', '--input']); + xsel.stderr.on('data', (data) => { + stderr += data; + }); + xsel.on('close', (code) => { + if (code !== 0) { + throw new Error(`xsel returns ${code}: ${stderr}`) + } + resolve(); + }); + xsel.stdin.write(data); + xsel.stdin.end(); + }); +}; + +const readDarwin = (): Promise<string> => { + let stdout = '', stderr = ''; + return new Promise((resolve) => { + let pbpaste = spawn('pbpaste'); + pbpaste.stdout.on('data', (data) => { + stdout += data; + }); + pbpaste.stderr.on('data', (data) => { + stderr += data; + }); + pbpaste.on('close', (code) => { + if (code !== 0) { + throw new Error(`pbpaste returns ${code}: ${stderr}`) + } + resolve(stdout); + }); + }); +}; + +const writeDarwin = (data: string): Promise<string> => { + let stderr = ''; + return new Promise((resolve) => { + let pbcopy = spawn('pbcopy'); + pbcopy.stderr.on('data', (data) => { + stderr += data; + }); + pbcopy.on('close', (code) => { + if (code !== 0) { + throw new Error(`pbcopy returns ${code}: ${stderr}`) + } + resolve(); + }); + pbcopy.stdin.write(data); + pbcopy.stdin.end(); + }); +}; + +class UnsupportedError extends Error { + constructor(platform: string) { + super(); + this.message = `Unsupported platform: ${platform}`; + } +} + +const read = () => { + switch (process.platform) { + case 'linux': + return readLinux(); + case 'darwin': + return readDarwin(); + } + throw new UnsupportedError(process.platform); +} + +const write = (data: string) => { + switch (process.platform) { + case 'linux': + return writeLinux(data); + case 'darwin': + return writeDarwin(data); + } + throw new UnsupportedError(process.platform); +} + +export { + read, + write, +}; |