Compare commits
8 Commits
main
...
00779b503e
| Author | SHA1 | Date | |
|---|---|---|---|
| 00779b503e | |||
| d67cb8b6a0 | |||
| bc954a8202 | |||
| 2f4d2cb27a | |||
| b33ca163a1 | |||
| 3db04f2f0d | |||
| 3b4c3f15f0 | |||
| 314543dcec |
@@ -13,15 +13,12 @@
|
|||||||
"warn",
|
"warn",
|
||||||
{
|
{
|
||||||
"selector": "import",
|
"selector": "import",
|
||||||
"format": [
|
"format": [ "camelCase", "PascalCase" ]
|
||||||
"camelCase",
|
|
||||||
"PascalCase"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"@typescript-eslint/semi": "off",
|
"@typescript-eslint/semi": "warn",
|
||||||
"curly": "warn",
|
"curly": "warn",
|
||||||
"eqeqeq": "off",
|
"eqeqeq": "warn",
|
||||||
"no-throw-literal": "warn",
|
"no-throw-literal": "warn",
|
||||||
"semi": "off"
|
"semi": "off"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ jobs:
|
|||||||
- name: 拉取代码
|
- name: 拉取代码
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
ref: ${{ github.head_ref }}
|
|
||||||
|
|
||||||
- name: 配置 Git
|
- name: 配置 Git
|
||||||
run: |
|
run: |
|
||||||
@@ -33,51 +33,32 @@ jobs:
|
|||||||
git config --global --add safe.directory /github/workspace
|
git config --global --add safe.directory /github/workspace
|
||||||
|
|
||||||
# 配置用户信息
|
# 配置用户信息
|
||||||
git config --global user.name "devstar"
|
git config --global user.name "github-actions[bot]"
|
||||||
git config --global user.email "devstar@noreply.devstar.cn"
|
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
|
|
||||||
- name: Check and bump version
|
- name: 自动递增版本号
|
||||||
if: github.event_name == 'pull_request'
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||||
run: |
|
run: |
|
||||||
apk add --no-cache jq
|
apk add --no-cache jq
|
||||||
# 获取远程 main 分支版本
|
CURRENT_VERSION=$(jq -r '.version' package.json)
|
||||||
git fetch origin main
|
echo "当前版本: $CURRENT_VERSION"
|
||||||
MAIN_VERSION=$(git show origin/main:package.json | jq -r '.version')
|
|
||||||
PR_VERSION=$(jq -r '.version' package.json)
|
|
||||||
|
|
||||||
echo "Main 分支版本: $MAIN_VERSION"
|
# 分解版本号
|
||||||
echo "当前 PR 版本: $PR_VERSION"
|
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
|
||||||
|
|
||||||
# 如果版本号相同,则递增
|
# 递增补丁版本号
|
||||||
if [ "$MAIN_VERSION" = "$PR_VERSION" ]; then
|
NEW_PATCH=$((PATCH + 1))
|
||||||
echo "版本号未变更,开始递增..."
|
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
|
||||||
|
echo "新版本: $NEW_VERSION"
|
||||||
|
|
||||||
# 分解版本号
|
# 更新 package.json
|
||||||
MAJOR=$(echo "$PR_VERSION" | cut -d'.' -f1)
|
jq --arg version "$NEW_VERSION" '.version = $version' package.json > package.json.tmp
|
||||||
MINOR=$(echo "$PR_VERSION" | cut -d'.' -f2)
|
mv package.json.tmp package.json
|
||||||
PATCH=$(echo "$PR_VERSION" | cut -d'.' -f3)
|
|
||||||
|
|
||||||
# 递增补丁版本号
|
# 提交版本变更
|
||||||
NEW_PATCH=$((PATCH + 1))
|
git add package.json
|
||||||
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
|
git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
|
||||||
echo "新版本: $NEW_VERSION"
|
git push
|
||||||
|
|
||||||
# 更新 package.json
|
|
||||||
jq --arg version "$NEW_VERSION" '.version = $version' package.json > package.json.tmp
|
|
||||||
mv package.json.tmp package.json
|
|
||||||
|
|
||||||
# 提交版本变更
|
|
||||||
git config user.name "devstar-actions"
|
|
||||||
git config user.email "devstar-actions@devstar.cn"
|
|
||||||
git add package.json
|
|
||||||
git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
|
|
||||||
|
|
||||||
# 推送到当前分支
|
|
||||||
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
|
|
||||||
git push origin $BRANCH_NAME || git push origin HEAD:refs/heads/$BRANCH_NAME
|
|
||||||
else
|
|
||||||
echo "版本号已更新,跳过递增"
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: 安装依赖
|
- name: 安装依赖
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "devstar",
|
"name": "devstar",
|
||||||
"displayName": "%displayName%",
|
"displayName": "%displayName%",
|
||||||
"description": "%description%",
|
"description": "%description%",
|
||||||
"version": "0.5.3",
|
"version": "0.4.3",
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"publisher": "mengning",
|
"publisher": "mengning",
|
||||||
"engines": {
|
"engines": {
|
||||||
@@ -42,10 +42,6 @@
|
|||||||
{
|
{
|
||||||
"protocol": "vscode",
|
"protocol": "vscode",
|
||||||
"path": "/openProject"
|
"path": "/openProject"
|
||||||
},
|
|
||||||
{
|
|
||||||
"protocol": "trae",
|
|
||||||
"path": "/openProject"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"views": {
|
"views": {
|
||||||
|
|||||||
@@ -25,46 +25,6 @@ export default class RemoteContainer {
|
|||||||
this.user = user;
|
this.user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建打开项目的命令
|
|
||||||
* 根据终端类型生成对应的命令
|
|
||||||
* 支持 Windows PowerShell/CMD 和 Linux/macOS Bash/Zsh
|
|
||||||
*/
|
|
||||||
private buildOpenProjectCommand(
|
|
||||||
host: string,
|
|
||||||
hostname: string,
|
|
||||||
port: number,
|
|
||||||
username: string,
|
|
||||||
path: string,
|
|
||||||
devstarDomain: string | undefined,
|
|
||||||
terminal: vscode.Terminal
|
|
||||||
): string {
|
|
||||||
const baseUrl = `vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}`;
|
|
||||||
const url = devstarDomain ? `${baseUrl}&devstar_domain=${devstarDomain}` : baseUrl;
|
|
||||||
|
|
||||||
// 获取本地操作系统类型
|
|
||||||
// 在远程环境下,os.platform() 返回的是远程系统,但这里我们需要的是本地系统
|
|
||||||
// 所以通过终端的 shell 路径来推断
|
|
||||||
const shellPath = (terminal.creationOptions as any)?.shellPath || '';
|
|
||||||
console.log('终端 shell 路径:', shellPath);
|
|
||||||
|
|
||||||
// 检测 Windows(Windows shell 路径包含反斜杠或特定关键字)
|
|
||||||
const isWindows = shellPath.includes('\\') ||
|
|
||||||
shellPath.toLowerCase().includes('powershell') ||
|
|
||||||
shellPath.toLowerCase().includes('pwsh') ||
|
|
||||||
shellPath.toLowerCase().includes('cmd');
|
|
||||||
|
|
||||||
if (isWindows) {
|
|
||||||
// Windows PowerShell/CMD: 使用分号分隔,URL 需要特殊的引号处理
|
|
||||||
console.log('检测到 Windows 本地系统,使用 PowerShell/CMD 语法');
|
|
||||||
return `code --new-window; code --open-url '"${url}"'`;
|
|
||||||
} else {
|
|
||||||
// macOS/Linux: 使用 && 分隔,标准双引号
|
|
||||||
console.log('检测到 Unix 本地系统(macOS/Linux),使用 Bash/Zsh 语法');
|
|
||||||
return `code --new-window && code --open-url "${url}"`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 第一次打开远程项目
|
* 第一次打开远程项目
|
||||||
*/
|
*/
|
||||||
@@ -80,8 +40,29 @@ export default class RemoteContainer {
|
|||||||
devstarDomain = undefined;
|
devstarDomain = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据终端类型生成对应的命令
|
const semver = require('semver');
|
||||||
const command = this.buildOpenProjectCommand(host, hostname, port, username, path, devstarDomain, terminal);
|
const powershellVersion = context.globalState.get('powershellVersion');
|
||||||
|
const powershell_semver_compatible_version = semver.coerce(powershellVersion);
|
||||||
|
|
||||||
|
let command = '';
|
||||||
|
if (devstarDomain === undefined) {
|
||||||
|
if (powershellVersion === undefined) {
|
||||||
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||||
|
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
||||||
|
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||||
|
} else {
|
||||||
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (powershellVersion === undefined) {
|
||||||
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||||
|
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
||||||
|
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||||
|
} else {
|
||||||
|
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
terminal.sendText(command);
|
terminal.sendText(command);
|
||||||
} else {
|
} else {
|
||||||
vscode.window.showErrorMessage('无法创建终端,请检查终端是否可用。');
|
vscode.window.showErrorMessage('无法创建终端,请检查终端是否可用。');
|
||||||
@@ -295,6 +276,18 @@ export default class RemoteContainer {
|
|||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 由于使用了 stdio: 'ignore',这些事件监听器不再需要
|
||||||
|
// sshProcess.stdout.on('data', (data: Buffer) => {
|
||||||
|
// console.log(`[SSH stdout] ${data.toString()}`);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// sshProcess.stderr.on('data', (data: Buffer) => {
|
||||||
|
// console.error(`[SSH stderr] ${data.toString()}`);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// 注意:由于进程已 detached 和 unref,不再需要保存到 sshProcesses Map
|
||||||
|
// 因为我们无法也不需要控制这些独立进程的生命周期
|
||||||
|
|
||||||
// 等待 SSH 连接建立
|
// 等待 SSH 连接建立
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
resolve();
|
resolve();
|
||||||
@@ -365,7 +358,7 @@ export default class RemoteContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* local env - 使用 Extension API 打开远程文件夹
|
* local env
|
||||||
*/
|
*/
|
||||||
async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext): Promise<void> {
|
async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -379,36 +372,14 @@ export default class RemoteContainer {
|
|||||||
} catch (portError) {
|
} catch (portError) {
|
||||||
vscode.window.showWarningMessage('端口映射设置失败,但容器连接已建立');
|
vscode.window.showWarningMessage('端口映射设置失败,但容器连接已建立');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自动配置本地 VSCode 的 MCP Server
|
|
||||||
try {
|
|
||||||
await this.configureMCPServerLocally();
|
|
||||||
} catch (mcpError) {
|
|
||||||
console.error('[MCP] 本地 MCP 配置失败:', mcpError);
|
|
||||||
// 不阻塞主流程,仅记录错误
|
|
||||||
}
|
|
||||||
|
|
||||||
// 自动配置容器内 AI IDE 的 MCP Server
|
|
||||||
try {
|
|
||||||
await this.configureMCPServerInContainer(sshConfig.hostname, port);
|
|
||||||
} catch (mcpError) {
|
|
||||||
console.error('[MCP] 容器内 MCP 配置失败:', mcpError);
|
|
||||||
// 不阻塞主流程,仅记录错误
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 VSCode Extension API 打开远程连接
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
||||||
// 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效
|
terminal.show(true);
|
||||||
// 格式: vscode-remote://ssh-remote+host/path
|
|
||||||
// 注意:不在 URI 中指定用户名,让 SSH config 中的 User root 配置生效
|
|
||||||
const remoteUri = vscode.Uri.parse(
|
|
||||||
`vscode-remote://ssh-remote+${host}${path}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// 尝试使用 vscode.openFolder 命令
|
const command = `code --remote ssh-remote+root@${host}:${port} ${path} --reuse-window`;
|
||||||
await vscode.commands.executeCommand('vscode.openFolder', remoteUri, {
|
|
||||||
forceNewWindow: false
|
terminal.sendText(command);
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
||||||
@@ -494,251 +465,18 @@ export default class RemoteContainer {
|
|||||||
console.log('未找到 forwardPorts 参数,跳过端口映射设置。');
|
console.log('未找到 forwardPorts 参数,跳过端口映射设置。');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 在容器内配置 AI IDE 的 MCP Server
|
|
||||||
* 支持 Trae IDE 等 AI IDE 的 MCP 自动配置
|
|
||||||
*/
|
|
||||||
private async configureMCPServerInContainer(hostname: string, sshPort: number): Promise<void> {
|
|
||||||
// 获取用户 token
|
|
||||||
const userToken = this.user.getUserTokenFromLocal();
|
|
||||||
if (!userToken) {
|
|
||||||
console.log('[MCP] 用户未登录,跳过 MCP Server 配置');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取 DevStar 域名
|
|
||||||
const devstarDomain = this.user.getDevstarDomain();
|
|
||||||
const mcpUrl = `${devstarDomain}/api/mcp`;
|
|
||||||
|
|
||||||
console.log(`[MCP] 开始配置 MCP Server: ${mcpUrl}`);
|
|
||||||
|
|
||||||
// Trae IDE 的 MCP 配置路径
|
|
||||||
const traeMcpPath = '/root/.trae-server/data/Machine/mcp.json';
|
|
||||||
|
|
||||||
// MCP 配置内容(注意:使用 "mcpServers" 格式)
|
|
||||||
const mcpConfig = {
|
|
||||||
mcpServers: {
|
|
||||||
devstar: {
|
|
||||||
type: 'http',
|
|
||||||
url: mcpUrl,
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${userToken}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const configJson = JSON.stringify(mcpConfig, null, 2);
|
|
||||||
|
|
||||||
// 使用 SSH 连接到容器并配置
|
|
||||||
const ssh = new NodeSSH();
|
|
||||||
|
|
||||||
try {
|
|
||||||
await ssh.connect({
|
|
||||||
host: hostname,
|
|
||||||
username: 'root',
|
|
||||||
port: sshPort,
|
|
||||||
privateKeyPath: this.user.getUserPrivateKeyPath(),
|
|
||||||
readyTimeout: 10000,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('[MCP] SSH 连接成功');
|
|
||||||
|
|
||||||
// 检查现有配置
|
|
||||||
const checkScript = `
|
|
||||||
if [ -f "${traeMcpPath}" ]; then
|
|
||||||
cat "${traeMcpPath}"
|
|
||||||
else
|
|
||||||
echo "FILE_NOT_EXISTS"
|
|
||||||
fi
|
|
||||||
`;
|
|
||||||
|
|
||||||
const checkResult = await ssh.execCommand(checkScript);
|
|
||||||
|
|
||||||
let needUpdate = true;
|
|
||||||
|
|
||||||
if (checkResult.stdout !== 'FILE_NOT_EXISTS') {
|
|
||||||
try {
|
|
||||||
const existingConfig = JSON.parse(checkResult.stdout);
|
|
||||||
const existingDevstar = existingConfig.mcpServers?.devstar;
|
|
||||||
|
|
||||||
if (existingDevstar) {
|
|
||||||
// 检查 URL 和 token 是否匹配
|
|
||||||
const urlMatch = existingDevstar.url === mcpUrl;
|
|
||||||
const tokenMatch = existingDevstar.headers?.Authorization === `Bearer ${userToken}`;
|
|
||||||
|
|
||||||
if (urlMatch && tokenMatch) {
|
|
||||||
console.log('[MCP] DevStar MCP 配置已存在且正确,无需更新');
|
|
||||||
needUpdate = false;
|
|
||||||
} else {
|
|
||||||
console.log(`[MCP] DevStar MCP 配置需要更新 (URL匹配: ${urlMatch}, Token匹配: ${tokenMatch})`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (parseError) {
|
|
||||||
console.log('[MCP] 解析现有配置失败,将创建新配置');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('[MCP] 容器内无配置文件,需要创建');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建或更新配置文件
|
|
||||||
if (needUpdate) {
|
|
||||||
const setupScript = `
|
|
||||||
mkdir -p /root/.trae-server/data/Machine && \
|
|
||||||
cat > ${traeMcpPath} << 'EOF'
|
|
||||||
${configJson}
|
|
||||||
EOF
|
|
||||||
echo "MCP 配置已更新"
|
|
||||||
`;
|
|
||||||
|
|
||||||
const result = await ssh.execCommand(setupScript);
|
|
||||||
|
|
||||||
if (result.code === 0) {
|
|
||||||
console.log('[MCP] DevStar MCP 配置成功');
|
|
||||||
} else {
|
|
||||||
console.error(`[MCP] 配置失败: ${result.stderr}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('[MCP] SSH 连接或配置失败:', error);
|
|
||||||
throw error;
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
await ssh.dispose();
|
|
||||||
} catch (e) {
|
|
||||||
// ignore dispose error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在本地 VSCode 中配置 MCP Server
|
|
||||||
* 支持 GitHub Copilot 等使用本地 MCP 配置
|
|
||||||
*/
|
|
||||||
private async configureMCPServerLocally(): Promise<void> {
|
|
||||||
// 获取用户 token
|
|
||||||
const userToken = this.user.getUserTokenFromLocal();
|
|
||||||
if (!userToken) {
|
|
||||||
console.log('[MCP] 用户未登录,跳过本地 MCP Server 配置');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取 DevStar 域名
|
|
||||||
const devstarDomain = this.user.getDevstarDomain();
|
|
||||||
const mcpUrl = `${devstarDomain}/api/mcp`;
|
|
||||||
|
|
||||||
console.log(`[MCP] 开始配置本地 MCP Server: ${mcpUrl}`);
|
|
||||||
|
|
||||||
// 根据操作系统确定配置文件路径
|
|
||||||
let mcpConfigPath: string;
|
|
||||||
const platform = os.platform();
|
|
||||||
const homedir = os.homedir();
|
|
||||||
|
|
||||||
if (platform === 'darwin') {
|
|
||||||
// macOS
|
|
||||||
mcpConfigPath = path.join(homedir, 'Library/Application Support/Code/User/mcp.json');
|
|
||||||
} else if (platform === 'win32') {
|
|
||||||
// Windows
|
|
||||||
mcpConfigPath = path.join(homedir, 'AppData/Roaming/Code/User/mcp.json');
|
|
||||||
} else {
|
|
||||||
// Linux
|
|
||||||
mcpConfigPath = path.join(homedir, '.config/Code/User/mcp.json');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`[MCP] 配置文件路径: ${mcpConfigPath}`);
|
|
||||||
|
|
||||||
// MCP 配置内容(本地 VSCode 使用 "servers" 格式)
|
|
||||||
const mcpConfig = {
|
|
||||||
servers: {
|
|
||||||
devstar: {
|
|
||||||
type: 'http',
|
|
||||||
url: mcpUrl,
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${userToken}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const configJson = JSON.stringify(mcpConfig, null, 2);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 检查现有配置
|
|
||||||
let existingConfig: any = {};
|
|
||||||
let needUpdate = true;
|
|
||||||
|
|
||||||
if (fs.existsSync(mcpConfigPath)) {
|
|
||||||
try {
|
|
||||||
const content = fs.readFileSync(mcpConfigPath, 'utf8');
|
|
||||||
existingConfig = JSON.parse(content);
|
|
||||||
const existingDevstar = existingConfig.servers?.devstar;
|
|
||||||
|
|
||||||
if (existingDevstar) {
|
|
||||||
// 检查 URL 和 token 是否匹配
|
|
||||||
const urlMatch = existingDevstar.url === mcpUrl;
|
|
||||||
const tokenMatch = existingDevstar.headers?.Authorization === `Bearer ${userToken}`;
|
|
||||||
|
|
||||||
if (urlMatch && tokenMatch) {
|
|
||||||
console.log('[MCP] 本地 DevStar MCP 配置已存在且正确,无需更新');
|
|
||||||
needUpdate = false;
|
|
||||||
} else {
|
|
||||||
console.log(`[MCP] 本地 DevStar MCP 配置需要更新 (URL匹配: ${urlMatch}, Token匹配: ${tokenMatch})`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (parseError) {
|
|
||||||
console.log('[MCP] 解析现有配置失败,将创建新配置');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('[MCP] 本地无配置文件,需要创建');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建或更新配置文件
|
|
||||||
if (needUpdate) {
|
|
||||||
// 确保目录存在
|
|
||||||
const configDir = path.dirname(mcpConfigPath);
|
|
||||||
if (!fs.existsSync(configDir)) {
|
|
||||||
fs.mkdirSync(configDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 合并现有配置(保留其他 MCP servers)
|
|
||||||
const newConfig = {
|
|
||||||
...existingConfig,
|
|
||||||
servers: {
|
|
||||||
...existingConfig.servers,
|
|
||||||
...mcpConfig.servers
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fs.writeFileSync(mcpConfigPath, JSON.stringify(newConfig, null, 2), 'utf8');
|
|
||||||
console.log('[MCP] 本地 DevStar MCP 配置成功');
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('[MCP] 本地 MCP 配置失败:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打开项目(无须插件登录)- 使用 Extension API
|
* 打开项目(无须插件登录)
|
||||||
*/
|
*/
|
||||||
export async function openProjectWithoutLogging(host: string, _port: number, _username: string, path: string): Promise<void> {
|
export async function openProjectWithoutLogging(hostname: string, port: number, username: string, path: string): Promise<void> {
|
||||||
|
const command = `code --remote ssh-remote+${username}@${hostname}:${port} ${path} --reuse-window`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 使用 VSCode Extension API 打开远程连接
|
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
||||||
// 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效
|
terminal.show(true);
|
||||||
// 格式: vscode-remote://ssh-remote+host/path
|
terminal.sendText(command);
|
||||||
const remoteUri = vscode.Uri.parse(
|
|
||||||
`vscode-remote://ssh-remote+${host}${path}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// 使用 vscode.openFolder 命令
|
|
||||||
await vscode.commands.executeCommand('vscode.openFolder', remoteUri, {
|
|
||||||
forceNewWindow: false
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
||||||
vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);
|
vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);
|
||||||
|
|||||||
35
src/utils.ts
35
src/utils.ts
@@ -1,7 +1,12 @@
|
|||||||
|
import * as http from 'http';
|
||||||
|
import * as https from 'https';
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
|
|
||||||
|
const {
|
||||||
|
generateKeyPairSync,
|
||||||
|
} = require('node:crypto')
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
|
|
||||||
@@ -18,6 +23,36 @@ export function isMacOS(): boolean {
|
|||||||
return os.platform() === 'darwin';
|
return os.platform() === 'darwin';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function fetch(url: string): Promise<string> {
|
||||||
|
// determine the library to use (based on the url protocol)
|
||||||
|
const lib = url.startsWith('https://') ? https : http;
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
lib.get(url, (response) => {
|
||||||
|
// make sure the status code is 200
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = '';
|
||||||
|
response.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
response.on('end', () => {
|
||||||
|
resolve(data);
|
||||||
|
});
|
||||||
|
}).on('error', (err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Sleep = (ms: number) => {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms))
|
||||||
|
}
|
||||||
|
|
||||||
export async function getVsCodeCommitId(): Promise<string> {
|
export async function getVsCodeCommitId(): Promise<string> {
|
||||||
if (isLinux() || isMacOS()) {
|
if (isLinux() || isMacOS()) {
|
||||||
return new Promise<string>((resolve) => {
|
return new Promise<string>((resolve) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user