Skip to content

Commit 4c87df3

Browse files
committed
Add hyper-init extension
1 parent 032d7ea commit 4c87df3

File tree

13 files changed

+109
-0
lines changed

13 files changed

+109
-0
lines changed

index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const { rulesHandler } = require('./src/rules/rules-handler')
4+
const waitFor = require('./src/wait-for')
5+
6+
const init = {}
7+
const terminal = {}
8+
9+
exports.onApp = ({ config }) =>
10+
Object.assign(init, config.getConfig().init)
11+
12+
exports.getTabProps = (uid, parentProps, props) =>
13+
Object.assign(terminal, uid, { tabs: parentProps.tabs }) &&
14+
Object.assign({}, props)
15+
16+
exports.reduceTermGroups = reducer =>
17+
Object.assign(terminal, reducer.termGroups) && reducer
18+
19+
exports.middleware = store => next => action => {
20+
if (action.type === 'SESSION_ADD')
21+
Object.assign(terminal, { splitDirection: action.splitDirection })
22+
23+
next(action)
24+
}
25+
26+
exports.onWindow = app =>
27+
app.rpc.on('execute commands', ({ uid, terminal }) =>
28+
Object.keys(init).map(key =>
29+
init[key].commands.map(cmd =>
30+
rulesHandler({ init, key, cmd, app, uid, terminal }))
31+
)
32+
)
33+
34+
exports.onRendererWindow = app =>
35+
waitFor(app, 'rpc', rpc =>
36+
rpc.on('session add', ({ uid }) =>
37+
rpc.emit('execute commands', { uid, terminal })
38+
)
39+
)

src/is/is-new-window.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { numberOfWindows } = require('../number-of-windows')
2+
3+
exports.isNewWindow = terminal =>
4+
!terminal.splitDirection &&
5+
Object.keys(terminal).length <= 2 &&
6+
numberOfWindows() > 2

src/is/is-once.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { numberOfWindows } = require('../number-of-windows')
2+
3+
exports.isOnce = terminal =>
4+
numberOfWindows() <= 2 &&
5+
Object.keys(terminal).length <= 2

src/is/is-tab.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.isTab = terminal =>
2+
terminal.uid ?
3+
!terminal[terminal.uid].direction &&
4+
Object.keys(terminal.tabs).length > 1 :
5+
''

src/number-of-windows.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { BrowserWindow } = require('electron')
2+
3+
exports.numberOfWindows = () =>
4+
BrowserWindow.getAllWindows().length

src/rules/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { onceRule } = require('./once-rule')
2+
const { onNewWindowRule } = require('./on-new-window-rule')
3+
const { onSplittedWindowsRule } = require('./on-splitted-windows-rule')
4+
const { onTabsRule } = require('./on-tabs-rule')
5+
const { onAllRule } = require('./on-all-rule')
6+
7+
module.exports = {
8+
'once': onceRule,
9+
'windows': onNewWindowRule,
10+
'splitted': onSplittedWindowsRule,
11+
'tabs': onTabsRule,
12+
'all': onAllRule
13+
}

src/rules/on-all-rule.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.onAllRule = ({ app, uid, cmd }) =>
2+
app.sessions.get(uid).write(`${cmd}\r`)

src/rules/on-new-window-rule.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { isNewWindow } = require('../is/is-new-window')
2+
3+
exports.onNewWindowRule = ({ app, uid, terminal, cmd }) =>
4+
isNewWindow(terminal) ?
5+
app.sessions.get(uid).write(`${cmd}\r`) :
6+
''
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exports.onSplittedWindowsRule = ({ app, uid, cmd, terminal }) =>
2+
!!terminal.splitDirection ?
3+
app.sessions.get(uid).write(`${cmd}\r`) :
4+
''

src/rules/on-tabs-rule.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { isTab } = require('../is/is-tab')
2+
3+
exports.onTabsRule = ({ app, uid, terminal, cmd }) =>
4+
isTab(terminal) &&
5+
!terminal.splitDirection ?
6+
app.sessions.get(uid).write(`${cmd}\r`) :
7+
''

0 commit comments

Comments
 (0)