-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (63 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('graceful-fs')
const { promisify } = require('util')
const { resolve } = require('path')
const { homedir } = require('os')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const access = promisify(fs.access)
const commandFile = resolve(homedir(), '.hyper_plugins', 'hyperalfred.txt')
async function loadCommand() {
try {
const hasCommand = await access(commandFile, fs.constants.R_OK | fs.constants.W_OK)
let command = await readFile(commandFile, 'utf8')
command = command.trim()
if (command.length === 0) {
return null
}
return command
} catch (err) {
if (err.code === 'ENOENT') {
console.log('[hyperalfred] command file not existing, ignoring')
}
console.error(err)
}
}
async function sendCommand(session, command) {
console.log(`[hyperalfred] sending command ${command} to window`)
if (session && typeof session.write === 'function') {
session.write(command)
session.write('\n')
}
}
async function clearCommand() {
try {
await writeFile(commandFile, '')
} catch (err) {
console.error(err)
}
}
async function main(session) {
const command = await loadCommand()
if (!command) {
console.log('[hyperalfred] no command found')
return
}
console.log(`[hyperalfred] command found : ${command}`)
await sendCommand(session, command)
await clearCommand()
}
exports.onWindow = win => {
win.rpc.on('hyperalfred write command', async (sessionId) => {
let session = win.sessions.get(sessionId)
await main(session)
})
}
exports.middleware = (store) => (next) => (action) => {
if ('SESSION_ADD' === action.type || 'SESSION_SET_ACTIVE' === action.type) {
const sessionId = action.uid
setTimeout(() => rpc.emit('hyperalfred write command', sessionId), 200)
next(action);
} else {
next(action);
}
}