File size: 755 Bytes
5858652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { spawn } from "node:child_process";
import os from "node:os";

function getShellExecutable() {
  if (process.platform === "win32") {
    return process.env.COMSPEC || "powershell.exe";
  }

  if (process.env.SHELL) {
    return process.env.SHELL;
  }

  return "/bin/bash";
}

export function createShellSession(cwd) {
  const shell = getShellExecutable();
  const child = spawn(shell, [], {
    cwd,
    env: {
      ...process.env,
      TERM: process.env.TERM || "xterm-256color"
    },
    stdio: ["pipe", "pipe", "pipe"]
  });

  return {
    os: os.platform(),
    shell,
    process: child,
    write(data) {
      child.stdin.write(data);
    },
    close() {
      if (!child.killed) {
        child.kill("SIGTERM");
      }
    }
  };
}