fix: 修正linux下文件拷贝逻辑

This commit is contained in:
ZiuChen 2022-09-20 20:27:24 +08:00
parent 15a16071f6
commit f7a2215b64

View File

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