-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
216 lines (189 loc) · 7.39 KB
/
index.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import { createNeededDirectoriesAndFiles } from "./startup.ts";
import { isTmuxSessionCurrentlyRunning } from "./utils/index.ts";
import { Tmux } from "./utils/tmux.ts"
const home = Deno.env.get("HOME");
const basePathToDisconnectedDirectory = `${home}/.config/disconnected`;
let editor = Deno.env.get("EDITOR")
if (!editor) {
editor = "nano"
}
interface IWindow {
name: string;
basePath: string;
commands: string[];
shouldCloseAfterCommand: boolean;
concatenateBasePathToGlobalBasePath: boolean;
}
const baseConfigFile = `{
"name": "SampleBaseConfig",
"basePath": "~/",
"startingWindow": "1",
"windows": [
{
"name": "listfiles",
"basePath": "",
"commands": ["ls"],
"shouldCloseAfterCommand": false,
"concatenateBasePathToGlobalBasePath": false
},
{
"name": "htop",
"basePath": "",
"commands": ["htop"],
"shouldCloseAfterCommand": true,
"concatenateBasePathToGlobalBasePath": false
}
]
}`
//const testCommand = new Command()
//.description(`TEST`)
//.action(() => {
//})
const initCommand = new Command()
.description(`Create all the files and folders needed to run disconnected. Files will be placed in ${basePathToDisconnectedDirectory}`)
.action(async () => {
await createNeededDirectoriesAndFiles(basePathToDisconnectedDirectory, baseConfigFile);
})
const startCommand = new Command()
.arguments("<name:string>")
.description("Start tmux with the supplied config file name.")
.action(async ( _options, name: string ) => {
const pathToBashScriptFile = `${basePathToDisconnectedDirectory}/bashScripts/${name}.sh`;
let doesFileExist = false;
for await (const dirEntry of Deno.readDir(basePathToDisconnectedDirectory)) {
if(dirEntry.name.includes(name)) {
doesFileExist = true;
break;
}
}
if(doesFileExist == false) {
console.log(`Could not find config file with name: ${name}`);
console.log(`Did you mean to run: disconnected new ${name}?`);
return;
}
if (await isTmuxSessionCurrentlyRunning(Tmux, name)) {
Tmux.attach(name);
return;
}
const decoder = new TextDecoder("utf-8");
const file = await Deno.readFile(`${basePathToDisconnectedDirectory}/${name}.json`);
const data = decoder.decode(file);
const configFile = JSON.parse(data.toString());
const lines = [];
const paneNumber = 1;
configFile.windows.forEach((window: IWindow, index: number) => {
if (configFile.basePath.includes("~")) {
configFile.basePath = configFile.basePath.replace("~", home as string)
}
if (window.basePath.includes("~")) {
window.basePath = window.basePath.replace("~", home as string)
}
if(window.name.includes(" ")) {
window.name = window.name.replace(" ", "-");
}
const windowNumber = index + 1;
if (index === 0) {
lines.push(`tmux new-session -d -s ${name} -n ${window.name}`);
} else {
lines.push(`tmux neww -t ${name} -n "${window.name}"`);
}
if(window.concatenateBasePathToGlobalBasePath) {
lines.push(
`tmux send -t ${name}:${windowNumber}.${paneNumber} "cd ${path.join(configFile.basePath, window.basePath)}" C-m`
);
} else {
lines.push(
`tmux send -t ${name}:${windowNumber}.${paneNumber} "cd ${window.basePath}" C-m`
)
}
window.commands.forEach((cmd: string) => {
lines.push(`tmux send -t ${name}:${windowNumber}.${paneNumber} "${cmd}" C-m`);
});
if("shouldCloseAfterCommand" in window && window.shouldCloseAfterCommand) {
lines.push(`tmux send -t ${name}:${windowNumber}.${paneNumber} "exit" C-m`);
}
});
lines.push(`tmux select-window -t ${configFile.startingWindow}`)
lines.push(`tmux attach -t ${name} -c ${configFile.basePath}`);
await Deno.writeTextFile(pathToBashScriptFile, lines.join("\n"));
await Deno.chmod(pathToBashScriptFile, 0o777);
const executeFile = new Deno.Command(Deno.env.get("SHELL") as string, { args: [`${pathToBashScriptFile}`] });
executeFile.spawn();
console.log("Run following command to attach to tmux.");
console.log(`tmux a -t ${name}`);
})
const listCommand = new Command()
.description("List all config files")
.action(async ( _options ) => {
for await (const dirEntry of Deno.readDir(basePathToDisconnectedDirectory)) {
if(dirEntry.isFile) {
if(dirEntry.name.includes(".json")) {
console.log(dirEntry.name.split(".json")[0]);
} else {
console.log(dirEntry.name);
}
}
}
})
const createNewConfigCommand = new Command()
.arguments("<name:string>")
.description("Create the config file")
.action(async ( _options, name: string ) => {
for await (const dirEntry of Deno.readDir(basePathToDisconnectedDirectory)) {
if(dirEntry.name.includes(name)) {
console.log(`File already exists with name: ${name}`);
console.log(`Did you mean to run: disconnected edit ${name}?`);
return;
}
}
const encoder = new TextEncoder();
const newConfigFile = baseConfigFile.replace(/SampleBaseConfig/gi, name);
const data = encoder.encode(newConfigFile)
Deno.writeFileSync(`${basePathToDisconnectedDirectory}/${name}.json`, data)
console.log("File created, opening in neovim");
const p = new Deno.Command(editor as string, { args: [`${basePathToDisconnectedDirectory}/${name}.json`] });
p.spawn();
})
// const deleteConfigCommand = new Command()
// .arguments("<name:string>")
// .description("Delete the config file")
// .action(( _options, name: string ) => {
// console.log(`You have hit the delete command with ${name}`)
// })
const editConfigCommand = new Command()
.arguments("<name:string>")
.description("Edit the config file")
.action(async ( _options, name: string ) => {
let doesTheDirectoryContainTheFile = false;
for await (const dirEntry of Deno.readDir(basePathToDisconnectedDirectory)) {
if(dirEntry.name.includes(name)) {
doesTheDirectoryContainTheFile = true;
break;
}
}
if(doesTheDirectoryContainTheFile === false) {
console.log(`Could not find config file with name: ${name}`);
console.log(`Did you mean to run: disconnected new ${name}?`);
return;
}
const p = new Deno.Command(editor as string, { args: [`${basePathToDisconnectedDirectory}/${name}.json`] });
p.spawn();
})
await new Command()
.name("Disconnected")
.version("0.3.0")
.description(`Disconnected is a powerful and versatile application that allows you to manage your terminal sessions with ease. As an alternative to tmuxinator, it offers a simple and intuitive cli that is perfect for both beginners and advanced users. With Disconnected, you can easily create, modify, and manage your terminal sessions with just a few commands.
One of the key features of Disconnected is its use of a JSON configuration file, which makes it easy to configure and customize your terminal sessions to your liking. Whether you're working on a complex project or just need to manage a few terminals at once, Disconnected makes it easy to get started.`)
.action(async (_options, ..._args) => {
await createNeededDirectoriesAndFiles(basePathToDisconnectedDirectory, baseConfigFile);
})
.command("init", initCommand)
.command("start", startCommand)
.command("list", listCommand)
.command("new", createNewConfigCommand)
// .command("delete", deleteConfigCommand)
.command("edit", editConfigCommand)
// .command("test", testCommand)
.parse(Deno.args);