fix terminal
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
import http from 'node:http';
|
import http from 'node:http';
|
||||||
import { existsSync } from 'node:fs';
|
import { existsSync } from 'node:fs';
|
||||||
import { cp, mkdir } from 'node:fs/promises';
|
import { cp, mkdir } from 'node:fs/promises';
|
||||||
import { join } from 'node:path';
|
import { join, dirname } from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
import { WebSocketServer } from 'ws';
|
import { WebSocketServer } from 'ws';
|
||||||
import * as pty from 'node-pty';
|
import * as pty from 'node-pty';
|
||||||
|
|
||||||
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||||
|
const isDocker = existsSync('/opt/terminal-templates/.zshrc');
|
||||||
|
|
||||||
|
const templateDir = isDocker ? '/opt/terminal-templates' : join(__dirname, 'templates');
|
||||||
|
const ohMyZshSource = isDocker ? '/opt/oh-my-zsh' : null;
|
||||||
|
|
||||||
const port = Number(process.env.TERMINAL_PTY_PORT ?? '5337');
|
const port = Number(process.env.TERMINAL_PTY_PORT ?? '5337');
|
||||||
const host = process.env.TERMINAL_PTY_HOST ?? '127.0.0.1';
|
const host = process.env.TERMINAL_PTY_HOST ?? '127.0.0.1';
|
||||||
|
|
||||||
@@ -26,20 +33,45 @@ const sendJson = (ws, msg) => {
|
|||||||
const ensureUserFiles = async (homeDir) => {
|
const ensureUserFiles = async (homeDir) => {
|
||||||
await mkdir(homeDir, { recursive: true });
|
await mkdir(homeDir, { recursive: true });
|
||||||
await mkdir(join(homeDir, '.config'), { recursive: true });
|
await mkdir(join(homeDir, '.config'), { recursive: true });
|
||||||
|
await mkdir(join(homeDir, '.local', 'bin'), { recursive: true });
|
||||||
|
|
||||||
const zshrcPath = join(homeDir, '.zshrc');
|
const zshrcPath = join(homeDir, '.zshrc');
|
||||||
if (!existsSync(zshrcPath)) {
|
if (!existsSync(zshrcPath)) {
|
||||||
await cp('/opt/terminal-templates/.zshrc', zshrcPath);
|
await cp(join(templateDir, '.zshrc'), zshrcPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
const starshipPath = join(homeDir, '.config', 'starship-officer.toml');
|
const starshipPath = join(homeDir, '.config', 'starship-officer.toml');
|
||||||
if (!existsSync(starshipPath)) {
|
if (!existsSync(starshipPath)) {
|
||||||
await cp('/opt/terminal-templates/starship-officer.toml', starshipPath);
|
await cp(join(templateDir, 'starship-officer.toml'), starshipPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ohMyZshPath = join(homeDir, '.oh-my-zsh');
|
const ohMyZshPath = join(homeDir, '.oh-my-zsh');
|
||||||
if (!existsSync(ohMyZshPath)) {
|
if (!existsSync(ohMyZshPath)) {
|
||||||
await cp('/opt/oh-my-zsh', ohMyZshPath, { recursive: true });
|
if (ohMyZshSource && existsSync(ohMyZshSource)) {
|
||||||
|
await cp(ohMyZshSource, ohMyZshPath, { recursive: true });
|
||||||
|
} else {
|
||||||
|
// Clone oh-my-zsh on first run (host mode)
|
||||||
|
const proc = Bun.spawn({
|
||||||
|
cmd: ['git', 'clone', '--depth=1', 'https://github.com/ohmyzsh/ohmyzsh.git', ohMyZshPath],
|
||||||
|
stdout: 'ignore',
|
||||||
|
stderr: 'ignore',
|
||||||
|
});
|
||||||
|
await proc.exited;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Install starship on host if missing (not needed in Docker)
|
||||||
|
if (!isDocker) {
|
||||||
|
const starshipBin = join(homeDir, '.local', 'bin', 'starship');
|
||||||
|
if (!existsSync(starshipBin)) {
|
||||||
|
const installProc = Bun.spawn({
|
||||||
|
cmd: ['sh', '-c', 'curl -sS https://starship.rs/install.sh | sh -s -- -y -b "$HOME/.local/bin"'],
|
||||||
|
env: { ...process.env, HOME: homeDir },
|
||||||
|
stdout: 'ignore',
|
||||||
|
stderr: 'ignore',
|
||||||
|
});
|
||||||
|
await installProc.exited;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,17 @@ source $ZSH/oh-my-zsh.sh
|
|||||||
if [[ -n "$OFFICER_TERMINAL_USER" ]]; then
|
if [[ -n "$OFFICER_TERMINAL_USER" ]]; then
|
||||||
export STARSHIP_CONFIG="$HOME/.config/starship-officer.toml"
|
export STARSHIP_CONFIG="$HOME/.config/starship-officer.toml"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Auto-install starship if not available (host mode)
|
||||||
|
if ! command -v starship &> /dev/null; then
|
||||||
|
if [[ ! -x "$HOME/.local/bin/starship" ]]; then
|
||||||
|
mkdir -p "$HOME/.local/bin"
|
||||||
|
echo "Installing starship..."
|
||||||
|
curl -sS https://starship.rs/install.sh | sh -s -- -y -b "$HOME/.local/bin" 2>/dev/null
|
||||||
|
fi
|
||||||
|
export PATH="$HOME/.local/bin:$PATH"
|
||||||
|
fi
|
||||||
|
|
||||||
eval "$(starship init zsh)"
|
eval "$(starship init zsh)"
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user