-
Notifications
You must be signed in to change notification settings - Fork 92
Update plugin VSCode 最近项目 v0.1.2 #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,21 +7,56 @@ const child_process_1 = require("child_process"); | |
| * Windows 下 PATH 中的 code 是 code.cmd,所以必须 shell: true。 | ||
| */ | ||
| function openInVSCode(item) { | ||
| const args = item.kind === 'remote' | ||
| ? ['--folder-uri', item.rawPath] | ||
| : [quoteIfNeeded(item.rawPath)]; | ||
| return new Promise(resolve => { | ||
| let settled = false; | ||
| const settle = (r) => { if (!settled) { | ||
| settled = true; | ||
| resolve(r); | ||
| } }; | ||
| try { | ||
| const child = (0, child_process_1.spawn)('code', args, { detached: true, stdio: 'ignore', shell: true }); | ||
| const isWin = (typeof ztools !== 'undefined' && typeof ztools.isWindows === 'function') | ||
| ? ztools.isWindows() | ||
| : process.platform === 'win32'; | ||
| const isMac = (typeof ztools !== 'undefined' && typeof ztools.isMacOs === 'function') | ||
| ? ztools.isMacOs() | ||
| : (typeof ztools !== 'undefined' && typeof ztools.isMacOS === 'function') | ||
| ? ztools.isMacOS() | ||
| : process.platform === 'darwin'; | ||
| let cmd; | ||
| let finalArgs; | ||
| let useShell; | ||
| const env = { ...process.env }; | ||
| if (isMac) { | ||
| // macOS 平台最佳实践:直接无视环境路径,利用 Bundle ID 原生唤醒应用 | ||
| cmd = 'open'; | ||
| useShell = false; | ||
| finalArgs = item.kind === 'remote' | ||
| ? ['-b', 'com.microsoft.VSCode', '--args', '--folder-uri', item.rawPath] | ||
| : ['-b', 'com.microsoft.VSCode', item.rawPath]; | ||
| } | ||
| else { | ||
| // Windows / Linux 依赖 code 命令唤起 | ||
| cmd = 'code'; | ||
| useShell = isWin; // Windows 下开启 shell 兼容 code.cmd | ||
| finalArgs = item.kind === 'remote' | ||
| ? ['--folder-uri', useShell ? quoteIfNeeded(item.rawPath) : item.rawPath] | ||
| : [useShell ? quoteIfNeeded(item.rawPath) : item.rawPath]; | ||
| if (!isWin) { | ||
| // 为 Linux 等环境尽力补全常规 PATH | ||
| const extraPaths = ['/usr/local/bin', '/usr/bin', '/bin']; | ||
| env.PATH = env.PATH ? `${extraPaths.join(':')}:${env.PATH}` : extraPaths.join(':'); | ||
| } | ||
| } | ||
| const child = (0, child_process_1.spawn)(cmd, finalArgs, { env, detached: true, stdio: 'ignore', shell: useShell }); | ||
| child.on('error', e => settle({ ok: false, reason: e.message })); | ||
| child.on('exit', code => { | ||
| if (code !== 0 && code !== null) { | ||
| settle({ ok: false, reason: `进程异常退出(code:${code}),请检查是否已安装或环境变量配置错误` }); | ||
| } | ||
| }); | ||
| child.unref(); | ||
| // 给 spawn 一个 tick 触发同步错误(如 ENOENT),再 resolve ok | ||
| setTimeout(() => settle({ ok: true }), 50); | ||
| // 给 spawn 一定时间触发错误或退出的事件,若 100ms 内没报错则假定成功 | ||
| setTimeout(() => settle({ ok: true }), 100); | ||
|
Comment on lines
51
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议在 JS 编译产物中也同步处理进程正常退出( child.on('error', e => settle({ ok: false, reason: e.message }));
child.on('exit', (code, signal) => {
if (code === 0) {
settle({ ok: true });
} else if (code !== null || signal !== null) {
settle({ ok: false, reason: "进程异常退出(code:" + code + (signal ? ", signal:" + signal : "") + "),请检查是否已安装或环境变量配置错误" });
}
});
child.unref();
// 给 spawn 一定时间触发错误或退出的事件,若 100ms 内没报错则假定成功
timeoutId = setTimeout(() => settle({ ok: true }), 100); |
||
| } | ||
| catch (e) { | ||
| const reason = e instanceof Error ? e.message : String(e); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,66 @@ | ||
| import { spawn } from 'child_process'; | ||
| import type { OpenResult, RecentItem } from '../types'; | ||
|
|
||
| declare const ztools: any; | ||
|
|
||
| /** | ||
| * 用 PATH 上的 `code` 命令打开项目。 | ||
| * Windows 下 PATH 中的 code 是 code.cmd,所以必须 shell: true。 | ||
| */ | ||
| export function openInVSCode(item: RecentItem): Promise<OpenResult> { | ||
| const args = item.kind === 'remote' | ||
| ? ['--folder-uri', item.rawPath] | ||
| : [quoteIfNeeded(item.rawPath)]; | ||
|
|
||
| return new Promise<OpenResult>(resolve => { | ||
| let settled = false; | ||
| const settle = (r: OpenResult) => { if (!settled) { settled = true; resolve(r); } }; | ||
|
Comment on lines
11
to
13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| try { | ||
| const child = spawn('code', args, { detached: true, stdio: 'ignore', shell: true }); | ||
| const isWin = (typeof ztools !== 'undefined' && typeof ztools.isWindows === 'function') | ||
| ? ztools.isWindows() | ||
| : process.platform === 'win32'; | ||
| const isMac = (typeof ztools !== 'undefined' && typeof ztools.isMacOs === 'function') | ||
| ? ztools.isMacOs() | ||
| : (typeof ztools !== 'undefined' && typeof ztools.isMacOS === 'function') | ||
| ? ztools.isMacOS() | ||
| : process.platform === 'darwin'; | ||
|
|
||
| let cmd: string; | ||
| let finalArgs: string[]; | ||
| let useShell: boolean; | ||
| const env = { ...process.env }; | ||
|
|
||
| if (isMac) { | ||
| // macOS 平台最佳实践:直接无视环境路径,利用 Bundle ID 原生唤醒应用 | ||
| cmd = 'open'; | ||
| useShell = false; | ||
| finalArgs = item.kind === 'remote' | ||
| ? ['-b', 'com.microsoft.VSCode', '--args', '--folder-uri', item.rawPath] | ||
| : ['-b', 'com.microsoft.VSCode', item.rawPath]; | ||
| } else { | ||
| // Windows / Linux 依赖 code 命令唤起 | ||
| cmd = 'code'; | ||
| useShell = isWin; // Windows 下开启 shell 兼容 code.cmd | ||
| finalArgs = item.kind === 'remote' | ||
| ? ['--folder-uri', useShell ? quoteIfNeeded(item.rawPath) : item.rawPath] | ||
| : [useShell ? quoteIfNeeded(item.rawPath) : item.rawPath]; | ||
|
|
||
| if (!isWin) { | ||
| // 为 Linux 等环境尽力补全常规 PATH | ||
| const extraPaths = ['/usr/local/bin', '/usr/bin', '/bin']; | ||
| env.PATH = env.PATH ? `${extraPaths.join(':')}:${env.PATH}` : extraPaths.join(':'); | ||
| } | ||
| } | ||
|
|
||
| const child = spawn(cmd, finalArgs, { env, detached: true, stdio: 'ignore', shell: useShell }); | ||
|
|
||
| child.on('error', e => settle({ ok: false, reason: e.message })); | ||
| child.on('exit', code => { | ||
| if (code !== 0 && code !== null) { | ||
| settle({ ok: false, reason: `进程异常退出(code:${code}),请检查是否已安装或环境变量配置错误` }); | ||
| } | ||
| }); | ||
| child.unref(); | ||
| // 给 spawn 一个 tick 触发同步错误(如 ENOENT),再 resolve ok | ||
| setTimeout(() => settle({ ok: true }), 50); | ||
|
|
||
| // 给 spawn 一定时间触发错误或退出的事件,若 100ms 内没报错则假定成功 | ||
| setTimeout(() => settle({ ok: true }), 100); | ||
|
Comment on lines
54
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议在进程正常退出( child.on('error', e => settle({ ok: false, reason: e.message }));
child.on('exit', (code, signal) => {
if (code === 0) {
settle({ ok: true });
} else if (code !== null || signal !== null) {
settle({ ok: false, reason: "进程异常退出(code:" + code + (signal ? ", signal:" + signal : "") + "),请检查是否已安装或环境变量配置错误" });
}
});
child.unref();
// 给 spawn 一定时间触发错误或退出的事件,若 100ms 内没报错则假定成功
timeoutId = setTimeout(() => settle({ ok: true }), 100); |
||
| } catch (e: unknown) { | ||
| const reason = e instanceof Error ? e.message : String(e); | ||
| settle({ ok: false, reason }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了与 TypeScript 源码保持一致,建议在 JS 编译产物中也引入
timeoutId并在settle中清除定时器。