@pixelbyte-software/pixcode 1.30.2 → 1.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +718 -718
- package/README.de.md +248 -248
- package/README.ja.md +240 -240
- package/README.ko.md +240 -240
- package/README.md +295 -285
- package/README.ru.md +248 -248
- package/README.tr.md +250 -250
- package/README.zh-CN.md +240 -240
- package/dist/api-docs.html +879 -879
- package/dist/assets/index-BtOeB3cE.js +837 -0
- package/dist/assets/index-CDpePeIN.css +32 -0
- package/dist/assets/vendor-codemirror-CzYAOTxS.js +41 -0
- package/dist/clear-cache.html +85 -85
- package/dist/convert-icons.md +52 -52
- package/dist/favicon.png +0 -0
- package/dist/favicon.svg +7 -8
- package/dist/generate-icons.js +48 -48
- package/dist/icons/codex-white.svg +3 -3
- package/dist/icons/codex.svg +3 -3
- package/dist/icons/cursor-white.svg +11 -11
- package/dist/icons/icon-128x128.png +0 -0
- package/dist/icons/icon-128x128.svg +9 -12
- package/dist/icons/icon-144x144.png +0 -0
- package/dist/icons/icon-144x144.svg +9 -12
- package/dist/icons/icon-152x152.png +0 -0
- package/dist/icons/icon-152x152.svg +9 -12
- package/dist/icons/icon-192x192.png +0 -0
- package/dist/icons/icon-192x192.svg +9 -12
- package/dist/icons/icon-384x384.png +0 -0
- package/dist/icons/icon-384x384.svg +9 -12
- package/dist/icons/icon-512x512.png +0 -0
- package/dist/icons/icon-512x512.svg +9 -12
- package/dist/icons/icon-72x72.png +0 -0
- package/dist/icons/icon-72x72.svg +9 -12
- package/dist/icons/icon-96x96.png +0 -0
- package/dist/icons/icon-96x96.svg +9 -12
- package/dist/icons/icon-template.svg +9 -12
- package/dist/icons/qwen-ai-icon.png +0 -0
- package/dist/index.html +60 -50
- package/dist/logo.png +0 -0
- package/dist/logo.svg +11 -16
- package/dist/manifest.json +60 -60
- package/dist/sw.js +124 -124
- package/dist-server/server/claude-sdk.js +28 -5
- package/dist-server/server/claude-sdk.js.map +1 -1
- package/dist-server/server/cli.js +100 -97
- package/dist-server/server/cli.js.map +1 -1
- package/dist-server/server/daemon/manager.js +33 -33
- package/dist-server/server/daemon-manager.js +62 -62
- package/dist-server/server/database/db.js +114 -22
- package/dist-server/server/database/db.js.map +1 -1
- package/dist-server/server/database/schema.js +122 -89
- package/dist-server/server/database/schema.js.map +1 -1
- package/dist-server/server/gemini-cli.js +6 -1
- package/dist-server/server/gemini-cli.js.map +1 -1
- package/dist-server/server/index.js +346 -61
- package/dist-server/server/index.js.map +1 -1
- package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js +29 -2
- package/dist-server/server/modules/providers/list/claude/claude-auth.provider.js.map +1 -1
- package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js +22 -2
- package/dist-server/server/modules/providers/list/codex/codex-auth.provider.js.map +1 -1
- package/dist-server/server/modules/providers/list/cursor/cursor-auth.provider.js +2 -2
- package/dist-server/server/modules/providers/list/cursor/cursor-auth.provider.js.map +1 -1
- package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js +14 -2
- package/dist-server/server/modules/providers/list/gemini/gemini-auth.provider.js.map +1 -1
- package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js +132 -0
- package/dist-server/server/modules/providers/list/qwen/qwen-auth.provider.js.map +1 -0
- package/dist-server/server/modules/providers/list/qwen/qwen-mcp.provider.js +87 -0
- package/dist-server/server/modules/providers/list/qwen/qwen-mcp.provider.js.map +1 -0
- package/dist-server/server/modules/providers/list/qwen/qwen-sessions.provider.js +201 -0
- package/dist-server/server/modules/providers/list/qwen/qwen-sessions.provider.js.map +1 -0
- package/dist-server/server/modules/providers/list/qwen/qwen.provider.js +19 -0
- package/dist-server/server/modules/providers/list/qwen/qwen.provider.js.map +1 -0
- package/dist-server/server/modules/providers/provider.registry.js +2 -0
- package/dist-server/server/modules/providers/provider.registry.js.map +1 -1
- package/dist-server/server/modules/providers/provider.routes.js +478 -1
- package/dist-server/server/modules/providers/provider.routes.js.map +1 -1
- package/dist-server/server/modules/providers/shared/provider-configs.js +105 -0
- package/dist-server/server/modules/providers/shared/provider-configs.js.map +1 -0
- package/dist-server/server/projects.js +197 -6
- package/dist-server/server/projects.js.map +1 -1
- package/dist-server/server/qwen-code-cli.js +350 -0
- package/dist-server/server/qwen-code-cli.js.map +1 -0
- package/dist-server/server/qwen-response-handler.js +70 -0
- package/dist-server/server/qwen-response-handler.js.map +1 -0
- package/dist-server/server/routes/commands.js +25 -25
- package/dist-server/server/routes/git.js +17 -17
- package/dist-server/server/routes/network.js +116 -0
- package/dist-server/server/routes/network.js.map +1 -0
- package/dist-server/server/routes/projects.js +166 -1
- package/dist-server/server/routes/projects.js.map +1 -1
- package/dist-server/server/routes/qwen.js +23 -0
- package/dist-server/server/routes/qwen.js.map +1 -0
- package/dist-server/server/routes/taskmaster.js +419 -419
- package/dist-server/server/routes/telegram.js +119 -0
- package/dist-server/server/routes/telegram.js.map +1 -0
- package/dist-server/server/services/external-access.js +228 -0
- package/dist-server/server/services/external-access.js.map +1 -0
- package/dist-server/server/services/install-jobs.js +552 -0
- package/dist-server/server/services/install-jobs.js.map +1 -0
- package/dist-server/server/services/notification-orchestrator.js +19 -5
- package/dist-server/server/services/notification-orchestrator.js.map +1 -1
- package/dist-server/server/services/provider-credentials.js +154 -0
- package/dist-server/server/services/provider-credentials.js.map +1 -0
- package/dist-server/server/services/provider-models.js +218 -0
- package/dist-server/server/services/provider-models.js.map +1 -0
- package/dist-server/server/services/telegram/bot.js +259 -0
- package/dist-server/server/services/telegram/bot.js.map +1 -0
- package/dist-server/server/services/telegram/translations.js +160 -0
- package/dist-server/server/services/telegram/translations.js.map +1 -0
- package/dist-server/server/utils/port-access.js +196 -0
- package/dist-server/server/utils/port-access.js.map +1 -0
- package/dist-server/shared/modelConstants.js +18 -0
- package/dist-server/shared/modelConstants.js.map +1 -1
- package/package.json +177 -168
- package/scripts/fix-node-pty.js +67 -67
- package/server/claude-sdk.js +857 -834
- package/server/cli.js +940 -937
- package/server/constants/config.js +4 -4
- package/server/cursor-cli.js +342 -342
- package/server/daemon/manager.js +564 -564
- package/server/daemon-manager.js +920 -920
- package/server/database/db.js +696 -593
- package/server/database/schema.js +138 -102
- package/server/gemini-cli.js +475 -469
- package/server/gemini-response-handler.js +79 -79
- package/server/index.js +2854 -2556
- package/server/load-env.js +34 -34
- package/server/middleware/auth.js +132 -132
- package/server/modules/providers/list/claude/claude-auth.provider.ts +145 -123
- package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
- package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
- package/server/modules/providers/list/claude/claude.provider.ts +15 -15
- package/server/modules/providers/list/codex/codex-auth.provider.ts +115 -100
- package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
- package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
- package/server/modules/providers/list/codex/codex.provider.ts +15 -15
- package/server/modules/providers/list/cursor/cursor-auth.provider.ts +143 -143
- package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
- package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
- package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
- package/server/modules/providers/list/gemini/gemini-auth.provider.ts +163 -151
- package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
- package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
- package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
- package/server/modules/providers/list/qwen/qwen-auth.provider.ts +145 -0
- package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -0
- package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +218 -0
- package/server/modules/providers/list/qwen/qwen.provider.ts +21 -0
- package/server/modules/providers/provider.registry.ts +38 -36
- package/server/modules/providers/provider.routes.ts +781 -217
- package/server/modules/providers/services/mcp.service.ts +94 -94
- package/server/modules/providers/services/provider-auth.service.ts +26 -26
- package/server/modules/providers/services/sessions.service.ts +45 -45
- package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
- package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
- package/server/modules/providers/shared/provider-configs.ts +118 -0
- package/server/modules/providers/tests/mcp.test.ts +293 -293
- package/server/openai-codex.js +426 -426
- package/server/projects.js +2993 -2792
- package/server/qwen-code-cli.js +392 -0
- package/server/qwen-response-handler.js +73 -0
- package/server/routes/agent.js +1245 -1245
- package/server/routes/auth.js +134 -134
- package/server/routes/codex.js +19 -19
- package/server/routes/commands.js +554 -554
- package/server/routes/cursor.js +52 -52
- package/server/routes/gemini.js +24 -24
- package/server/routes/git.js +1488 -1488
- package/server/routes/mcp-utils.js +31 -31
- package/server/routes/messages.js +61 -61
- package/server/routes/network.js +128 -0
- package/server/routes/plugins.js +307 -307
- package/server/routes/projects.js +795 -627
- package/server/routes/qwen.js +27 -0
- package/server/routes/settings.js +286 -286
- package/server/routes/taskmaster.js +1471 -1471
- package/server/routes/telegram.js +125 -0
- package/server/routes/user.js +123 -123
- package/server/services/external-access.js +240 -0
- package/server/services/install-jobs.js +569 -0
- package/server/services/notification-orchestrator.js +242 -227
- package/server/services/provider-credentials.js +151 -0
- package/server/services/provider-models.js +225 -0
- package/server/services/telegram/bot.js +280 -0
- package/server/services/telegram/translations.js +170 -0
- package/server/services/vapid-keys.js +35 -35
- package/server/sessionManager.js +225 -225
- package/server/shared/interfaces.ts +54 -54
- package/server/shared/types.ts +172 -172
- package/server/shared/utils.ts +193 -193
- package/server/tsconfig.json +36 -36
- package/server/utils/colors.js +21 -21
- package/server/utils/commandParser.js +303 -303
- package/server/utils/frontmatter.js +18 -18
- package/server/utils/gitConfig.js +34 -34
- package/server/utils/mcp-detector.js +147 -147
- package/server/utils/plugin-loader.js +457 -457
- package/server/utils/plugin-process-manager.js +184 -184
- package/server/utils/port-access.js +209 -0
- package/server/utils/runtime-paths.js +37 -37
- package/server/utils/taskmaster-websocket.js +128 -128
- package/server/utils/url-detection.js +71 -71
- package/server/vite-daemon.js +78 -78
- package/shared/modelConstants.js +117 -97
- package/shared/networkHosts.js +22 -22
- package/dist/assets/index-C2c9QNwK.css +0 -32
- package/dist/assets/index-DyXDZED-.js +0 -1277
- package/dist/assets/vendor-codemirror-NA4v81it.js +0 -41
|
@@ -1,627 +1,795 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import { promises as fs } from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { spawn } from 'child_process';
|
|
5
|
-
import os from 'os';
|
|
6
|
-
import { addProjectManually } from '../projects.js';
|
|
7
|
-
|
|
8
|
-
const router = express.Router();
|
|
9
|
-
|
|
10
|
-
function sanitizeGitError(message, token) {
|
|
11
|
-
if (!message || !token) return message;
|
|
12
|
-
return message.replace(new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '***');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Configure allowed workspace root (defaults to user's home directory)
|
|
16
|
-
export const WORKSPACES_ROOT = process.env.WORKSPACES_ROOT || os.homedir();
|
|
17
|
-
export const WORKSPACES_BASE = path.resolve(
|
|
18
|
-
process.env.WORKSPACES_BASE || path.join(WORKSPACES_ROOT, 'pixcode', 'projects')
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
// System-critical paths that should never be used as workspace directories
|
|
22
|
-
export const FORBIDDEN_PATHS = [
|
|
23
|
-
// Unix
|
|
24
|
-
'/',
|
|
25
|
-
'/etc',
|
|
26
|
-
'/bin',
|
|
27
|
-
'/sbin',
|
|
28
|
-
'/usr',
|
|
29
|
-
'/dev',
|
|
30
|
-
'/proc',
|
|
31
|
-
'/sys',
|
|
32
|
-
'/var',
|
|
33
|
-
'/boot',
|
|
34
|
-
'/root',
|
|
35
|
-
'/lib',
|
|
36
|
-
'/lib64',
|
|
37
|
-
'/opt',
|
|
38
|
-
'/tmp',
|
|
39
|
-
'/run',
|
|
40
|
-
// Windows
|
|
41
|
-
'C:\\Windows',
|
|
42
|
-
'C:\\Program Files',
|
|
43
|
-
'C:\\Program Files (x86)',
|
|
44
|
-
'C:\\ProgramData',
|
|
45
|
-
'C:\\System Volume Information',
|
|
46
|
-
'C:\\$Recycle.Bin'
|
|
47
|
-
];
|
|
48
|
-
|
|
49
|
-
const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
|
|
50
|
-
|
|
51
|
-
function isPathWithin(basePath, targetPath) {
|
|
52
|
-
const normalizedBase = path.normalize(basePath);
|
|
53
|
-
const normalizedTarget = path.normalize(targetPath);
|
|
54
|
-
return (
|
|
55
|
-
normalizedTarget === normalizedBase ||
|
|
56
|
-
normalizedTarget.startsWith(normalizedBase + path.sep)
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async function realpathOrResolved(targetPath) {
|
|
61
|
-
try {
|
|
62
|
-
return await fs.realpath(targetPath);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
if (error.code === 'ENOENT') {
|
|
65
|
-
return path.resolve(targetPath);
|
|
66
|
-
}
|
|
67
|
-
throw error;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function normalizeWorkspacePath(requestedPath) {
|
|
72
|
-
if (typeof requestedPath !== 'string') {
|
|
73
|
-
return WORKSPACES_BASE;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const trimmedPath = requestedPath.trim();
|
|
77
|
-
if (!trimmedPath) {
|
|
78
|
-
return WORKSPACES_BASE;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (trimmedPath === '~') {
|
|
82
|
-
return WORKSPACES_BASE;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (trimmedPath.startsWith('~/') || trimmedPath.startsWith('~\\')) {
|
|
86
|
-
return path.join(WORKSPACES_BASE, trimmedPath.slice(2));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const isWindowsAbsolutePath = WINDOWS_ABSOLUTE_PATH_PATTERN.test(trimmedPath);
|
|
90
|
-
if (!path.isAbsolute(trimmedPath) && !isWindowsAbsolutePath) {
|
|
91
|
-
return path.join(WORKSPACES_BASE, trimmedPath);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return path.resolve(trimmedPath);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Validates that a path is safe for workspace operations
|
|
99
|
-
* @param {string} requestedPath - The path to validate
|
|
100
|
-
* @returns {Promise<{valid: boolean, resolvedPath?: string, error?: string}>}
|
|
101
|
-
*/
|
|
102
|
-
export async function validateWorkspacePath(requestedPath) {
|
|
103
|
-
try {
|
|
104
|
-
if (typeof requestedPath !== 'string' || requestedPath.trim().length === 0) {
|
|
105
|
-
return {
|
|
106
|
-
valid: false,
|
|
107
|
-
error: 'Workspace path is required'
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Resolve aliases and relative paths into a safe default base.
|
|
112
|
-
// Example: "my-app" -> "<WORKSPACES_BASE>/my-app"
|
|
113
|
-
const normalizedInputPath = normalizeWorkspacePath(requestedPath);
|
|
114
|
-
|
|
115
|
-
// Resolve to absolute path
|
|
116
|
-
let absolutePath = path.resolve(normalizedInputPath);
|
|
117
|
-
let resolvedWorkspaceBase = await realpathOrResolved(WORKSPACES_BASE);
|
|
118
|
-
resolvedWorkspaceBase = path.normalize(resolvedWorkspaceBase);
|
|
119
|
-
|
|
120
|
-
// Check if path is a forbidden system directory
|
|
121
|
-
const normalizedPath = path.normalize(absolutePath);
|
|
122
|
-
if (FORBIDDEN_PATHS.includes(normalizedPath) || normalizedPath === '/') {
|
|
123
|
-
const isRootWorkspaceException =
|
|
124
|
-
(normalizedPath === '/root' || normalizedPath.startsWith('/root' + path.sep)) &&
|
|
125
|
-
isPathWithin(resolvedWorkspaceBase, normalizedPath);
|
|
126
|
-
if (isRootWorkspaceException) {
|
|
127
|
-
// Allow /root/<base> carve-out for root installations.
|
|
128
|
-
} else {
|
|
129
|
-
return {
|
|
130
|
-
valid: false,
|
|
131
|
-
error: 'Cannot use system-critical directories as workspace locations'
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Additional check for paths starting with forbidden directories
|
|
137
|
-
for (const forbidden of FORBIDDEN_PATHS) {
|
|
138
|
-
const isInsideForbidden = normalizedPath === forbidden ||
|
|
139
|
-
normalizedPath.startsWith(forbidden + path.sep);
|
|
140
|
-
|
|
141
|
-
if (!isInsideForbidden) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const isRootWorkspaceException =
|
|
146
|
-
(forbidden === '/root') && isPathWithin(resolvedWorkspaceBase, normalizedPath);
|
|
147
|
-
if (isRootWorkspaceException) {
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Exception: /var/tmp and similar user-accessible paths might be allowed
|
|
152
|
-
// but /var itself and most /var subdirectories should be blocked
|
|
153
|
-
if (forbidden === '/var' &&
|
|
154
|
-
(normalizedPath.startsWith('/var/tmp') ||
|
|
155
|
-
normalizedPath.startsWith('/var/folders'))) {
|
|
156
|
-
continue; // Allow these specific cases
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return {
|
|
160
|
-
valid: false,
|
|
161
|
-
error: `Cannot create workspace in system directory: ${forbidden}`
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Try to resolve the real path (following symlinks)
|
|
166
|
-
let realPath;
|
|
167
|
-
try {
|
|
168
|
-
// Check if path exists to resolve real path
|
|
169
|
-
await fs.access(absolutePath);
|
|
170
|
-
realPath = await fs.realpath(absolutePath);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
if (error.code === 'ENOENT') {
|
|
173
|
-
// Path doesn't exist yet - check parent directory
|
|
174
|
-
let parentPath = path.dirname(absolutePath);
|
|
175
|
-
try {
|
|
176
|
-
const parentRealPath = await fs.realpath(parentPath);
|
|
177
|
-
|
|
178
|
-
// Reconstruct the full path with real parent
|
|
179
|
-
realPath = path.join(parentRealPath, path.basename(absolutePath));
|
|
180
|
-
} catch (parentError) {
|
|
181
|
-
if (parentError.code === 'ENOENT') {
|
|
182
|
-
// Parent doesn't exist either - use the absolute path as-is
|
|
183
|
-
// We'll validate it's within allowed root
|
|
184
|
-
realPath = absolutePath;
|
|
185
|
-
} else {
|
|
186
|
-
throw parentError;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
throw error;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Resolve the workspace root to its real path
|
|
195
|
-
const resolvedWorkspaceRoot = await realpathOrResolved(WORKSPACES_ROOT);
|
|
196
|
-
|
|
197
|
-
// Ensure the resolved path is contained within the allowed workspace root
|
|
198
|
-
if (!isPathWithin(resolvedWorkspaceRoot, realPath)) {
|
|
199
|
-
return {
|
|
200
|
-
valid: false,
|
|
201
|
-
error: `Workspace path must be within the allowed workspace root: ${WORKSPACES_ROOT}. For new projects, use ${WORKSPACES_BASE}`
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Additional symlink check for existing paths
|
|
206
|
-
try {
|
|
207
|
-
await fs.access(absolutePath);
|
|
208
|
-
const stats = await fs.lstat(absolutePath);
|
|
209
|
-
|
|
210
|
-
if (stats.isSymbolicLink()) {
|
|
211
|
-
// Verify symlink target is also within allowed root
|
|
212
|
-
const linkTarget = await fs.readlink(absolutePath);
|
|
213
|
-
const resolvedTarget = path.resolve(path.dirname(absolutePath), linkTarget);
|
|
214
|
-
const realTarget = await fs.realpath(resolvedTarget);
|
|
215
|
-
|
|
216
|
-
if (!isPathWithin(resolvedWorkspaceRoot, realTarget)) {
|
|
217
|
-
return {
|
|
218
|
-
valid: false,
|
|
219
|
-
error: 'Symlink target is outside the allowed workspace root'
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
} catch (error) {
|
|
224
|
-
if (error.code !== 'ENOENT') {
|
|
225
|
-
throw error;
|
|
226
|
-
}
|
|
227
|
-
// Path doesn't exist - that's fine for new workspace creation
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
valid: true,
|
|
232
|
-
resolvedPath: realPath
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
} catch (error) {
|
|
236
|
-
return {
|
|
237
|
-
valid: false,
|
|
238
|
-
error: `Path validation failed: ${error.message}`
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* -
|
|
252
|
-
*
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
//
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
await fs.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
*
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
if (!
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
await
|
|
465
|
-
|
|
466
|
-
res.
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
try {
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { spawn } from 'child_process';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
import { addProjectManually, extractProjectDirectory } from '../projects.js';
|
|
7
|
+
|
|
8
|
+
const router = express.Router();
|
|
9
|
+
|
|
10
|
+
function sanitizeGitError(message, token) {
|
|
11
|
+
if (!message || !token) return message;
|
|
12
|
+
return message.replace(new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '***');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Configure allowed workspace root (defaults to user's home directory)
|
|
16
|
+
export const WORKSPACES_ROOT = process.env.WORKSPACES_ROOT || os.homedir();
|
|
17
|
+
export const WORKSPACES_BASE = path.resolve(
|
|
18
|
+
process.env.WORKSPACES_BASE || path.join(WORKSPACES_ROOT, 'pixcode', 'projects')
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
// System-critical paths that should never be used as workspace directories
|
|
22
|
+
export const FORBIDDEN_PATHS = [
|
|
23
|
+
// Unix
|
|
24
|
+
'/',
|
|
25
|
+
'/etc',
|
|
26
|
+
'/bin',
|
|
27
|
+
'/sbin',
|
|
28
|
+
'/usr',
|
|
29
|
+
'/dev',
|
|
30
|
+
'/proc',
|
|
31
|
+
'/sys',
|
|
32
|
+
'/var',
|
|
33
|
+
'/boot',
|
|
34
|
+
'/root',
|
|
35
|
+
'/lib',
|
|
36
|
+
'/lib64',
|
|
37
|
+
'/opt',
|
|
38
|
+
'/tmp',
|
|
39
|
+
'/run',
|
|
40
|
+
// Windows
|
|
41
|
+
'C:\\Windows',
|
|
42
|
+
'C:\\Program Files',
|
|
43
|
+
'C:\\Program Files (x86)',
|
|
44
|
+
'C:\\ProgramData',
|
|
45
|
+
'C:\\System Volume Information',
|
|
46
|
+
'C:\\$Recycle.Bin'
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
|
|
50
|
+
|
|
51
|
+
function isPathWithin(basePath, targetPath) {
|
|
52
|
+
const normalizedBase = path.normalize(basePath);
|
|
53
|
+
const normalizedTarget = path.normalize(targetPath);
|
|
54
|
+
return (
|
|
55
|
+
normalizedTarget === normalizedBase ||
|
|
56
|
+
normalizedTarget.startsWith(normalizedBase + path.sep)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function realpathOrResolved(targetPath) {
|
|
61
|
+
try {
|
|
62
|
+
return await fs.realpath(targetPath);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error.code === 'ENOENT') {
|
|
65
|
+
return path.resolve(targetPath);
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function normalizeWorkspacePath(requestedPath) {
|
|
72
|
+
if (typeof requestedPath !== 'string') {
|
|
73
|
+
return WORKSPACES_BASE;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const trimmedPath = requestedPath.trim();
|
|
77
|
+
if (!trimmedPath) {
|
|
78
|
+
return WORKSPACES_BASE;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (trimmedPath === '~') {
|
|
82
|
+
return WORKSPACES_BASE;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (trimmedPath.startsWith('~/') || trimmedPath.startsWith('~\\')) {
|
|
86
|
+
return path.join(WORKSPACES_BASE, trimmedPath.slice(2));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isWindowsAbsolutePath = WINDOWS_ABSOLUTE_PATH_PATTERN.test(trimmedPath);
|
|
90
|
+
if (!path.isAbsolute(trimmedPath) && !isWindowsAbsolutePath) {
|
|
91
|
+
return path.join(WORKSPACES_BASE, trimmedPath);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return path.resolve(trimmedPath);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Validates that a path is safe for workspace operations
|
|
99
|
+
* @param {string} requestedPath - The path to validate
|
|
100
|
+
* @returns {Promise<{valid: boolean, resolvedPath?: string, error?: string}>}
|
|
101
|
+
*/
|
|
102
|
+
export async function validateWorkspacePath(requestedPath) {
|
|
103
|
+
try {
|
|
104
|
+
if (typeof requestedPath !== 'string' || requestedPath.trim().length === 0) {
|
|
105
|
+
return {
|
|
106
|
+
valid: false,
|
|
107
|
+
error: 'Workspace path is required'
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Resolve aliases and relative paths into a safe default base.
|
|
112
|
+
// Example: "my-app" -> "<WORKSPACES_BASE>/my-app"
|
|
113
|
+
const normalizedInputPath = normalizeWorkspacePath(requestedPath);
|
|
114
|
+
|
|
115
|
+
// Resolve to absolute path
|
|
116
|
+
let absolutePath = path.resolve(normalizedInputPath);
|
|
117
|
+
let resolvedWorkspaceBase = await realpathOrResolved(WORKSPACES_BASE);
|
|
118
|
+
resolvedWorkspaceBase = path.normalize(resolvedWorkspaceBase);
|
|
119
|
+
|
|
120
|
+
// Check if path is a forbidden system directory
|
|
121
|
+
const normalizedPath = path.normalize(absolutePath);
|
|
122
|
+
if (FORBIDDEN_PATHS.includes(normalizedPath) || normalizedPath === '/') {
|
|
123
|
+
const isRootWorkspaceException =
|
|
124
|
+
(normalizedPath === '/root' || normalizedPath.startsWith('/root' + path.sep)) &&
|
|
125
|
+
isPathWithin(resolvedWorkspaceBase, normalizedPath);
|
|
126
|
+
if (isRootWorkspaceException) {
|
|
127
|
+
// Allow /root/<base> carve-out for root installations.
|
|
128
|
+
} else {
|
|
129
|
+
return {
|
|
130
|
+
valid: false,
|
|
131
|
+
error: 'Cannot use system-critical directories as workspace locations'
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Additional check for paths starting with forbidden directories
|
|
137
|
+
for (const forbidden of FORBIDDEN_PATHS) {
|
|
138
|
+
const isInsideForbidden = normalizedPath === forbidden ||
|
|
139
|
+
normalizedPath.startsWith(forbidden + path.sep);
|
|
140
|
+
|
|
141
|
+
if (!isInsideForbidden) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const isRootWorkspaceException =
|
|
146
|
+
(forbidden === '/root') && isPathWithin(resolvedWorkspaceBase, normalizedPath);
|
|
147
|
+
if (isRootWorkspaceException) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Exception: /var/tmp and similar user-accessible paths might be allowed
|
|
152
|
+
// but /var itself and most /var subdirectories should be blocked
|
|
153
|
+
if (forbidden === '/var' &&
|
|
154
|
+
(normalizedPath.startsWith('/var/tmp') ||
|
|
155
|
+
normalizedPath.startsWith('/var/folders'))) {
|
|
156
|
+
continue; // Allow these specific cases
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
valid: false,
|
|
161
|
+
error: `Cannot create workspace in system directory: ${forbidden}`
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Try to resolve the real path (following symlinks)
|
|
166
|
+
let realPath;
|
|
167
|
+
try {
|
|
168
|
+
// Check if path exists to resolve real path
|
|
169
|
+
await fs.access(absolutePath);
|
|
170
|
+
realPath = await fs.realpath(absolutePath);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (error.code === 'ENOENT') {
|
|
173
|
+
// Path doesn't exist yet - check parent directory
|
|
174
|
+
let parentPath = path.dirname(absolutePath);
|
|
175
|
+
try {
|
|
176
|
+
const parentRealPath = await fs.realpath(parentPath);
|
|
177
|
+
|
|
178
|
+
// Reconstruct the full path with real parent
|
|
179
|
+
realPath = path.join(parentRealPath, path.basename(absolutePath));
|
|
180
|
+
} catch (parentError) {
|
|
181
|
+
if (parentError.code === 'ENOENT') {
|
|
182
|
+
// Parent doesn't exist either - use the absolute path as-is
|
|
183
|
+
// We'll validate it's within allowed root
|
|
184
|
+
realPath = absolutePath;
|
|
185
|
+
} else {
|
|
186
|
+
throw parentError;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Resolve the workspace root to its real path
|
|
195
|
+
const resolvedWorkspaceRoot = await realpathOrResolved(WORKSPACES_ROOT);
|
|
196
|
+
|
|
197
|
+
// Ensure the resolved path is contained within the allowed workspace root
|
|
198
|
+
if (!isPathWithin(resolvedWorkspaceRoot, realPath)) {
|
|
199
|
+
return {
|
|
200
|
+
valid: false,
|
|
201
|
+
error: `Workspace path must be within the allowed workspace root: ${WORKSPACES_ROOT}. For new projects, use ${WORKSPACES_BASE}`
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Additional symlink check for existing paths
|
|
206
|
+
try {
|
|
207
|
+
await fs.access(absolutePath);
|
|
208
|
+
const stats = await fs.lstat(absolutePath);
|
|
209
|
+
|
|
210
|
+
if (stats.isSymbolicLink()) {
|
|
211
|
+
// Verify symlink target is also within allowed root
|
|
212
|
+
const linkTarget = await fs.readlink(absolutePath);
|
|
213
|
+
const resolvedTarget = path.resolve(path.dirname(absolutePath), linkTarget);
|
|
214
|
+
const realTarget = await fs.realpath(resolvedTarget);
|
|
215
|
+
|
|
216
|
+
if (!isPathWithin(resolvedWorkspaceRoot, realTarget)) {
|
|
217
|
+
return {
|
|
218
|
+
valid: false,
|
|
219
|
+
error: 'Symlink target is outside the allowed workspace root'
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (error.code !== 'ENOENT') {
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
// Path doesn't exist - that's fine for new workspace creation
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
valid: true,
|
|
232
|
+
resolvedPath: realPath
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return {
|
|
237
|
+
valid: false,
|
|
238
|
+
error: `Path validation failed: ${error.message}`
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Is this `pixcode-project-N` slot already in use? "In use" means the
|
|
245
|
+
* user has sent at least one message under any provider — presence of a
|
|
246
|
+
* session file under ~/.claude/projects/<encoded>/, ~/.codex/sessions/,
|
|
247
|
+
* or ~/.gemini/… is our signal. We keep it best-effort: if we can't
|
|
248
|
+
* probe a provider's session dir (no permissions, path missing), we
|
|
249
|
+
* treat it as "no sessions for this provider" rather than raise.
|
|
250
|
+
*
|
|
251
|
+
* Checking the on-disk workspace dir for files is NOT a reliable signal
|
|
252
|
+
* — providers store their history outside the workspace, so a project
|
|
253
|
+
* that has had 20 messages still has an empty folder.
|
|
254
|
+
*/
|
|
255
|
+
async function projectHasAnySessions(workspacePath) {
|
|
256
|
+
const home = os.homedir();
|
|
257
|
+
// encodeProjectName strips drive separators (C:\ → -C--…) and dots so
|
|
258
|
+
// `extractProjectDirectory` can round-trip. Using the same encoder as
|
|
259
|
+
// the rest of projects.js keeps us aligned with however Claude's CLI
|
|
260
|
+
// computes its per-project directory name.
|
|
261
|
+
const slug = workspacePath.replace(/[\\/:]/g, '-').replace(/\./g, '-');
|
|
262
|
+
|
|
263
|
+
const probes = [
|
|
264
|
+
// Claude Code: JSONL-per-session files under a per-project subdir.
|
|
265
|
+
path.join(home, '.claude', 'projects', slug),
|
|
266
|
+
// Codex writes session logs under ~/.codex/sessions — they're cross-project
|
|
267
|
+
// so we can't cheaply attribute them to a specific slot; skip.
|
|
268
|
+
// Gemini: same layout as Claude.
|
|
269
|
+
path.join(home, '.gemini', 'projects', slug),
|
|
270
|
+
// Qwen Code (Gemini fork): same layout.
|
|
271
|
+
path.join(home, '.qwen', 'projects', slug),
|
|
272
|
+
];
|
|
273
|
+
|
|
274
|
+
for (const dir of probes) {
|
|
275
|
+
try {
|
|
276
|
+
const entries = await fs.readdir(dir);
|
|
277
|
+
if (entries.some((name) => name.endsWith('.jsonl') || name.endsWith('.json'))) {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
} catch {
|
|
281
|
+
// Missing / unreadable dir just means "no sessions here", not fatal.
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* GET /api/projects/:projectName/dir-status
|
|
289
|
+
*
|
|
290
|
+
* Lightweight "does the workspace still exist on disk?" check used by
|
|
291
|
+
* the chat composer to detect deleted-directory sessions. We decode the
|
|
292
|
+
* project name back to an absolute path and stat it — a slug alone isn't
|
|
293
|
+
* useful because the user may have deleted the workspace while the
|
|
294
|
+
* session metadata still lives under ~/.<provider>/projects/.
|
|
295
|
+
*
|
|
296
|
+
* Returns `{ exists, path, isDirectory }` so the UI can lock the
|
|
297
|
+
* composer and surface a "directory deleted" warning instead of letting
|
|
298
|
+
* the user fire prompts into a void.
|
|
299
|
+
*/
|
|
300
|
+
router.get('/:projectName/dir-status', async (req, res) => {
|
|
301
|
+
const { projectName } = req.params;
|
|
302
|
+
try {
|
|
303
|
+
const actualPath = await extractProjectDirectory(projectName);
|
|
304
|
+
if (!actualPath) {
|
|
305
|
+
return res.json({ exists: false, path: null, isDirectory: false });
|
|
306
|
+
}
|
|
307
|
+
try {
|
|
308
|
+
const stat = await fs.stat(actualPath);
|
|
309
|
+
return res.json({
|
|
310
|
+
exists: true,
|
|
311
|
+
path: actualPath,
|
|
312
|
+
isDirectory: stat.isDirectory(),
|
|
313
|
+
});
|
|
314
|
+
} catch (err) {
|
|
315
|
+
// ENOENT is the typical "user rm -rf'd the workspace" path.
|
|
316
|
+
if (err.code === 'ENOENT') {
|
|
317
|
+
return res.json({ exists: false, path: actualPath, isDirectory: false });
|
|
318
|
+
}
|
|
319
|
+
throw err;
|
|
320
|
+
}
|
|
321
|
+
} catch (error) {
|
|
322
|
+
console.error(`[projects] dir-status ${projectName}:`, error);
|
|
323
|
+
res.status(500).json({ error: error.message || 'Failed to check project directory' });
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* POST /api/projects/quick-start
|
|
329
|
+
*
|
|
330
|
+
* Zero-config project creation: **reuses** the first unused
|
|
331
|
+
* `pixcode-project-N` slot if one exists, otherwise creates the next
|
|
332
|
+
* free index. "Unused" = no session files on disk for any provider.
|
|
333
|
+
* Without reuse, clicking "New chat" rapidly stacks up pixcode-project-1
|
|
334
|
+
* through pixcode-project-N and litters the workspace — the UX we want
|
|
335
|
+
* matches ChatGPT's "New chat" which reuses the empty canvas until the
|
|
336
|
+
* user actually commits a message.
|
|
337
|
+
*/
|
|
338
|
+
router.post('/quick-start', async (req, res) => {
|
|
339
|
+
try {
|
|
340
|
+
await fs.mkdir(WORKSPACES_BASE, { recursive: true });
|
|
341
|
+
|
|
342
|
+
let entries = [];
|
|
343
|
+
try {
|
|
344
|
+
entries = await fs.readdir(WORKSPACES_BASE, { withFileTypes: true });
|
|
345
|
+
} catch { /* empty is fine */ }
|
|
346
|
+
|
|
347
|
+
// Pixcode-owned slots, sorted by numeric index so reuse is deterministic
|
|
348
|
+
// and picks the lowest idle slot (pixcode-project-1 before -3).
|
|
349
|
+
const existingSlots = entries
|
|
350
|
+
.filter((e) => e.isDirectory() && /^pixcode-project-\d+$/i.test(e.name))
|
|
351
|
+
.map((e) => ({
|
|
352
|
+
name: e.name,
|
|
353
|
+
index: parseInt(e.name.split('-').pop(), 10) || 0,
|
|
354
|
+
}))
|
|
355
|
+
.sort((a, b) => a.index - b.index);
|
|
356
|
+
|
|
357
|
+
// 1. First pass: reuse the lowest-indexed slot that has no sessions.
|
|
358
|
+
for (const slot of existingSlots) {
|
|
359
|
+
const absolutePath = path.join(WORKSPACES_BASE, slot.name);
|
|
360
|
+
const used = await projectHasAnySessions(absolutePath);
|
|
361
|
+
if (!used) {
|
|
362
|
+
let project;
|
|
363
|
+
try {
|
|
364
|
+
project = await addProjectManually(absolutePath);
|
|
365
|
+
} catch (err) {
|
|
366
|
+
// addProjectManually throws when the project is already
|
|
367
|
+
// registered. That's fine — look it up via its encoded name
|
|
368
|
+
// instead of creating a duplicate.
|
|
369
|
+
const msg = err?.message || '';
|
|
370
|
+
if (!/already configured/i.test(msg)) throw err;
|
|
371
|
+
project = {
|
|
372
|
+
name: absolutePath.replace(/[\\/:]/g, '-').replace(/\./g, '-'),
|
|
373
|
+
path: absolutePath,
|
|
374
|
+
fullPath: absolutePath,
|
|
375
|
+
displayName: slot.name,
|
|
376
|
+
isManuallyAdded: true,
|
|
377
|
+
sessions: [],
|
|
378
|
+
cursorSessions: [],
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
return res.json({
|
|
382
|
+
success: true,
|
|
383
|
+
project,
|
|
384
|
+
suggestedName: slot.name,
|
|
385
|
+
reused: true,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// 2. No idle slot — create the next free index above what exists.
|
|
391
|
+
const takenIndices = new Set(existingSlots.map((s) => s.index));
|
|
392
|
+
let nextIndex = 1;
|
|
393
|
+
while (takenIndices.has(nextIndex)) {
|
|
394
|
+
nextIndex += 1;
|
|
395
|
+
if (nextIndex > 9999) {
|
|
396
|
+
return res.status(500).json({ error: 'No free pixcode-project slot (exhausted 1..9999)' });
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const name = `pixcode-project-${nextIndex}`;
|
|
400
|
+
const absolutePath = path.join(WORKSPACES_BASE, name);
|
|
401
|
+
await fs.mkdir(absolutePath, { recursive: true });
|
|
402
|
+
const project = await addProjectManually(absolutePath);
|
|
403
|
+
|
|
404
|
+
res.json({ success: true, project, suggestedName: name, reused: false });
|
|
405
|
+
} catch (error) {
|
|
406
|
+
console.error('[projects] quick-start failed:', error);
|
|
407
|
+
res.status(500).json({ error: error.message || 'Failed to quick-start project' });
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Create a new workspace
|
|
413
|
+
* POST /api/projects/create-workspace
|
|
414
|
+
*
|
|
415
|
+
* Body:
|
|
416
|
+
* - workspaceType: 'existing' | 'new'
|
|
417
|
+
* - path: string (workspace path)
|
|
418
|
+
* - githubUrl?: string (optional, for new workspaces)
|
|
419
|
+
* - githubTokenId?: number (optional, ID of stored token)
|
|
420
|
+
* - newGithubToken?: string (optional, one-time token)
|
|
421
|
+
*/
|
|
422
|
+
router.post('/create-workspace', async (req, res) => {
|
|
423
|
+
try {
|
|
424
|
+
const { workspaceType, path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.body;
|
|
425
|
+
|
|
426
|
+
// Validate required fields
|
|
427
|
+
if (!workspaceType || !workspacePath) {
|
|
428
|
+
return res.status(400).json({ error: 'workspaceType and path are required' });
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (!['existing', 'new'].includes(workspaceType)) {
|
|
432
|
+
return res.status(400).json({ error: 'workspaceType must be "existing" or "new"' });
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Validate path safety before any operations
|
|
436
|
+
const validation = await validateWorkspacePath(workspacePath);
|
|
437
|
+
if (!validation.valid) {
|
|
438
|
+
return res.status(400).json({
|
|
439
|
+
error: 'Invalid workspace path',
|
|
440
|
+
details: validation.error
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const absolutePath = validation.resolvedPath;
|
|
445
|
+
|
|
446
|
+
// Handle existing workspace
|
|
447
|
+
if (workspaceType === 'existing') {
|
|
448
|
+
// Check if the path exists
|
|
449
|
+
try {
|
|
450
|
+
await fs.access(absolutePath);
|
|
451
|
+
const stats = await fs.stat(absolutePath);
|
|
452
|
+
|
|
453
|
+
if (!stats.isDirectory()) {
|
|
454
|
+
return res.status(400).json({ error: 'Path exists but is not a directory' });
|
|
455
|
+
}
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (error.code === 'ENOENT') {
|
|
458
|
+
return res.status(404).json({ error: 'Workspace path does not exist' });
|
|
459
|
+
}
|
|
460
|
+
throw error;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Add the existing workspace to the project list
|
|
464
|
+
const project = await addProjectManually(absolutePath);
|
|
465
|
+
|
|
466
|
+
return res.json({
|
|
467
|
+
success: true,
|
|
468
|
+
project,
|
|
469
|
+
message: 'Existing workspace added successfully'
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Handle new workspace creation
|
|
474
|
+
if (workspaceType === 'new') {
|
|
475
|
+
// Create the directory if it doesn't exist
|
|
476
|
+
await fs.mkdir(absolutePath, { recursive: true });
|
|
477
|
+
|
|
478
|
+
// If GitHub URL is provided, clone the repository
|
|
479
|
+
if (githubUrl) {
|
|
480
|
+
let githubToken = null;
|
|
481
|
+
|
|
482
|
+
// Get GitHub token if needed
|
|
483
|
+
if (githubTokenId) {
|
|
484
|
+
// Fetch token from database
|
|
485
|
+
const token = await getGithubTokenById(githubTokenId, req.user.id);
|
|
486
|
+
if (!token) {
|
|
487
|
+
// Clean up created directory
|
|
488
|
+
await fs.rm(absolutePath, { recursive: true, force: true });
|
|
489
|
+
return res.status(404).json({ error: 'GitHub token not found' });
|
|
490
|
+
}
|
|
491
|
+
githubToken = token.github_token;
|
|
492
|
+
} else if (newGithubToken) {
|
|
493
|
+
githubToken = newGithubToken;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Extract repo name from URL for the clone destination
|
|
497
|
+
const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
|
|
498
|
+
const repoName = normalizedUrl.split('/').pop() || 'repository';
|
|
499
|
+
const clonePath = path.join(absolutePath, repoName);
|
|
500
|
+
|
|
501
|
+
// Check if clone destination already exists to prevent data loss
|
|
502
|
+
try {
|
|
503
|
+
await fs.access(clonePath);
|
|
504
|
+
return res.status(409).json({
|
|
505
|
+
error: 'Directory already exists',
|
|
506
|
+
details: `The destination path "${clonePath}" already exists. Please choose a different location or remove the existing directory.`
|
|
507
|
+
});
|
|
508
|
+
} catch (err) {
|
|
509
|
+
// Directory doesn't exist, which is what we want
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Clone the repository into a subfolder
|
|
513
|
+
try {
|
|
514
|
+
await cloneGitHubRepository(githubUrl, clonePath, githubToken);
|
|
515
|
+
} catch (error) {
|
|
516
|
+
// Only clean up if clone created partial data (check if dir exists and is empty or partial)
|
|
517
|
+
try {
|
|
518
|
+
const stats = await fs.stat(clonePath);
|
|
519
|
+
if (stats.isDirectory()) {
|
|
520
|
+
await fs.rm(clonePath, { recursive: true, force: true });
|
|
521
|
+
}
|
|
522
|
+
} catch (cleanupError) {
|
|
523
|
+
// Directory doesn't exist or cleanup failed - ignore
|
|
524
|
+
}
|
|
525
|
+
throw new Error(`Failed to clone repository: ${error.message}`);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// Add the cloned repo path to the project list
|
|
529
|
+
const project = await addProjectManually(clonePath);
|
|
530
|
+
|
|
531
|
+
return res.json({
|
|
532
|
+
success: true,
|
|
533
|
+
project,
|
|
534
|
+
message: 'New workspace created and repository cloned successfully'
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// Add the new workspace to the project list (no clone)
|
|
539
|
+
const project = await addProjectManually(absolutePath);
|
|
540
|
+
|
|
541
|
+
return res.json({
|
|
542
|
+
success: true,
|
|
543
|
+
project,
|
|
544
|
+
message: 'New workspace created successfully'
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
} catch (error) {
|
|
549
|
+
console.error('Error creating workspace:', error);
|
|
550
|
+
res.status(500).json({
|
|
551
|
+
error: error.message || 'Failed to create workspace',
|
|
552
|
+
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Helper function to get GitHub token from database
|
|
559
|
+
*/
|
|
560
|
+
async function getGithubTokenById(tokenId, userId) {
|
|
561
|
+
const { db } = await import('../database/db.js');
|
|
562
|
+
|
|
563
|
+
const credential = db.prepare(
|
|
564
|
+
'SELECT * FROM user_credentials WHERE id = ? AND user_id = ? AND credential_type = ? AND is_active = 1'
|
|
565
|
+
).get(tokenId, userId, 'github_token');
|
|
566
|
+
|
|
567
|
+
// Return in the expected format (github_token field for compatibility)
|
|
568
|
+
if (credential) {
|
|
569
|
+
return {
|
|
570
|
+
...credential,
|
|
571
|
+
github_token: credential.credential_value
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Clone repository with progress streaming (SSE)
|
|
580
|
+
* GET /api/projects/clone-progress
|
|
581
|
+
*/
|
|
582
|
+
router.get('/clone-progress', async (req, res) => {
|
|
583
|
+
const { path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.query;
|
|
584
|
+
|
|
585
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
586
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
587
|
+
res.setHeader('Connection', 'keep-alive');
|
|
588
|
+
res.flushHeaders();
|
|
589
|
+
|
|
590
|
+
const sendEvent = (type, data) => {
|
|
591
|
+
res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
try {
|
|
595
|
+
if (!workspacePath || !githubUrl) {
|
|
596
|
+
sendEvent('error', { message: 'workspacePath and githubUrl are required' });
|
|
597
|
+
res.end();
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const validation = await validateWorkspacePath(workspacePath);
|
|
602
|
+
if (!validation.valid) {
|
|
603
|
+
sendEvent('error', { message: validation.error });
|
|
604
|
+
res.end();
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const absolutePath = validation.resolvedPath;
|
|
609
|
+
|
|
610
|
+
await fs.mkdir(absolutePath, { recursive: true });
|
|
611
|
+
|
|
612
|
+
let githubToken = null;
|
|
613
|
+
if (githubTokenId) {
|
|
614
|
+
const token = await getGithubTokenById(parseInt(githubTokenId), req.user.id);
|
|
615
|
+
if (!token) {
|
|
616
|
+
await fs.rm(absolutePath, { recursive: true, force: true });
|
|
617
|
+
sendEvent('error', { message: 'GitHub token not found' });
|
|
618
|
+
res.end();
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
githubToken = token.github_token;
|
|
622
|
+
} else if (newGithubToken) {
|
|
623
|
+
githubToken = newGithubToken;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
|
|
627
|
+
const repoName = normalizedUrl.split('/').pop() || 'repository';
|
|
628
|
+
const clonePath = path.join(absolutePath, repoName);
|
|
629
|
+
|
|
630
|
+
// Check if clone destination already exists to prevent data loss
|
|
631
|
+
try {
|
|
632
|
+
await fs.access(clonePath);
|
|
633
|
+
sendEvent('error', { message: `Directory "${repoName}" already exists. Please choose a different location or remove the existing directory.` });
|
|
634
|
+
res.end();
|
|
635
|
+
return;
|
|
636
|
+
} catch (err) {
|
|
637
|
+
// Directory doesn't exist, which is what we want
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
let cloneUrl = githubUrl;
|
|
641
|
+
if (githubToken) {
|
|
642
|
+
try {
|
|
643
|
+
const url = new URL(githubUrl);
|
|
644
|
+
url.username = githubToken;
|
|
645
|
+
url.password = '';
|
|
646
|
+
cloneUrl = url.toString();
|
|
647
|
+
} catch (error) {
|
|
648
|
+
// SSH URL or invalid - use as-is
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
sendEvent('progress', { message: `Cloning into '${repoName}'...` });
|
|
653
|
+
|
|
654
|
+
const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, clonePath], {
|
|
655
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
656
|
+
env: {
|
|
657
|
+
...process.env,
|
|
658
|
+
GIT_TERMINAL_PROMPT: '0'
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
let lastError = '';
|
|
663
|
+
|
|
664
|
+
gitProcess.stdout.on('data', (data) => {
|
|
665
|
+
const message = data.toString().trim();
|
|
666
|
+
if (message) {
|
|
667
|
+
sendEvent('progress', { message });
|
|
668
|
+
}
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
gitProcess.stderr.on('data', (data) => {
|
|
672
|
+
const message = data.toString().trim();
|
|
673
|
+
lastError = message;
|
|
674
|
+
if (message) {
|
|
675
|
+
sendEvent('progress', { message });
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
gitProcess.on('close', async (code) => {
|
|
680
|
+
if (code === 0) {
|
|
681
|
+
try {
|
|
682
|
+
const project = await addProjectManually(clonePath);
|
|
683
|
+
sendEvent('complete', { project, message: 'Repository cloned successfully' });
|
|
684
|
+
} catch (error) {
|
|
685
|
+
sendEvent('error', { message: `Clone succeeded but failed to add project: ${error.message}` });
|
|
686
|
+
}
|
|
687
|
+
} else {
|
|
688
|
+
const sanitizedError = sanitizeGitError(lastError, githubToken);
|
|
689
|
+
let errorMessage = 'Git clone failed';
|
|
690
|
+
if (lastError.includes('Authentication failed') || lastError.includes('could not read Username')) {
|
|
691
|
+
errorMessage = 'Authentication failed. Please check your credentials.';
|
|
692
|
+
} else if (lastError.includes('Repository not found')) {
|
|
693
|
+
errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
|
|
694
|
+
} else if (lastError.includes('already exists')) {
|
|
695
|
+
errorMessage = 'Directory already exists';
|
|
696
|
+
} else if (sanitizedError) {
|
|
697
|
+
errorMessage = sanitizedError;
|
|
698
|
+
}
|
|
699
|
+
try {
|
|
700
|
+
await fs.rm(clonePath, { recursive: true, force: true });
|
|
701
|
+
} catch (cleanupError) {
|
|
702
|
+
console.error('Failed to clean up after clone failure:', sanitizeGitError(cleanupError.message, githubToken));
|
|
703
|
+
}
|
|
704
|
+
sendEvent('error', { message: errorMessage });
|
|
705
|
+
}
|
|
706
|
+
res.end();
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
gitProcess.on('error', (error) => {
|
|
710
|
+
if (error.code === 'ENOENT') {
|
|
711
|
+
sendEvent('error', { message: 'Git is not installed or not in PATH' });
|
|
712
|
+
} else {
|
|
713
|
+
sendEvent('error', { message: error.message });
|
|
714
|
+
}
|
|
715
|
+
res.end();
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
req.on('close', () => {
|
|
719
|
+
gitProcess.kill();
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
} catch (error) {
|
|
723
|
+
sendEvent('error', { message: error.message });
|
|
724
|
+
res.end();
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Helper function to clone a GitHub repository
|
|
730
|
+
*/
|
|
731
|
+
function cloneGitHubRepository(githubUrl, destinationPath, githubToken = null) {
|
|
732
|
+
return new Promise((resolve, reject) => {
|
|
733
|
+
let cloneUrl = githubUrl;
|
|
734
|
+
|
|
735
|
+
if (githubToken) {
|
|
736
|
+
try {
|
|
737
|
+
const url = new URL(githubUrl);
|
|
738
|
+
url.username = githubToken;
|
|
739
|
+
url.password = '';
|
|
740
|
+
cloneUrl = url.toString();
|
|
741
|
+
} catch (error) {
|
|
742
|
+
// SSH URL - use as-is
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, destinationPath], {
|
|
747
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
748
|
+
env: {
|
|
749
|
+
...process.env,
|
|
750
|
+
GIT_TERMINAL_PROMPT: '0'
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
let stdout = '';
|
|
755
|
+
let stderr = '';
|
|
756
|
+
|
|
757
|
+
gitProcess.stdout.on('data', (data) => {
|
|
758
|
+
stdout += data.toString();
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
gitProcess.stderr.on('data', (data) => {
|
|
762
|
+
stderr += data.toString();
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
gitProcess.on('close', (code) => {
|
|
766
|
+
if (code === 0) {
|
|
767
|
+
resolve({ stdout, stderr });
|
|
768
|
+
} else {
|
|
769
|
+
let errorMessage = 'Git clone failed';
|
|
770
|
+
|
|
771
|
+
if (stderr.includes('Authentication failed') || stderr.includes('could not read Username')) {
|
|
772
|
+
errorMessage = 'Authentication failed. Please check your GitHub token.';
|
|
773
|
+
} else if (stderr.includes('Repository not found')) {
|
|
774
|
+
errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
|
|
775
|
+
} else if (stderr.includes('already exists')) {
|
|
776
|
+
errorMessage = 'Directory already exists';
|
|
777
|
+
} else if (stderr) {
|
|
778
|
+
errorMessage = stderr;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
reject(new Error(errorMessage));
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
gitProcess.on('error', (error) => {
|
|
786
|
+
if (error.code === 'ENOENT') {
|
|
787
|
+
reject(new Error('Git is not installed or not in PATH'));
|
|
788
|
+
} else {
|
|
789
|
+
reject(error);
|
|
790
|
+
}
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
export default router;
|