Skip to content

Commit

Permalink
Changed communication method
Browse files Browse the repository at this point in the history
Resolves #4
  • Loading branch information
timolins committed Nov 14, 2016
1 parent 15b681a commit 07e0a97
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
26 changes: 7 additions & 19 deletions bin/hyperlayout
Original file line number Diff line number Diff line change
@@ -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'))

Expand Down Expand Up @@ -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()
45 changes: 20 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const io = require('socket.io')()

let hyperlayout

// Resolves an array, to find deepest cild. [[[1]]] -> 1
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -89,7 +86,6 @@ class Hyperlayout {
}
} else if (lastIndex) {
runCommand(activeUid, pane.cmd)
this.socket.send('done')
this.lastIndex = 0
}
}
Expand All @@ -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':
Expand Down Expand Up @@ -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)
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 07e0a97

Please sign in to comment.