Skip to content

Commit 62a5f94

Browse files
committed
查找全部子进程todo
1 parent c4fb1b2 commit 62a5f94

4 files changed

Lines changed: 50 additions & 24 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# node-pty-prebuilt
2-
spawn函数的opt选项添加了agentExePath,可以指定 agentExePath的位置.
2+
在windwos下spawn函数的opt选项添加了exePath,可以指定 exePath的位置.
33

44
The spawn function's opt option has been added with agentExePath, which allows specifying the location of the agent executable.
55

66
https://github.com/microsoft/node-pty
7+
8+
1. 在windows下useConpty如果开启,不能在debug模式下运行,useConptyDll如果开启了,你依然可以用exePath参数来指定它所需要的dll位置,不提供会使用项目默认的。
9+
2. 已经适配webpack打包

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"author": {
55
"name": "xiaobaidadada"
66
},
7-
"version": "1.0.2",
7+
"version": "1.0.3",
88
"license": "MIT",
99
"main": "./lib/index.js",
1010
"types": "./typings/node-pty.d.ts",

src/win/conpty_console_list.cc

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,44 @@
66
#include <napi.h>
77
#include <windows.h>
88

9-
static Napi::Value ApiConsoleProcessList(const Napi::CallbackInfo& info) {
9+
static Napi::Promise ApiConsoleProcessList(const Napi::CallbackInfo& info) {
1010
Napi::Env env(info.Env());
1111
if (info.Length() != 1 ||
1212
!info[0].IsNumber()) {
1313
throw Napi::Error::New(env, "Usage: getConsoleProcessList(shellPid)");
1414
}
1515

1616
const DWORD pid = info[0].As<Napi::Number>().Uint32Value();
17-
17+
// 创建一个 Promise 对象
18+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
1819
if (!FreeConsole()) {
19-
throw Napi::Error::New(env, "FreeConsole failed");
20-
}
21-
if (!AttachConsole(pid)) {
22-
throw Napi::Error::New(env, "AttachConsole failed");
20+
deferred.Reject(Napi::String::New(deferred.Env(),"FreeConsole failed"));
2321
}
24-
auto processList = std::vector<DWORD>(64);
25-
auto processCount = GetConsoleProcessList(&processList[0], static_cast<DWORD>(processList.size()));
26-
if (processList.size() < processCount) {
27-
processList.resize(processCount);
28-
processCount = GetConsoleProcessList(&processList[0], static_cast<DWORD>(processList.size()));
22+
if (!AttachConsole(pid)) { // todo 这都是操当前的,不太行
23+
deferred.Reject(Napi::String::New(deferred.Env(),"AttachConsole failed"));
2924
}
30-
FreeConsole();
25+
std::thread([pid,deferred]() mutable {
26+
// 执行耗时任务
27+
try {
3128

32-
Napi::Array result = Napi::Array::New(env);
33-
for (DWORD i = 0; i < processCount; i++) {
34-
result.Set(i, Napi::Number::New(env, processList[i]));
35-
}
36-
return result;
29+
30+
auto processList = std::vector<DWORD>(64);
31+
auto processCount = GetConsoleProcessList(&processList[0], static_cast<DWORD>(processList.size()));
32+
if (processList.size() < processCount) {
33+
processList.resize(processCount);
34+
processCount = GetConsoleProcessList(&processList[0], static_cast<DWORD>(processList.size()));
35+
}
36+
FreeConsole();
37+
Napi::Array result = Napi::Array::New(deferred.Env());
38+
for (DWORD i = 0; i < processCount; i++) {
39+
result.Set(i, Napi::Number::New(deferred.Env(), processList[i]));
40+
}
41+
deferred.Resolve(result);
42+
} catch (const std::exception& e) {
43+
deferred.Reject(Napi::String::New(deferred.Env(), e.what()));
44+
}
45+
}).detach(); // 分离线程,这样线程会在后台运行
46+
return deferred.Promise();
3747
}
3848

3949
Napi::Object init(Napi::Env env, Napi::Object exports) {

src/windowsPtyAgent.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,29 @@ export class WindowsPtyAgent {
194194
this._conoutSocketWorker.dispose();
195195
}
196196

197+
getConsoleProcessList: any;
197198
private _getConsoleProcessList(): Promise<number[]> {
199+
try {
200+
if (!this.getConsoleProcessList) {
201+
this.getConsoleProcessList = __non_webpack_require__('../build/Release/conpty_console_list.node').getConsoleProcessList;
202+
}
203+
} catch (err) {
204+
this.getConsoleProcessList = __non_webpack_require__('../build/Debug/conpty_console_list.node').getConsoleProcessList;
205+
}
198206
return new Promise<number[]>(resolve => {
199-
const agent = fork(path.join(__dirname, 'conpty_console_list_agent'), [ this._innerPid.toString() ]);
200-
agent.on('message', message => {
207+
// const agent = fork(path.join(__dirname, 'conpty_console_list_agent'), [ this._innerPid.toString() ]);
208+
// agent.on('message', message => {
209+
// clearTimeout(timeout);
210+
// resolve(message.consoleProcessList);
211+
// });
212+
this.getConsoleProcessList(this._innerPid).then((consoleProcessList: number[] | PromiseLike<number[]> | undefined) => {
201213
clearTimeout(timeout);
202-
resolve(message.consoleProcessList);
214+
resolve(consoleProcessList);
215+
}).catch((error: any) => {
216+
console.error('Error:', error); // 如果发生错误,会打印出来
217+
resolve([ this._innerPid ]);
203218
});
204219
const timeout = setTimeout(() => {
205-
// Something went wrong, just send back the shell PID
206-
agent.kill();
207220
resolve([ this._innerPid ]);
208221
}, 5000);
209222
});

0 commit comments

Comments
 (0)