MCPcopy Create free account
hub / github.com/6174/comflowyspace / runCommandWithPty

Function runCommandWithPty

apps/node/src/modules/utils/run-command.ts:82–159  ·  view source on GitHub ↗
(
    command: string,
    dispatcher: TaskEventDispatcher = () => { },
    options: Options = {},
    cb?: (process: nodePty.IPty) => void
)

Source from the content-addressed store, hash-verified

80const appDir = getAppDataDir();
81
82export async function runCommandWithPty(
83 command: string,
84 dispatcher: TaskEventDispatcher = () => { },
85 options: Options = {},
86 cb?: (process: nodePty.IPty) => void
87) {
88 const { systemProxy, systemProxyString } = await getSystemProxy();
89 logger.info("run command with PTY");
90 const fullCommand = `${command} && echo END_OF_COMMAND\n`;
91
92 return new Promise((resolve, reject) => {
93 try {
94 const pty = nodePty.spawn(shell, [], {
95 name: 'xterm-color',
96 // conpty will cause Error: ptyProcess.kill() will throw a error that can't be catched
97 useConpty: false,
98 cols: 80,
99 rows: 30,
100 env: {
101 ...process.env,
102 ...systemProxy,
103 Path: process.env.PATH,
104 DISABLE_UPDATE_PROMPT: "true",
105 encoding: 'utf-8',
106 },
107 cwd: (options.cwd || appDir) as string
108 });
109
110 cb && cb(pty);
111
112 let buffer = "";
113 const disposable = pty.onData(function (data: string) {
114 // raw output data
115 dispatcher && dispatcher({
116 message: data,
117 data: {
118 raw: true
119 }
120 });
121
122 if (data.trim() === fullCommand.trim()) {
123 return;
124 }
125
126 // wrapped output data
127 buffer += data;
128 if (data.indexOf('\n') > 0) {
129 logger.info("[Log:" + buffer + "]");
130 dispatcher && dispatcher({
131 message: buffer.replace("&& echo END_OF_COMMAND", "").replace("END_OF_COMMAND", ""),
132 data: {
133 raw: false
134 }
135 });
136 buffer = ""
137 }
138 if (data.trim() === 'END_OF_COMMAND') {
139 logger.info('The command has finished executing.');

Callers

nothing calls this directly

Calls 4

getSystemProxyFunction · 0.90
cbFunction · 0.85
disposeMethod · 0.65
writeMethod · 0.45

Tested by

no test coverage detected