diff --git a/bin/hyperlayout b/bin/hyperlayout index 7b026eb..1571d3d 100755 --- a/bin/hyperlayout +++ b/bin/hyperlayout @@ -1,11 +1,9 @@ #!/usr/bin/env node const fs = require('fs') -const io = require('socket.io-client') const isHyper = require('is-hyper')() const debug = require('debug')('hyperlayout') const command = process.argv[2] - // Read json file const readJson = (dir, file) => JSON.parse(fs.readFileSync(`${dir}/${file}`, 'utf8')) @@ -65,29 +63,19 @@ function start() { return } - const config = getConfig(command) + const data = { + config: getConfig(command), + cwd: process.cwd() + } - if (!config) { + if (!data.config) { console.log(`Can't find any hyperlayout preset (.hyperlayout, package.json, ~./hyperlayout)`) return - } else if (config === 'error') { + } else if (data.config.layout === 'error') { return } - const socket = io('http://localhost:7150') - socket.on('ready', () => { - socket.emit('hyperlayout', process.cwd(), config) - }) - - socket.on('received', () => { - console.log('hyperlayout activated') - socket.close() - }) - - socket.on('connect_error', () => { - console.log(`Can't connect to plugin. Is hyperlayout installed in Hyper.app?`) - socket.close() - }) + console.log('[hyperlayout config]:' + JSON.stringify(data)) } start() diff --git a/index.js b/index.js index 3143c5c..e1ebc9b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -const io = require('socket.io')() - let hyperlayout // Resolves an array, to find deepest cild. [[[1]]] -> 1 @@ -50,10 +48,9 @@ function generateQueue(converted, mode = 'TAB') { // Hyperlayout instance class Hyperlayout { - constructor(cwd, store, config, socket) { + constructor({config, cwd}, store) { this.cwd = cwd this.store = store - this.socket = socket this.panes = [] this.lastIndex = 0 @@ -89,7 +86,6 @@ class Hyperlayout { } } else if (lastIndex) { runCommand(activeUid, pane.cmd) - this.socket.send('done') this.lastIndex = 0 } } @@ -105,24 +101,8 @@ class Hyperlayout { } } -// Listen for commands -const listen = store => { - io.on('connection', socket => { - socket.emit('ready') - socket.on('hyperlayout', (cwd, config) => { - hyperlayout = new Hyperlayout(cwd, store, config, socket) - socket.emit('received') - }) - }) - io.listen(7150) - io.on('error', err => { - console.log('New window failed to create socket server', err) - }) -} - // Request new Session (Tab, Pane) function requestSession(cwd, mode) { - console.log('new', mode) const payload = {cwd} switch (mode) { case 'HORIZONTAL': @@ -153,13 +133,28 @@ function focusUid({dispatch}, uid) { } // Listens for initial load and sessions +let lastUid exports.middleware = store => next => action => { - const {type} = action - if (type === 'CONFIG_LOAD') { - listen(store) + const {type, data} = action + const {sessions} = store.getState() + const {activeUid} = sessions + + // Check for hyperlayout config + if (type === 'SESSION_ADD_DATA') { + const testedData = /^\[hyperlayout config\]:(.*)/.exec(data) + if (testedData && testedData[1]) { + const config = JSON.parse(testedData[1]) + hyperlayout = new Hyperlayout(config, store) + return + } } + + // Check for new sessions if (type === 'SESSION_SET_PROCESS_TITLE' && hyperlayout) { - hyperlayout.work() + if (lastUid !== activeUid) { + hyperlayout.work() + } + lastUid = activeUid } next(action) } diff --git a/package.json b/package.json index a028fda..b1604e5 100644 --- a/package.json +++ b/package.json @@ -27,10 +27,7 @@ "xo": "^0.17.0" }, "dependencies": { - "child-process-promise": "^2.2.0", "debug": "^2.3.2", - "is-hyper": "^0.2.0", - "socket.io": "^1.5.1", - "socket.io-client": "^1.5.1" + "is-hyper": "^0.2.0" } }