-
Notifications
You must be signed in to change notification settings - Fork 0
/
createTmuxSessions.js
executable file
·63 lines (55 loc) · 1.68 KB
/
createTmuxSessions.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
#!/bin/env node
//TODO append description
'use strict';
const fs = require('node:fs');
const {execSync} = require('node:child_process');
const extractValue = require('./utils').extractValue;
const configPath = extractValue('--session-config');
const encodingOption = {encoding: 'utf-8'};
const orientation = {'vertical': '-h', 'horizontal': ''};
try {
/** @type {
* {
* name: string,
* windows: {
* number: number,
* panes: {
* number: number,
* commands: string[],
* splits: {
* orientation: string
* }[]
* }[]
* }[]
* }[]
}
* */
const sessionConfig = JSON.parse(fs.readFileSync(configPath, encodingOption));
const desiredSessions = sessionConfig.map(session => session.name);
execSync('tmux ls | awk \'{ print $1 }\'', encodingOption)
.split('\n')
.filter(session => session)
.map(session => session.replace(':', ''))
.filter(session => desiredSessions.includes(session))
.forEach(session => {
execSync(`tmux kill-session -t ${session}`, encodingOption);
});
sessionConfig.forEach(session => {
execSync(`tmux new -d -s ${session.name}`, encodingOption);
session.windows.forEach(window => {
window.panes.forEach(pane => {
pane.splits.forEach(split => {
execSync(`tmux split-window ${orientation[split.orientation]} -t ${session.name}:${window.number}.${pane.number}`, encodingOption);
});
pane.commands.forEach(command => {
execSync(`tmux send -t ${session.name}:${window.number}.${pane.number} "${command}" ENTER`, encodingOption);
});
});
});
});
process.exit(0);
} catch (error) {
console.error(`ERROR: ${error.message}`);
console.error(error);
process.exit(1);
}