fix: linux用户改用不同的监听机制 监听异常时抛出错误

This commit is contained in:
ZiuChen
2022-09-20 13:40:49 +08:00
parent 5af526ed3e
commit e57c0aff71
2 changed files with 30 additions and 7 deletions

View File

@@ -1,7 +1,6 @@
const { chmodSync } = require('fs');
const { EventEmitter } = require('events');
const path = require('path');
const { execFile } = require('child_process');
const { execFile, exec } = require('child_process');
class ClipboardEventListener extends EventEmitter {
constructor() {
@@ -11,12 +10,11 @@ class ClipboardEventListener extends EventEmitter {
startListening() {
const { platform } = process;
if (platform === 'win32') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-win32.exe'));
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'));
}
else if (platform === 'linux') {
const p = path.join(__dirname, 'platform/clipboard-event-handler-linux');
chmodSync(p, 0o111); // modify file permission: everyone can execute, or it will fail
this.child = execFile(p);
// manually install clipboard-event-handler-linux to /usr/local/bin
this.child = exec('clipboard-event-handler-linux')
}
else if (platform === 'darwin') {
this.child = execFile(path.join(__dirname, 'platform/clipboard-event-handler-mac'));
@@ -29,6 +27,12 @@ class ClipboardEventListener extends EventEmitter {
this.emit('change');
}
});
this.child.stdout.on('close', (code) => {
this.emit('close', code);
});
this.child.stdout.on('exit', (code) => {
this.emit('exit', code);
});
}
stopListening() {
const res = this.child.kill();