forked from CyrusNuevoDia/iterm-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-iterm-workspace
executable file
·54 lines (44 loc) · 1.48 KB
/
bootstrap-iterm-workspace
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
#!/usr/bin/env osascript -l JavaScript
function bootstrap(session, command, split) {
if (command instanceof Array) {
let sessions = [session]
for (let i = 1; i < command.length; ++i) {
sessions.push(session[`${split}WithSameProfile`]())
}
const nextSplit =
split === "splitVertically" ? "splitHorizontally" : "splitVertically"
for (let i = 0; i < command.length; ++i) {
bootstrap(sessions[i], command[i], nextSplit)
}
} else if (command) {
session.write({ text: command })
}
}
function tabConfiguration({ tabs }) {
const wrapWith = cd => cmd =>
cmd instanceof Array ? cmd.map(wrapWith(cd)) : cmd ? `${cd} && ${cmd}` : cd
return tabs.map(({ cd, commands, firstSplit }) => ({
firstSplit: firstSplit || "splitHorizontally",
commands: cd ? commands.map(wrapWith(cd)) : commands,
}))
}
function main(config) {
const terminal = Application("iTerm")
terminal.includeStandardAdditions = true
const window = terminal.currentWindow()
let tabs = [window.currentTab()]
const tabConfig = tabConfiguration(config)
for (let i = 0; i < tabConfig.length; ++i) {
if (i !== 0) {
tabs.push(window.createTabWithDefaultProfile())
}
const { commands, firstSplit } = tabConfig[i]
bootstrap(tabs[i].currentSession(), commands, firstSplit)
}
tabs[0].select()
}
function run(argv) {
const fileContents = $.NSString.stringWithContentsOfFile(argv[0]).js || "[]"
const config = JSON.parse(fileContents)
main(config)
}