const { chmodSync, existsSync, mkdirSync, copyFileSync } = require('fs') const { EventEmitter } = require('events'); const path = require('path'); const { execFile } = require('child_process'); const homeDir = require('os').homedir(); class ClipboardEventListener extends EventEmitter { constructor() { super(); this.child = null; } startListening() { const { platform } = process; if (platform === 'win32') { this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-win32.exe')); } else if (platform === 'linux') { // linux: cant execFile without chmod, and cant chmod in app.asar // so we need to copy the file to /usr/bin const target = path.resolve(homeDir, '.local', 'bin') const p = path.join(__dirname, 'platform/clipboard-event-handler-linux') if(!existsSync(target)) { mkdirSync(target) } copyFileSync(p, path.join(target, 'clipboard-event-handler-linux')) chmodSync(path.join(target, 'clipboard-event-handler-linux'), 0o755) this.child = execFile(path.join(target, 'clipboard-event-handler-linux')); } else if (platform === 'darwin') { this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac')); } else { throw 'Not yet supported'; } this.child.stdout.on('data', (data) => { if (data.trim() === 'CLIPBOARD_CHANGE') { this.emit('change'); } }); this.child.stdout.on('close', () => { this.emit('close'); }); this.child.stdout.on('exit', () => { this.emit('exit'); }); } stopListening() { const res = this.child.kill(); return res; } } module.exports = new ClipboardEventListener();