dr-claw 1.1.2
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 +1380 -0
- package/NOTICE +17 -0
- package/README.md +875 -0
- package/README.zh-CN.md +857 -0
- package/dist/api-docs.html +878 -0
- package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/dist/assets/_basePickBy-C0n1uo4u.js +1 -0
- package/dist/assets/_baseUniq-BRNmhFkc.js +1 -0
- package/dist/assets/arc-DOMBmC_A.js +1 -0
- package/dist/assets/architectureDiagram-VXUJARFQ-CtMWglbD.js +36 -0
- package/dist/assets/blockDiagram-VD42YOAC-DK_gTkJy.js +122 -0
- package/dist/assets/c4Diagram-YG6GDRKO-DrDAvsn7.js +10 -0
- package/dist/assets/channel-BFOE7OyC.js +1 -0
- package/dist/assets/chunk-4BX2VUAB-CGbhdZcN.js +1 -0
- package/dist/assets/chunk-55IACEB6-sGeFSxen.js +1 -0
- package/dist/assets/chunk-B4BG7PRW-RxgHAU_P.js +165 -0
- package/dist/assets/chunk-DI55MBZ5-CvHUXZrx.js +220 -0
- package/dist/assets/chunk-FMBD7UC4-Sn7cXVNU.js +15 -0
- package/dist/assets/chunk-QN33PNHL-bDKOHppN.js +1 -0
- package/dist/assets/chunk-QZHKN3VN-B9kN5LGm.js +1 -0
- package/dist/assets/chunk-TZMSLE5B-CCBe6len.js +1 -0
- package/dist/assets/classDiagram-2ON5EDUG-DQoMNYlY.js +1 -0
- package/dist/assets/classDiagram-v2-WZHVMYZB-DQoMNYlY.js +1 -0
- package/dist/assets/clone-BulQSblH.js +1 -0
- package/dist/assets/cose-bilkent-S5V4N54A-r6sLiHBW.js +1 -0
- package/dist/assets/cytoscape.esm-5J0xJHOV.js +321 -0
- package/dist/assets/dagre-6UL2VRFP-AbsaR1Oa.js +4 -0
- package/dist/assets/defaultLocale-DX6XiGOO.js +1 -0
- package/dist/assets/diagram-PSM6KHXK-DjCvdUyy.js +24 -0
- package/dist/assets/diagram-QEK2KX5R-CqiocXkH.js +43 -0
- package/dist/assets/diagram-S2PKOQOG-D5ALeoII.js +24 -0
- package/dist/assets/erDiagram-Q2GNP2WA-C1AeOsiz.js +60 -0
- package/dist/assets/flowDiagram-NV44I4VS-C98ve_DO.js +162 -0
- package/dist/assets/ganttDiagram-JELNMOA3-3zdLvcxD.js +267 -0
- package/dist/assets/gitGraphDiagram-V2S2FVAM-nDuDMsHh.js +65 -0
- package/dist/assets/graph-Blm-emkR.js +1 -0
- package/dist/assets/index-B8Yutsnm.css +1 -0
- package/dist/assets/index-Cf6gyhc3.js +654 -0
- package/dist/assets/infoDiagram-HS3SLOUP-CmzE3q0R.js +2 -0
- package/dist/assets/init-Gi6I4Gst.js +1 -0
- package/dist/assets/journeyDiagram-XKPGCS4Q-CYaW7LrP.js +139 -0
- package/dist/assets/kanban-definition-3W4ZIXB7-mkmW7p0j.js +89 -0
- package/dist/assets/layout-Ct2kFIId.js +1 -0
- package/dist/assets/linear-CKG9EwIJ.js +1 -0
- package/dist/assets/mermaid.core-CaKcUnnd.js +250 -0
- package/dist/assets/mindmap-definition-VGOIOE7T-Bct-pYin.js +68 -0
- package/dist/assets/ordinal-Cboi1Yqb.js +1 -0
- package/dist/assets/pieDiagram-ADFJNKIX-C8wFD4Jr.js +30 -0
- package/dist/assets/quadrantDiagram-AYHSOK5B-CuiE_f46.js +7 -0
- package/dist/assets/requirementDiagram-UZGBJVZJ-C4v1CqU8.js +64 -0
- package/dist/assets/sankeyDiagram-TZEHDZUN-keAqqs-Y.js +10 -0
- package/dist/assets/sequenceDiagram-WL72ISMW-ClNbvKTB.js +145 -0
- package/dist/assets/stateDiagram-FKZM4ZOC-D8We-2vq.js +1 -0
- package/dist/assets/stateDiagram-v2-4FDKWEC3-BUURfjrE.js +1 -0
- package/dist/assets/timeline-definition-IT6M3QCI-DsvEMT5T.js +61 -0
- package/dist/assets/treemap-GDKQZRPO-TJrzN7Bj.js +162 -0
- package/dist/assets/vendor-codemirror-ansM-eig.js +33 -0
- package/dist/assets/vendor-react-BWF6YQQ3.js +8 -0
- package/dist/assets/vendor-xterm-DfcmCpbH.js +66 -0
- package/dist/assets/xychartDiagram-PRI3JC2R-BdATmY5F.js +7 -0
- package/dist/clear-cache.html +85 -0
- package/dist/convert-icons.md +53 -0
- package/dist/dr-claw.png +0 -0
- package/dist/favicon.png +0 -0
- package/dist/generate-icons.js +49 -0
- package/dist/icons/claude-ai-icon.svg +1 -0
- package/dist/icons/codex-white.svg +3 -0
- package/dist/icons/codex.svg +3 -0
- package/dist/icons/cursor-white.svg +12 -0
- package/dist/icons/cursor.svg +1 -0
- package/dist/icons/file.svg +16997 -0
- package/dist/icons/generate-icons.md +19 -0
- package/dist/icons/icon-128x128.png +0 -0
- package/dist/icons/icon-144x144.png +0 -0
- package/dist/icons/icon-152x152.png +0 -0
- package/dist/icons/icon-192x192.png +0 -0
- package/dist/icons/icon-384x384.png +0 -0
- package/dist/icons/icon-512x512.png +0 -0
- package/dist/icons/icon-72x72.png +0 -0
- package/dist/icons/icon-96x96.png +0 -0
- package/dist/icons/news/arxiv.svg +1 -0
- package/dist/icons/news/huggingface.svg +8 -0
- package/dist/icons/news/x-black.png +0 -0
- package/dist/icons/news/x-white.png +0 -0
- package/dist/icons/news/xiaohongshu.png +0 -0
- package/dist/index.html +51 -0
- package/dist/leveraged-cognition.png +0 -0
- package/dist/manifest.json +61 -0
- package/dist/screenshots/chat.png +0 -0
- package/dist/screenshots/new_claw.png +0 -0
- package/dist/screenshots/news_dashboard.png +0 -0
- package/dist/screenshots/project_dashboard.png +0 -0
- package/dist/screenshots/research_lab.png +0 -0
- package/dist/screenshots/skill_library.png +0 -0
- package/dist/screenshots/task_list.png +0 -0
- package/dist/screenshots/terminal_example.png +0 -0
- package/dist/screenshots/terminal_example1.png +0 -0
- package/dist/sw.js +58 -0
- package/dist/wechat-group-qr.jpg +0 -0
- package/package.json +133 -0
- package/public/api-docs.html +878 -0
- package/public/clear-cache.html +85 -0
- package/public/convert-icons.md +53 -0
- package/public/dr-claw.png +0 -0
- package/public/favicon.png +0 -0
- package/public/generate-icons.js +49 -0
- package/public/icons/claude-ai-icon.svg +1 -0
- package/public/icons/codex-white.svg +3 -0
- package/public/icons/codex.svg +3 -0
- package/public/icons/cursor-white.svg +12 -0
- package/public/icons/cursor.svg +1 -0
- package/public/icons/file.svg +16997 -0
- package/public/icons/generate-icons.md +19 -0
- package/public/icons/icon-128x128.png +0 -0
- package/public/icons/icon-144x144.png +0 -0
- package/public/icons/icon-152x152.png +0 -0
- package/public/icons/icon-192x192.png +0 -0
- package/public/icons/icon-384x384.png +0 -0
- package/public/icons/icon-512x512.png +0 -0
- package/public/icons/icon-72x72.png +0 -0
- package/public/icons/icon-96x96.png +0 -0
- package/public/icons/news/arxiv.svg +1 -0
- package/public/icons/news/huggingface.svg +8 -0
- package/public/icons/news/x-black.png +0 -0
- package/public/icons/news/x-white.png +0 -0
- package/public/icons/news/xiaohongshu.png +0 -0
- package/public/leveraged-cognition.png +0 -0
- package/public/manifest.json +61 -0
- package/public/screenshots/chat.png +0 -0
- package/public/screenshots/new_claw.png +0 -0
- package/public/screenshots/news_dashboard.png +0 -0
- package/public/screenshots/project_dashboard.png +0 -0
- package/public/screenshots/research_lab.png +0 -0
- package/public/screenshots/skill_library.png +0 -0
- package/public/screenshots/task_list.png +0 -0
- package/public/screenshots/terminal_example.png +0 -0
- package/public/screenshots/terminal_example1.png +0 -0
- package/public/sw.js +58 -0
- package/public/wechat-group-qr.jpg +0 -0
- package/scripts/export-skills-catalog-v2.mjs +746 -0
- package/scripts/fix-node-pty.js +45 -0
- package/scripts/gemini_image_core.py +470 -0
- package/scripts/install-python-tools.js +64 -0
- package/server/AGENTS.md +73 -0
- package/server/__tests__/gemini-legacy-thought.test.mjs +88 -0
- package/server/__tests__/runtimePorts.test.mjs +115 -0
- package/server/__tests__/session-delete.test.mjs +166 -0
- package/server/bin/compute-node +70 -0
- package/server/claude-sdk.js +799 -0
- package/server/cli-chat.js +488 -0
- package/server/cli.js +354 -0
- package/server/compute-node.js +439 -0
- package/server/constants/config.js +5 -0
- package/server/cursor-cli.js +340 -0
- package/server/database/auth.db +0 -0
- package/server/database/db.js +1852 -0
- package/server/database/init.sql +194 -0
- package/server/gemini-cli.js +1352 -0
- package/server/index.js +3154 -0
- package/server/load-env.js +71 -0
- package/server/local-gpu.js +860 -0
- package/server/middleware/auth.js +100 -0
- package/server/openai-codex.js +724 -0
- package/server/openrouter.js +890 -0
- package/server/project-token-usage.js +407 -0
- package/server/projects.js +3973 -0
- package/server/routes/agent.js +1272 -0
- package/server/routes/auth.js +128 -0
- package/server/routes/auto-research.js +558 -0
- package/server/routes/cli-auth.js +958 -0
- package/server/routes/codex.js +344 -0
- package/server/routes/commands.js +601 -0
- package/server/routes/compute.js +358 -0
- package/server/routes/cursor.js +795 -0
- package/server/routes/git.js +1348 -0
- package/server/routes/mcp-utils.js +48 -0
- package/server/routes/mcp.js +618 -0
- package/server/routes/news.js +711 -0
- package/server/routes/projects.js +633 -0
- package/server/routes/references.js +338 -0
- package/server/routes/settings.js +285 -0
- package/server/routes/skills.js +914 -0
- package/server/routes/taskmaster.js +2566 -0
- package/server/routes/telemetry.js +89 -0
- package/server/routes/user.js +155 -0
- package/server/scripts/fix_session_names.js +57 -0
- package/server/scripts/research-news/common_words.py +19 -0
- package/server/scripts/research-news/link_keywords.py +298 -0
- package/server/scripts/research-news/scan_existing_notes.py +266 -0
- package/server/scripts/research-news/scoring_utils.py +227 -0
- package/server/scripts/research-news/search_arxiv.py +842 -0
- package/server/scripts/research-news/search_huggingface.py +438 -0
- package/server/scripts/research-news/search_x.py +490 -0
- package/server/scripts/research-news/search_xiaohongshu.py +610 -0
- package/server/taskmaster-templates/README.md +150 -0
- package/server/taskmaster-templates/README.zh-CN.md +150 -0
- package/server/taskmaster-templates/ai-research-dataset.json +230 -0
- package/server/taskmaster-templates/ai-research-method-model.json +230 -0
- package/server/taskmaster-templates/ai-research-position-paper.json +230 -0
- package/server/telemetry.js +121 -0
- package/server/templates/AGENTS.md +103 -0
- package/server/templates/CLAUDE.md +104 -0
- package/server/templates/GEMINI.md +103 -0
- package/server/templates/cursor-project.md +108 -0
- package/server/templates/index.js +37 -0
- package/server/utils/__tests__/sessionTokenUsage.test.js +82 -0
- package/server/utils/auto-research-mailer.js +57 -0
- package/server/utils/cliResolution.js +170 -0
- package/server/utils/commandParser.js +303 -0
- package/server/utils/cursorCommand.js +61 -0
- package/server/utils/geminiApiKey.js +28 -0
- package/server/utils/gitConfig.js +24 -0
- package/server/utils/imageAttachmentFiles.js +35 -0
- package/server/utils/mcp-detector.js +198 -0
- package/server/utils/parsers/bibtex-parser.js +207 -0
- package/server/utils/permissions.js +100 -0
- package/server/utils/runtimePorts.js +212 -0
- package/server/utils/sessionFormatting.js +68 -0
- package/server/utils/sessionIndex.js +100 -0
- package/server/utils/sessionMode.js +95 -0
- package/server/utils/sessionTokenUsage.js +105 -0
- package/server/utils/taskmaster-websocket.js +129 -0
- package/server/utils/zotero-client.js +136 -0
- package/shared/errorClassifier.js +102 -0
- package/shared/geminiThinkingSupport.d.ts +22 -0
- package/shared/geminiThinkingSupport.js +96 -0
- package/shared/geminiThoughtParser.d.ts +8 -0
- package/shared/geminiThoughtParser.js +30 -0
- package/shared/modelConstants.js +139 -0
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import fsSync from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { spawn } from 'child_process';
|
|
6
|
+
import os from 'os';
|
|
7
|
+
import { addProjectManually, getWorkspaceRootFromConfig, setWorkspaceRootInConfig } from '../projects.js';
|
|
8
|
+
import { IS_PLATFORM } from '../constants/config.js';
|
|
9
|
+
|
|
10
|
+
const router = express.Router();
|
|
11
|
+
|
|
12
|
+
function sanitizeGitError(message, token) {
|
|
13
|
+
if (!message || !token) return message;
|
|
14
|
+
return message.replace(new RegExp(token.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), '***');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Default workspace root: ~/dr-claw
|
|
18
|
+
const DEFAULT_WORKSPACES_ROOT = path.join(os.homedir(), 'dr-claw');
|
|
19
|
+
const LEGACY_DEFAULT_WORKSPACES_ROOT = path.join(os.homedir(), 'vibelab');
|
|
20
|
+
|
|
21
|
+
// Dynamic workspace root: config file > env var > ~/dr-claw
|
|
22
|
+
export async function getWorkspacesRoot() {
|
|
23
|
+
const configRoot = await getWorkspaceRootFromConfig();
|
|
24
|
+
return configRoot || process.env.WORKSPACES_ROOT || DEFAULT_WORKSPACES_ROOT;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Keep a synchronous fallback for backward compat (used only at import time)
|
|
28
|
+
export const WORKSPACES_ROOT =
|
|
29
|
+
process.env.WORKSPACES_ROOT || (fsSync.existsSync(DEFAULT_WORKSPACES_ROOT) ? DEFAULT_WORKSPACES_ROOT : LEGACY_DEFAULT_WORKSPACES_ROOT);
|
|
30
|
+
|
|
31
|
+
// System-critical paths that should never be used as workspace directories
|
|
32
|
+
export const FORBIDDEN_PATHS = [
|
|
33
|
+
// Unix
|
|
34
|
+
'/',
|
|
35
|
+
'/etc',
|
|
36
|
+
'/bin',
|
|
37
|
+
'/sbin',
|
|
38
|
+
'/usr',
|
|
39
|
+
'/dev',
|
|
40
|
+
'/proc',
|
|
41
|
+
'/sys',
|
|
42
|
+
'/var',
|
|
43
|
+
'/boot',
|
|
44
|
+
'/root',
|
|
45
|
+
'/lib',
|
|
46
|
+
'/lib64',
|
|
47
|
+
'/opt',
|
|
48
|
+
'/tmp',
|
|
49
|
+
'/run',
|
|
50
|
+
// Windows
|
|
51
|
+
'C:\\Windows',
|
|
52
|
+
'C:\\Program Files',
|
|
53
|
+
'C:\\Program Files (x86)',
|
|
54
|
+
'C:\\ProgramData',
|
|
55
|
+
'C:\\System Volume Information',
|
|
56
|
+
'C:\\$Recycle.Bin'
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Validates that a path is safe for workspace operations
|
|
61
|
+
* @param {string} requestedPath - The path to validate
|
|
62
|
+
* @returns {Promise<{valid: boolean, resolvedPath?: string, error?: string}>}
|
|
63
|
+
*/
|
|
64
|
+
export async function validateWorkspacePath(requestedPath) {
|
|
65
|
+
try {
|
|
66
|
+
// Resolve to absolute path
|
|
67
|
+
let absolutePath = path.resolve(requestedPath);
|
|
68
|
+
|
|
69
|
+
// Check if path is a forbidden system directory
|
|
70
|
+
const normalizedPath = path.normalize(absolutePath);
|
|
71
|
+
if (FORBIDDEN_PATHS.includes(normalizedPath) || normalizedPath === '/') {
|
|
72
|
+
return {
|
|
73
|
+
valid: false,
|
|
74
|
+
error: 'Cannot use system-critical directories as workspace locations'
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Additional check for paths starting with forbidden directories
|
|
79
|
+
for (const forbidden of FORBIDDEN_PATHS) {
|
|
80
|
+
if (normalizedPath === forbidden ||
|
|
81
|
+
normalizedPath.startsWith(forbidden + path.sep)) {
|
|
82
|
+
// Exception: /var/tmp and similar user-accessible paths might be allowed
|
|
83
|
+
// but /var itself and most /var subdirectories should be blocked
|
|
84
|
+
if (forbidden === '/var' &&
|
|
85
|
+
(normalizedPath.startsWith('/var/tmp') ||
|
|
86
|
+
normalizedPath.startsWith('/var/folders'))) {
|
|
87
|
+
continue; // Allow these specific cases
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
valid: false,
|
|
92
|
+
error: `Cannot create workspace in system directory: ${forbidden}`
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Try to resolve the real path (following symlinks)
|
|
98
|
+
let realPath;
|
|
99
|
+
try {
|
|
100
|
+
// Check if path exists to resolve real path
|
|
101
|
+
await fs.access(absolutePath);
|
|
102
|
+
realPath = await fs.realpath(absolutePath);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (error.code === 'ENOENT') {
|
|
105
|
+
// Path doesn't exist yet - check parent directory
|
|
106
|
+
let parentPath = path.dirname(absolutePath);
|
|
107
|
+
try {
|
|
108
|
+
const parentRealPath = await fs.realpath(parentPath);
|
|
109
|
+
|
|
110
|
+
// Reconstruct the full path with real parent
|
|
111
|
+
realPath = path.join(parentRealPath, path.basename(absolutePath));
|
|
112
|
+
} catch (parentError) {
|
|
113
|
+
if (parentError.code === 'ENOENT') {
|
|
114
|
+
// Parent doesn't exist either - use the absolute path as-is
|
|
115
|
+
realPath = absolutePath;
|
|
116
|
+
} else {
|
|
117
|
+
throw parentError;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// In OSS mode, custom paths under the user's home directory remain valid even if
|
|
126
|
+
// the default suggested storage root is narrower (for example ~/dr-claw).
|
|
127
|
+
const currentWorkspacesRoot = await getWorkspacesRoot();
|
|
128
|
+
const resolvedWorkspaceRoot = await fs.realpath(currentWorkspacesRoot);
|
|
129
|
+
const resolvedUserHome = await fs.realpath(os.homedir());
|
|
130
|
+
const allowedRoots = IS_PLATFORM ? [resolvedWorkspaceRoot] : [resolvedWorkspaceRoot, resolvedUserHome];
|
|
131
|
+
|
|
132
|
+
const isWithinAllowedRoot = allowedRoots.some((allowedRoot) => (
|
|
133
|
+
realPath.startsWith(allowedRoot + path.sep) || realPath === allowedRoot
|
|
134
|
+
));
|
|
135
|
+
|
|
136
|
+
// Ensure the resolved path is contained within the allowed workspace root
|
|
137
|
+
if (!isWithinAllowedRoot) {
|
|
138
|
+
return {
|
|
139
|
+
valid: false,
|
|
140
|
+
error: IS_PLATFORM
|
|
141
|
+
? `Workspace path must be within the allowed workspace root: ${currentWorkspacesRoot}`
|
|
142
|
+
: `Workspace path must be within your home directory or the configured workspace root: ${currentWorkspacesRoot}`
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Additional symlink check for existing paths
|
|
147
|
+
try {
|
|
148
|
+
await fs.access(absolutePath);
|
|
149
|
+
const stats = await fs.lstat(absolutePath);
|
|
150
|
+
|
|
151
|
+
if (stats.isSymbolicLink()) {
|
|
152
|
+
// Resolve target
|
|
153
|
+
const linkTarget = await fs.readlink(absolutePath);
|
|
154
|
+
const resolvedTarget = path.resolve(path.dirname(absolutePath), linkTarget);
|
|
155
|
+
const realTarget = await fs.realpath(resolvedTarget);
|
|
156
|
+
|
|
157
|
+
const symlinkWithinAllowedRoot = allowedRoots.some((allowedRoot) => (
|
|
158
|
+
realTarget.startsWith(allowedRoot + path.sep) || realTarget === allowedRoot
|
|
159
|
+
));
|
|
160
|
+
|
|
161
|
+
if (!symlinkWithinAllowedRoot) {
|
|
162
|
+
return {
|
|
163
|
+
valid: false,
|
|
164
|
+
error: 'Symlink target is outside the allowed workspace root'
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error.code !== 'ENOENT') {
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
// Path doesn't exist - that's fine for new workspace creation
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
valid: true,
|
|
177
|
+
resolvedPath: realPath
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
} catch (error) {
|
|
181
|
+
return {
|
|
182
|
+
valid: false,
|
|
183
|
+
error: `Path validation failed: ${error.message}`
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get current workspace root path
|
|
190
|
+
* GET /api/projects/workspace-root
|
|
191
|
+
*/
|
|
192
|
+
router.get('/workspace-root', async (req, res) => {
|
|
193
|
+
try {
|
|
194
|
+
const currentRoot = await getWorkspacesRoot();
|
|
195
|
+
const defaultRoot = process.env.WORKSPACES_ROOT || DEFAULT_WORKSPACES_ROOT;
|
|
196
|
+
res.json({ path: currentRoot, defaultPath: defaultRoot });
|
|
197
|
+
} catch (error) {
|
|
198
|
+
console.error('Error getting workspace root:', error);
|
|
199
|
+
res.status(500).json({ error: 'Failed to get workspace root' });
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Set workspace root path
|
|
205
|
+
* PUT /api/projects/workspace-root
|
|
206
|
+
*/
|
|
207
|
+
router.put('/workspace-root', async (req, res) => {
|
|
208
|
+
try {
|
|
209
|
+
const { path: newPath } = req.body;
|
|
210
|
+
|
|
211
|
+
// If null/empty, reset to default
|
|
212
|
+
if (!newPath) {
|
|
213
|
+
await setWorkspaceRootInConfig(null);
|
|
214
|
+
const defaultRoot = process.env.WORKSPACES_ROOT || DEFAULT_WORKSPACES_ROOT;
|
|
215
|
+
return res.json({ success: true, path: defaultRoot });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const absolutePath = path.resolve(newPath);
|
|
219
|
+
|
|
220
|
+
// Validate the path exists and is a directory
|
|
221
|
+
try {
|
|
222
|
+
const stats = await fs.stat(absolutePath);
|
|
223
|
+
if (!stats.isDirectory()) {
|
|
224
|
+
return res.status(400).json({ error: 'Path is not a directory' });
|
|
225
|
+
}
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (error.code === 'ENOENT') {
|
|
228
|
+
return res.status(400).json({ error: 'Directory does not exist' });
|
|
229
|
+
}
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Check it's not a forbidden system path
|
|
234
|
+
const normalizedPath = path.normalize(absolutePath);
|
|
235
|
+
if (FORBIDDEN_PATHS.includes(normalizedPath) || normalizedPath === '/') {
|
|
236
|
+
return res.status(400).json({ error: 'Cannot use system-critical directories' });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
await setWorkspaceRootInConfig(absolutePath);
|
|
240
|
+
res.json({ success: true, path: absolutePath });
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error('Error setting workspace root:', error);
|
|
243
|
+
res.status(500).json({ error: 'Failed to set workspace root' });
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Create a new workspace
|
|
249
|
+
* POST /api/projects/create-workspace
|
|
250
|
+
*
|
|
251
|
+
* Body:
|
|
252
|
+
* - workspaceType: 'existing' | 'new'
|
|
253
|
+
* - path: string (workspace path)
|
|
254
|
+
* - githubUrl?: string (optional, for new workspaces)
|
|
255
|
+
* - githubTokenId?: number (optional, ID of stored token)
|
|
256
|
+
* - newGithubToken?: string (optional, one-time token)
|
|
257
|
+
*/
|
|
258
|
+
router.post('/create-workspace', async (req, res) => {
|
|
259
|
+
try {
|
|
260
|
+
const { workspaceType, path: workspacePath, githubUrl, githubTokenId, newGithubToken, displayName } = req.body;
|
|
261
|
+
|
|
262
|
+
// Validate required fields
|
|
263
|
+
if (!workspaceType || !workspacePath) {
|
|
264
|
+
return res.status(400).json({ error: 'workspaceType and path are required' });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (!['existing', 'new'].includes(workspaceType)) {
|
|
268
|
+
return res.status(400).json({ error: 'workspaceType must be "existing" or "new"' });
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Validate path safety before any operations
|
|
272
|
+
const validation = await validateWorkspacePath(workspacePath);
|
|
273
|
+
if (!validation.valid) {
|
|
274
|
+
return res.status(400).json({
|
|
275
|
+
error: 'Invalid workspace path',
|
|
276
|
+
details: validation.error
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const absolutePath = validation.resolvedPath;
|
|
281
|
+
|
|
282
|
+
// Handle existing workspace
|
|
283
|
+
if (workspaceType === 'existing') {
|
|
284
|
+
// Check if the path exists
|
|
285
|
+
try {
|
|
286
|
+
await fs.access(absolutePath);
|
|
287
|
+
const stats = await fs.stat(absolutePath);
|
|
288
|
+
|
|
289
|
+
if (!stats.isDirectory()) {
|
|
290
|
+
return res.status(400).json({ error: 'Path exists but is not a directory' });
|
|
291
|
+
}
|
|
292
|
+
} catch (error) {
|
|
293
|
+
if (error.code === 'ENOENT') {
|
|
294
|
+
return res.status(404).json({ error: 'Workspace path does not exist' });
|
|
295
|
+
}
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Add the existing workspace to the project list
|
|
300
|
+
const project = await addProjectManually(absolutePath, displayName, req.user?.id);
|
|
301
|
+
|
|
302
|
+
return res.json({
|
|
303
|
+
success: true,
|
|
304
|
+
project,
|
|
305
|
+
message: 'Existing workspace added successfully'
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Handle new workspace creation
|
|
310
|
+
if (workspaceType === 'new') {
|
|
311
|
+
// Create the directory if it doesn't exist
|
|
312
|
+
await fs.mkdir(absolutePath, { recursive: true });
|
|
313
|
+
|
|
314
|
+
// If GitHub URL is provided, clone the repository
|
|
315
|
+
if (githubUrl) {
|
|
316
|
+
let githubToken = null;
|
|
317
|
+
|
|
318
|
+
// Get GitHub token if needed
|
|
319
|
+
if (githubTokenId) {
|
|
320
|
+
// Fetch token from database
|
|
321
|
+
const token = await getGithubTokenById(githubTokenId, req.user.id);
|
|
322
|
+
if (!token) {
|
|
323
|
+
// Clean up created directory
|
|
324
|
+
await fs.rm(absolutePath, { recursive: true, force: true });
|
|
325
|
+
return res.status(404).json({ error: 'GitHub token not found' });
|
|
326
|
+
}
|
|
327
|
+
githubToken = token.github_token;
|
|
328
|
+
} else if (newGithubToken) {
|
|
329
|
+
githubToken = newGithubToken;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Extract repo name from URL for the clone destination
|
|
333
|
+
const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
|
|
334
|
+
const repoName = normalizedUrl.split('/').pop() || 'repository';
|
|
335
|
+
const clonePath = path.join(absolutePath, repoName);
|
|
336
|
+
|
|
337
|
+
// Check if clone destination already exists to prevent data loss
|
|
338
|
+
try {
|
|
339
|
+
await fs.access(clonePath);
|
|
340
|
+
return res.status(409).json({
|
|
341
|
+
error: 'Directory already exists',
|
|
342
|
+
details: `The destination path "${clonePath}" already exists. Please choose a different location or remove the existing directory.`
|
|
343
|
+
});
|
|
344
|
+
} catch (err) {
|
|
345
|
+
// Directory doesn't exist, which is what we want
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Clone the repository into a subfolder
|
|
349
|
+
try {
|
|
350
|
+
await cloneGitHubRepository(githubUrl, clonePath, githubToken);
|
|
351
|
+
} catch (error) {
|
|
352
|
+
// Only clean up if clone created partial data (check if dir exists and is empty or partial)
|
|
353
|
+
try {
|
|
354
|
+
const stats = await fs.stat(clonePath);
|
|
355
|
+
if (stats.isDirectory()) {
|
|
356
|
+
await fs.rm(clonePath, { recursive: true, force: true });
|
|
357
|
+
}
|
|
358
|
+
} catch (cleanupError) {
|
|
359
|
+
// Directory doesn't exist or cleanup failed - ignore
|
|
360
|
+
}
|
|
361
|
+
throw new Error(`Failed to clone repository: ${error.message}`);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Add the cloned repo path to the project list
|
|
365
|
+
const project = await addProjectManually(clonePath, displayName, req.user?.id);
|
|
366
|
+
|
|
367
|
+
return res.json({
|
|
368
|
+
success: true,
|
|
369
|
+
project,
|
|
370
|
+
message: 'New workspace created and repository cloned successfully'
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Add the new workspace to the project list (no clone)
|
|
375
|
+
const project = await addProjectManually(absolutePath, displayName, req.user?.id);
|
|
376
|
+
|
|
377
|
+
return res.json({
|
|
378
|
+
success: true,
|
|
379
|
+
project,
|
|
380
|
+
message: 'New workspace created successfully'
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
} catch (error) {
|
|
385
|
+
console.error('Error creating workspace:', error);
|
|
386
|
+
res.status(500).json({
|
|
387
|
+
error: error.message || 'Failed to create workspace',
|
|
388
|
+
details: process.env.NODE_ENV === 'development' ? error.stack : undefined
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Helper function to get GitHub token from database
|
|
395
|
+
*/
|
|
396
|
+
async function getGithubTokenById(tokenId, userId) {
|
|
397
|
+
const { getDatabase } = await import('../database/db.js');
|
|
398
|
+
const db = await getDatabase();
|
|
399
|
+
|
|
400
|
+
const credential = await db.get(
|
|
401
|
+
'SELECT * FROM user_credentials WHERE id = ? AND user_id = ? AND credential_type = ? AND is_active = 1',
|
|
402
|
+
[tokenId, userId, 'github_token']
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
// Return in the expected format (github_token field for compatibility)
|
|
406
|
+
if (credential) {
|
|
407
|
+
return {
|
|
408
|
+
...credential,
|
|
409
|
+
github_token: credential.credential_value
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return null;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Clone repository with progress streaming (SSE)
|
|
418
|
+
* GET /api/projects/clone-progress
|
|
419
|
+
*/
|
|
420
|
+
router.get('/clone-progress', async (req, res) => {
|
|
421
|
+
const { path: workspacePath, githubUrl, githubTokenId, newGithubToken } = req.query;
|
|
422
|
+
|
|
423
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
424
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
425
|
+
res.setHeader('Connection', 'keep-alive');
|
|
426
|
+
res.flushHeaders();
|
|
427
|
+
|
|
428
|
+
const sendEvent = (type, data) => {
|
|
429
|
+
res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
try {
|
|
433
|
+
if (!workspacePath || !githubUrl) {
|
|
434
|
+
sendEvent('error', { message: 'workspacePath and githubUrl are required' });
|
|
435
|
+
res.end();
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const validation = await validateWorkspacePath(workspacePath);
|
|
440
|
+
if (!validation.valid) {
|
|
441
|
+
sendEvent('error', { message: validation.error });
|
|
442
|
+
res.end();
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const absolutePath = validation.resolvedPath;
|
|
447
|
+
|
|
448
|
+
await fs.mkdir(absolutePath, { recursive: true });
|
|
449
|
+
|
|
450
|
+
let githubToken = null;
|
|
451
|
+
if (githubTokenId) {
|
|
452
|
+
const token = await getGithubTokenById(parseInt(githubTokenId), req.user.id);
|
|
453
|
+
if (!token) {
|
|
454
|
+
await fs.rm(absolutePath, { recursive: true, force: true });
|
|
455
|
+
sendEvent('error', { message: 'GitHub token not found' });
|
|
456
|
+
res.end();
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
githubToken = token.github_token;
|
|
460
|
+
} else if (newGithubToken) {
|
|
461
|
+
githubToken = newGithubToken;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const normalizedUrl = githubUrl.replace(/\/+$/, '').replace(/\.git$/, '');
|
|
465
|
+
const repoName = normalizedUrl.split('/').pop() || 'repository';
|
|
466
|
+
const clonePath = path.join(absolutePath, repoName);
|
|
467
|
+
|
|
468
|
+
// Check if clone destination already exists to prevent data loss
|
|
469
|
+
try {
|
|
470
|
+
await fs.access(clonePath);
|
|
471
|
+
sendEvent('error', { message: `Directory "${repoName}" already exists. Please choose a different location or remove the existing directory.` });
|
|
472
|
+
res.end();
|
|
473
|
+
return;
|
|
474
|
+
} catch (err) {
|
|
475
|
+
// Directory doesn't exist, which is what we want
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
let cloneUrl = githubUrl;
|
|
479
|
+
if (githubToken) {
|
|
480
|
+
try {
|
|
481
|
+
const url = new URL(githubUrl);
|
|
482
|
+
url.username = githubToken;
|
|
483
|
+
url.password = '';
|
|
484
|
+
cloneUrl = url.toString();
|
|
485
|
+
} catch (error) {
|
|
486
|
+
// SSH URL or invalid - use as-is
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
sendEvent('progress', { message: `Cloning into '${repoName}'...` });
|
|
491
|
+
|
|
492
|
+
const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, clonePath], {
|
|
493
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
494
|
+
env: {
|
|
495
|
+
...process.env,
|
|
496
|
+
GIT_TERMINAL_PROMPT: '0'
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
let lastError = '';
|
|
501
|
+
|
|
502
|
+
gitProcess.stdout.on('data', (data) => {
|
|
503
|
+
const message = data.toString().trim();
|
|
504
|
+
if (message) {
|
|
505
|
+
sendEvent('progress', { message });
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
gitProcess.stderr.on('data', (data) => {
|
|
510
|
+
const message = data.toString().trim();
|
|
511
|
+
lastError = message;
|
|
512
|
+
if (message) {
|
|
513
|
+
sendEvent('progress', { message });
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
gitProcess.on('close', async (code) => {
|
|
518
|
+
if (code === 0) {
|
|
519
|
+
try {
|
|
520
|
+
const project = await addProjectManually(clonePath);
|
|
521
|
+
sendEvent('complete', { project, message: 'Repository cloned successfully' });
|
|
522
|
+
} catch (error) {
|
|
523
|
+
sendEvent('error', { message: `Clone succeeded but failed to add project: ${error.message}` });
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
const sanitizedError = sanitizeGitError(lastError, githubToken);
|
|
527
|
+
let errorMessage = 'Git clone failed';
|
|
528
|
+
if (lastError.includes('Authentication failed') || lastError.includes('could not read Username')) {
|
|
529
|
+
errorMessage = 'Authentication failed. Please check your credentials.';
|
|
530
|
+
} else if (lastError.includes('Repository not found')) {
|
|
531
|
+
errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
|
|
532
|
+
} else if (lastError.includes('already exists')) {
|
|
533
|
+
errorMessage = 'Directory already exists';
|
|
534
|
+
} else if (sanitizedError) {
|
|
535
|
+
errorMessage = sanitizedError;
|
|
536
|
+
}
|
|
537
|
+
try {
|
|
538
|
+
await fs.rm(clonePath, { recursive: true, force: true });
|
|
539
|
+
} catch (cleanupError) {
|
|
540
|
+
console.error('Failed to clean up after clone failure:', sanitizeGitError(cleanupError.message, githubToken));
|
|
541
|
+
}
|
|
542
|
+
sendEvent('error', { message: errorMessage });
|
|
543
|
+
}
|
|
544
|
+
res.end();
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
gitProcess.on('error', (error) => {
|
|
548
|
+
if (error.code === 'ENOENT') {
|
|
549
|
+
sendEvent('error', { message: 'Git is not installed or not in PATH' });
|
|
550
|
+
} else {
|
|
551
|
+
sendEvent('error', { message: error.message });
|
|
552
|
+
}
|
|
553
|
+
res.end();
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
req.on('close', () => {
|
|
557
|
+
gitProcess.kill();
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
} catch (error) {
|
|
561
|
+
sendEvent('error', { message: error.message });
|
|
562
|
+
res.end();
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Helper function to clone a GitHub repository
|
|
568
|
+
*/
|
|
569
|
+
function cloneGitHubRepository(githubUrl, destinationPath, githubToken = null) {
|
|
570
|
+
return new Promise((resolve, reject) => {
|
|
571
|
+
let cloneUrl = githubUrl;
|
|
572
|
+
|
|
573
|
+
if (githubToken) {
|
|
574
|
+
try {
|
|
575
|
+
const url = new URL(githubUrl);
|
|
576
|
+
url.username = githubToken;
|
|
577
|
+
url.password = '';
|
|
578
|
+
cloneUrl = url.toString();
|
|
579
|
+
} catch (error) {
|
|
580
|
+
// SSH URL - use as-is
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const gitProcess = spawn('git', ['clone', '--progress', cloneUrl, destinationPath], {
|
|
585
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
586
|
+
env: {
|
|
587
|
+
...process.env,
|
|
588
|
+
GIT_TERMINAL_PROMPT: '0'
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
let stdout = '';
|
|
593
|
+
let stderr = '';
|
|
594
|
+
|
|
595
|
+
gitProcess.stdout.on('data', (data) => {
|
|
596
|
+
stdout += data.toString();
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
gitProcess.stderr.on('data', (data) => {
|
|
600
|
+
stderr += data.toString();
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
gitProcess.on('close', (code) => {
|
|
604
|
+
if (code === 0) {
|
|
605
|
+
resolve({ stdout, stderr });
|
|
606
|
+
} else {
|
|
607
|
+
let errorMessage = 'Git clone failed';
|
|
608
|
+
|
|
609
|
+
if (stderr.includes('Authentication failed') || stderr.includes('could not read Username')) {
|
|
610
|
+
errorMessage = 'Authentication failed. Please check your GitHub token.';
|
|
611
|
+
} else if (stderr.includes('Repository not found')) {
|
|
612
|
+
errorMessage = 'Repository not found. Please check the URL and ensure you have access.';
|
|
613
|
+
} else if (stderr.includes('already exists')) {
|
|
614
|
+
errorMessage = 'Directory already exists';
|
|
615
|
+
} else if (stderr) {
|
|
616
|
+
errorMessage = stderr;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
reject(new Error(errorMessage));
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
gitProcess.on('error', (error) => {
|
|
624
|
+
if (error.code === 'ENOENT') {
|
|
625
|
+
reject(new Error('Git is not installed or not in PATH'));
|
|
626
|
+
} else {
|
|
627
|
+
reject(error);
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export default router;
|