nuwax-file-server 1.2.9 → 1.3.0-beta.11
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/dist/appConfig/index.js +1 -1
- package/dist/cli.js +1 -1
- package/dist/env.development +13 -1
- package/dist/env.production +13 -1
- package/dist/routes/codeRoutes.js +1 -1
- package/dist/routes/computerRoutes.js +1 -1
- package/dist/routes/gitRoutes.js +1 -0
- package/dist/routes/projectRoutes.js +1 -1
- package/dist/routes/router.js +1 -1
- package/dist/service/codeService.js +2 -2
- package/dist/service/gitService.js +4 -0
- package/dist/service/projectService.js +1 -1
- package/dist/utils/build/restartDevUtils.js +1 -1
- package/dist/utils/build/startDevUtils.js +1 -1
- package/dist/utils/buildDependency/dependencyManager.js +16 -16
- package/dist/utils/common/npmrcUtils.js +6 -5
- package/dist/utils/common/templateCacheUtils.js +7 -0
- package/dist/utils/computer/computerFileUtils.js +1 -1
- package/dist/utils/computer/computerUtils.js +1 -1
- package/dist/utils/git/gitUtils.js +6 -0
- package/dist/utils/project/copyProjectUtils.js +1 -1
- package/dist/utils/project/getContentUtils.js +1 -1
- package/package.json +2 -1
package/dist/appConfig/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import E from"path";import n from"fs";import o from"os";import{fileURLToPath as
|
|
1
|
+
import E from"path";import n from"fs";import o from"os";import{fileURLToPath as T}from"url";import I from"dotenv";const L=E.dirname(T(import.meta.url));I.config();function O(){const e=process.env.LOG_BASE_DIR,_=E.join(o.tmpdir(),"nuwax-file-server","project_logs");if(!e)return _;try{return n.mkdirSync(e,{recursive:!0}),e}catch{try{n.mkdirSync(_,{recursive:!0})}catch{}return _}}const r=process.env.NODE_ENV||"development";function p(e){const _=E.join(L,"..",`env.${e}`);if(n.existsSync(_))I.config({path:_}),console.log(`Environment configuration file env.${e} loaded`);else{const s=`Environment configuration file env.${e} does not exist, please create the corresponding environment configuration file and try again`;throw console.error(s),new Error(s)}}p(r);const R={NODE_ENV:r,PORT:parseInt(process.env.PORT),DEPLOYMENT_MODE:process.env.DEPLOYMENT_MODE||"docker-compose",INIT_PROJECT_NAME_REACT:process.env.INIT_PROJECT_NAME_REACT||"react-vite-template",INIT_PROJECT_NAME_VUE3:process.env.INIT_PROJECT_NAME_VUE3||"vue3-vite-template",INIT_PROJECT_DIR:process.env.INIT_PROJECT_DIR,PROJECT_SOURCE_DIR:process.env.PROJECT_SOURCE_DIR,DIST_TARGET_DIR:process.env.DIST_TARGET_DIR,UPLOAD_PROJECT_DIR:process.env.UPLOAD_PROJECT_DIR,MAX_BUILD_CONCURRENCY:process.env.MAX_BUILD_CONCURRENCY?parseInt(process.env.MAX_BUILD_CONCURRENCY,10):void 0,MAX_INLINE_FILE_SIZE_BYTES:process.env.MAX_INLINE_FILE_SIZE_BYTES?parseInt(process.env.MAX_INLINE_FILE_SIZE_BYTES,10):void 0,UPLOAD_MAX_FILE_SIZE_BYTES:process.env.UPLOAD_MAX_FILE_SIZE_BYTES?parseInt(process.env.UPLOAD_MAX_FILE_SIZE_BYTES,10):void 0,UPLOAD_ALLOWED_EXTENSIONS:process.env.UPLOAD_ALLOWED_EXTENSIONS?process.env.UPLOAD_ALLOWED_EXTENSIONS.split(",").map(e=>e.trim().toLowerCase()).filter(Boolean):[],UPLOAD_SINGLE_FILE_SIZE_BYTES:process.env.UPLOAD_SINGLE_FILE_SIZE_BYTES?parseInt(process.env.UPLOAD_SINGLE_FILE_SIZE_BYTES,10):void 0,DOWNLOAD_MAX_FILE_SIZE_BYTES:process.env.DOWNLOAD_MAX_FILE_SIZE_BYTES?parseInt(process.env.DOWNLOAD_MAX_FILE_SIZE_BYTES,10):void 0,REQUEST_BODY_LIMIT:process.env.REQUEST_BODY_LIMIT,TRAVERSE_EXCLUDE_DIRS:process.env.TRAVERSE_EXCLUDE_DIRS?process.env.TRAVERSE_EXCLUDE_DIRS.split(",").map(e=>e.trim()).filter(Boolean):[],BACKUP_TRAVERSE_EXCLUDE_FILES:process.env.BACKUP_TRAVERSE_EXCLUDE_FILES?process.env.BACKUP_TRAVERSE_EXCLUDE_FILES.split(",").map(e=>e.trim()).filter(Boolean):[],CONTENT_TRAVERSE_EXCLUDE_FILES:process.env.CONTENT_TRAVERSE_EXCLUDE_FILES?process.env.CONTENT_TRAVERSE_EXCLUDE_FILES.split(",").map(e=>e.trim()).filter(Boolean):[],INLINE_IMAGE_EXTENSIONS:process.env.INLINE_IMAGE_EXTENSIONS?process.env.INLINE_IMAGE_EXTENSIONS.split(",").map(e=>e.trim().toLowerCase()).filter(Boolean):[],TOP_LEVEL_NOISE_PATTERNS:process.env.TOP_LEVEL_NOISE_PATTERNS?process.env.TOP_LEVEL_NOISE_PATTERNS.split(",").map(e=>e.trim()).filter(Boolean):[],LOG_BASE_DIR:O(),LOG_LEVEL:process.env.LOG_LEVEL?process.env.LOG_LEVEL.toLowerCase():void 0,LOG_PREFIX_API:process.env.LOG_PREFIX_API,LOG_PREFIX_BUILD:process.env.LOG_PREFIX_BUILD,LOG_CONSOLE_ENABLED:typeof process.env.LOG_CONSOLE_ENABLED=="string"?process.env.LOG_CONSOLE_ENABLED.toLowerCase()==="true":void 0,LOG_CACHE_ENABLED:typeof process.env.LOG_CACHE_ENABLED=="string"?process.env.LOG_CACHE_ENABLED.toLowerCase()==="true":void 0,LOG_CACHE_DURATION:process.env.LOG_CACHE_DURATION?parseInt(process.env.LOG_CACHE_DURATION,10):void 0,LOG_CACHE_MAX_ENTRIES:process.env.LOG_CACHE_MAX_ENTRIES?parseInt(process.env.LOG_CACHE_MAX_ENTRIES,10):void 0,LOG_CACHE_MAX_FILE_SIZE:process.env.LOG_CACHE_MAX_FILE_SIZE?parseInt(process.env.LOG_CACHE_MAX_FILE_SIZE,10):void 0,DEV_SERVER_PORT_TIMEOUT:process.env.DEV_SERVER_PORT_TIMEOUT?parseInt(process.env.DEV_SERVER_PORT_TIMEOUT,10):void 0,DEV_SERVER_STOP_TIMEOUT:process.env.DEV_SERVER_STOP_TIMEOUT?parseInt(process.env.DEV_SERVER_STOP_TIMEOUT,10):void 0,DEV_SERVER_STOP_CHECK_INTERVAL:process.env.DEV_SERVER_STOP_CHECK_INTERVAL?parseInt(process.env.DEV_SERVER_STOP_CHECK_INTERVAL,10):void 0,DEV_SERVER_STOP_MAX_ATTEMPTS:process.env.DEV_SERVER_STOP_MAX_ATTEMPTS?parseInt(process.env.DEV_SERVER_STOP_MAX_ATTEMPTS,10):void 0,COMPUTER_WORKSPACE_DIR:process.env.COMPUTER_WORKSPACE_DIR,COMPUTER_LOG_DIR:process.env.COMPUTER_LOG_DIR,CLI_SERVICE_NAME:"nuwax-file-server",CLI_PID_DIR:process.env.CLI_PID_DIR||(process.platform==="win32"?E.join(process.env.TEMP||"","nuwax-file-server"):E.join("/tmp","nuwax-file-server")),CLI_PID_FILE:"server.pid",CLI_STOP_TIMEOUT:process.env.CLI_STOP_TIMEOUT?parseInt(process.env.CLI_STOP_TIMEOUT,10):3e4,CLI_CHECK_INTERVAL:process.env.CLI_CHECK_INTERVAL?parseInt(process.env.CLI_CHECK_INTERVAL,10):500,CLI_LOG_DIR:process.env.CLI_LOG_DIR||(process.platform==="win32"?E.join(process.env.TEMP||"","nuwax-file-server","logs"):E.join("/tmp","nuwax-file-server","logs")),CLI_IS_WINDOWS:process.platform==="win32",GIT_DEFAULT_AUTHOR_NAME:process.env.GIT_DEFAULT_AUTHOR_NAME||"Nuwax File Server",GIT_DEFAULT_AUTHOR_EMAIL:process.env.GIT_DEFAULT_AUTHOR_EMAIL||"git@nuwax.local",GIT_AUTO_GITIGNORE:process.env.GIT_AUTO_GITIGNORE?.toLowerCase()!=="false",GIT_GITIGNORE_ENTRIES:process.env.GIT_GITIGNORE_ENTRIES?process.env.GIT_GITIGNORE_ENTRIES.split("|").map(e=>e.trim()).filter(Boolean):["node_modules/",".pnpm-store/","dist/","build/",".idea/",".vscode/",".DS_Store",".agents/",".claude/",".opencode/",".codex/"],GIT_ENABLED:process.env.GIT_ENABLED?.toLowerCase()==="true"};export default R;
|
package/dist/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as ee}from"commander";import{createRequire as te}from"module";import f from"path";import U from"os";import c from"fs-extra";import{spawn as E}from"cross-spawn";import V from"tree-kill";import{fileURLToPath as q}from"url";import{execFileSync as R}from"child_process";import M from"http";import{createRequire as G}from"module";var I=f.dirname(q(import.meta.url)),H=G(import.meta.url),n={name:"nuwax-file-server",pidDir:f.join(U.tmpdir(),"nuwax-file-server"),pidFileName:"server.pid",lockFileName:"start.lock",defaultStopTimeout:3e4,defaultStartTimeout:3e4,staleLockTimeout:12e4,checkInterval:500};function $(){return f.join(n.pidDir,n.pidFileName)}function F(){return f.join(n.pidDir,n.lockFileName)}function J(){let e=[f.join(I,"..","server.js"),f.join(I,"server.js")];for(let t of e)if(c.existsSync(t))return t;return e[0]}function W(){let e=["../../package.json","../package.json"];for(let t of e)try{let o=H(t);if(o?.version)return o.version}catch{}return"unknown"}function x(){try{let e=$();if(!c.existsSync(e))return null;let t=c.readFileSync(e,"utf8"),o=JSON.parse(t);return!o||typeof o.pid!="number"?null:o}catch(e){return e.code!=="ENOENT"&&console.error(`Read PID file failed: ${e.message}`),null}}function B(e){try{let t=$();c.ensureDirSync(n.pidDir),c.writeFileSync(t,JSON.stringify(e,null,2)),console.debug(`PID file written: ${t}`)}catch(t){throw console.error(`Write PID file failed: ${t.message}`),t}}function v(){try{let e=$();c.existsSync(e)&&(c.removeSync(e),console.debug(`PID file deleted: ${e}`))}catch(e){console.error(`Delete PID file failed: ${e.message}`)}}function g(e){try{return process.kill(e,0),!0}catch(t){return t.code!=="ESRCH"}}function K(e){try{return k()?R("powershell",["-NoProfile","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${e}").CommandLine`],{encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim():R("ps",["-p",String(e),"-o","command="],{encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim()}catch{return""}}function X(e){if(!g(e))return!1;let t=K(e);if(!t)return!1;let o=t.toLowerCase(),s=f.basename(f.join(I,"..","server.js")).toLowerCase(),i=n.name.toLowerCase();return o.includes(s)||o.includes(i)}function z(){c.ensureDirSync(n.pidDir);let e=F(),t=`${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2,10)}`,o=JSON.stringify({pid:process.pid,token:t,createdAt:new Date().toISOString()},null,2);try{let s=c.openSync(e,"wx");return c.writeSync(s,o),{fd:s,token:t}}catch(s){if(s.code!=="EEXIST")throw s;try{let i=c.readFileSync(e,"utf8"),r=JSON.parse(i),a=Number(r?.pid),u=r?.createdAt?new Date(r.createdAt).getTime():0,l=Date.now()-u,C=Number.isFinite(a)&&a>0&&g(a),p=!u||Number.isNaN(l)||l>n.staleLockTimeout;if(!C||p){c.removeSync(e);let S=c.openSync(e,"wx");return c.writeSync(S,o),console.warn("Detected stale start lock, auto cleaned"),{fd:S,token:t}}}catch{c.removeSync(e);let r=c.openSync(e,"wx");return c.writeSync(r,o),console.warn("Detected invalid start lock, auto cleaned"),{fd:r,token:t}}throw s}}function Q(e){let t=e?.fd,o=e?.token;try{t!=null&&c.closeSync(t)}catch{}try{let s=F();if(c.existsSync(s)){let i=!1;if(!o)i=!0;else try{let r=c.readFileSync(s,"utf8");i=JSON.parse(r)?.token===o}catch{i=!0}i&&c.removeSync(s)}}catch{}}function k(){return process.platform==="win32"}async function Y(e=n.defaultStopTimeout){let t=x();return t?X(t.pid)?(console.log(`Existing service process detected (PID: ${t.pid}), stopping before start...`),await h(t.pid,!1)&&await D(t.pid,e)?(v(),{success:!0,message:`Existing process ${t.pid} stopped gracefully`}):(console.warn(`Graceful stop timeout or failed for PID ${t.pid}, force stop...`),await h(t.pid,!0)?await D(t.pid,e)?(v(),{success:!0,message:`Existing process ${t.pid} stopped forcibly`}):{success:!1,message:`Existing process ${t.pid} did not exit after force stop`}:{success:!1,message:`Failed to stop existing process ${t.pid}`})):(console.log(`Found stale PID file (PID: ${t.pid}), clean it...`),v(),{success:!0,message:"Stale PID file cleaned"}):{success:!0,message:"No existing service process"}}async function h(e,t=!1){return new Promise(o=>{if(!g(e)){console.debug(`Process ${e} does not exist, already stopped`),o(!0);return}let s=t?"SIGKILL":"SIGTERM",i=k()?"taskkill":"tree-kill";if(console.debug(`Use ${i} to stop process ${e} (signal: ${s})`),k()){let r=t?["/F","/PID",String(e)]:["/PID",String(e)],a=E("taskkill",r,{stdio:["ignore","pipe","pipe"],windowsHide:!0}),u="";a.stdout.on("data",l=>{u+=l.toString()}),a.stderr.on("data",l=>{u+=l.toString()}),a.on("error",l=>{console.error(`Stop process failed: ${l.message}`),o(!1)}),a.on("close",l=>{l===0?(console.debug(`Process ${e} stopped`),o(!0)):(console.warn(`taskkill exit code: ${l}, output: ${u}`),t?o(!1):h(e,!0).then(o))})}else V(e,s,r=>{r?r.code==="ESRCH"?(console.debug(`Process ${e} does not exist`),o(!0)):(console.error(`Stop process failed: ${r.message}`),o(!1)):(console.debug(`Process ${e} stopped`),o(!0))})})}async function D(e,t=n.defaultStopTimeout){let o=Date.now();for(;g(e);){if(Date.now()-o>t)return console.warn(`Wait for process ${e} to stop timeout (${t}ms)`),!1;await new Promise(i=>setTimeout(i,n.checkInterval))}let s=Date.now()-o;return console.debug(`Process ${e} stopped after ${s}ms`),!0}async function Z(e,t=n.defaultStartTimeout){let o=Number(e);if(!Number.isFinite(o)||o<=0)return!1;let s=Date.now();for(;Date.now()-s<=t;){if(await new Promise(r=>{let a=M.get({host:"127.0.0.1",port:o,path:"/health",timeout:Math.min(2e3,n.checkInterval*4)},u=>{r(u.statusCode>=200&&u.statusCode<300),u.resume()});a.on("timeout",()=>{a.destroy(),r(!1)}),a.on("error",()=>r(!1))}))return!0;await new Promise(r=>setTimeout(r,n.checkInterval))}return!1}async function N(e={}){let{env:t,port:o,config:s}=e,i=null;console.log(`Start service ${n.name}...`);try{i=z()}catch(r){return r.code==="EEXIST"?{success:!1,pid:null,message:"Another start operation is in progress, please retry later"}:{success:!1,pid:null,message:`Acquire start lock failed: ${r.message}`}}try{let r=Number(e.timeout)||n.defaultStopTimeout,a=await Y(r);if(!a.success)return{success:!1,pid:null,message:`Service start blocked: ${a.message}`};let u={...process.env};t&&(u.NODE_ENV=t,console.log(`Environment: ${t}`)),o&&(u.PORT=o,console.log(`Port: ${o}`)),s&&(u.CONFIG_FILE=s,console.log(`Configuration file: ${s}`));let l=J(),p=E("node",[l,...[]],{env:u,stdio:["pipe","pipe","pipe"],detached:!0,cwd:process.cwd()});if(p.stdout.on("data",m=>{process.stdout.write(m)}),p.stderr.on("data",m=>{process.stderr.write(m)}),p.on("error",m=>{console.error(`Start service failed: ${m.message}`)}),await new Promise(m=>setTimeout(m,2e3)),!g(p.pid))return{success:!1,pid:null,message:"Service start failed"};let S={pid:p.pid,startedAt:new Date().toISOString(),env:t||process.env.NODE_ENV||"production",port:o||process.env.PORT||"60000",version:W(),platform:process.platform};B(S);let P=Number(e.startTimeout)||n.defaultStartTimeout;return await Z(S.port,P)?(p.unref(),console.log(`Service started (PID: ${p.pid})`),console.log(`Service address: http://localhost:${S.port}`),{success:!0,pid:p.pid,message:"Service started successfully"}):(console.error(`Service health check timeout (${P}ms), stop failed instance...`),await h(p.pid,!0),v(),{success:!1,pid:null,message:`Service health check timeout (${P}ms)`})}finally{Q(i)}}async function T(e={}){let{force:t=!1,timeout:o=n.defaultStopTimeout}=e;console.log(`Stop service ${n.name}...`);let s=x();if(!s)return{success:!1,message:"Service not found"};if(!g(s.pid))return console.log("Service process has stopped, clean PID file..."),v(),{success:!0,message:"Service has stopped (process has exited)"};if(!await h(s.pid,t))return{success:!1,message:"Stop service failed"};let r=await D(s.pid,o);return v(),r?{success:!0,message:"Service has stopped"}:{success:!1,message:"Service stop timeout"}}async function O(e={}){console.log(`Restart service ${n.name}...`);let t=await T(e);!t.success&&t.message!=="Service not found"&&console.warn(`Stop service failed: ${t.message}`),await new Promise(s=>setTimeout(s,2e3));let o=await N(e);return o.success?{success:!0,pid:o.pid,message:"Service has restarted"}:{success:!1,pid:null,message:`Restart failed: ${o.message}`}}function _(){let e=x();return e?g(e.pid)?{running:!0,pidInfo:e,message:"Service running"}:{running:!1,pidInfo:e,message:"Service process does not exist"}:{running:!1,pidInfo:null,message:"Service not running"}}function L(e){try{let t=new Date(e),o=new Date;if(isNaN(t.getTime()))return"unknown";let s=Math.floor((o-t)/1e3),i=Math.floor(s/3600),r=Math.floor(s%3600/60),a=s%60;return i>0?`${i} hours ${r} minutes ${a} seconds`:r>0?`${r} minutes ${a} seconds`:`${a} seconds`}catch{return"unknown"}}var Pe=te(import.meta.url),A="1.2.9",d=new ee,w={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"};function j(e){console.error(`${w.red}ERROR: ${e}${w.reset}`)}function b(e){console.log(`${w.green}${e}${w.reset}`)}function oe(e){console.log(`${w.blue}${e}${w.reset}`)}function y(e){return async(...t)=>{try{let o=await e(...t);o&&o.success===!1?(j(o.message||"Command failed"),process.exitCode=1):process.exitCode=0}catch(o){j(o?.message||"Command failed with unexpected error"),process.exitCode=1}}}async function se(e){let t=await N({env:e.env,port:e.port,config:e.config,timeout:Number(e.timeout),startTimeout:Number(e.startTimeout)});return t.success&&b(`Service started (PID: ${t.pid})`),t}async function re(e){let t=await T({force:e.force,timeout:Number(e.timeout)});return t.success&&b(t.message||"Service stopped"),t}async function ne(e){let t=await O({env:e.env,port:e.port,config:e.config,timeout:Number(e.timeout),startTimeout:Number(e.startTimeout)});return t.success&&b(t.message||"Service restarted"),t}function ie(){let e=_();return oe(`${n.name} service status:`),console.log(""),console.log(` Service name: ${n.name}`),console.log(` Running status: ${e.running?"Running":"Stopped"}`),console.log(` Message: ${e.message}`),console.log(` PID file: ${$()}`),e.pidInfo&&(console.log(` Process ID: ${e.pidInfo.pid}`),console.log(` Environment: ${e.pidInfo.env||"Unknown"}`),console.log(` Port: ${e.pidInfo.port||"Unknown"}`),console.log(` Version: ${e.pidInfo.version||A}`),console.log(` Platform: ${e.pidInfo.platform||process.platform}`),console.log(` Started at: ${e.pidInfo.startedAt||"Unknown"}`),console.log(` Uptime: ${L(e.pidInfo.startedAt)}`)),console.log(""),{success:e.running,message:e.message}}function ce(){d.name("nuwax-file-server").description("Cross-platform file service deployment tool, supporting start/stop/restart/status").version(A,"-v, --version","Display version number").helpOption("-h, --help","Display help information"),d.command("start").allowUnknownOption().description("Start service").option("--env <environment>","\u73AF\u5883: development|production|test","production").option("--port <port>","Service port").option("--config <path>","Custom configuration file path").option("--timeout <ms>","\u505C\u6B62\u65E7\u8FDB\u7A0B\u7B49\u5F85\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStopTimeout}`).option("--start-timeout <ms>","\u542F\u52A8\u5065\u5EB7\u68C0\u67E5\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStartTimeout}`).action(y(se)),d.command("stop").description("Stop service").option("--force","Force stop").option("--timeout <ms>","\u505C\u6B62\u670D\u52A1\u7B49\u5F85\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStopTimeout}`).action(y(re)),d.command("restart").allowUnknownOption().description("Restart service").option("--env <environment>","\u73AF\u5883: development|production|test","production").option("--port <port>","Service port").option("--config <path>","Custom configuration file path").option("--timeout <ms>","\u505C\u6B62\u65E7\u8FDB\u7A0B\u7B49\u5F85\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStopTimeout}`).option("--start-timeout <ms>","\u542F\u52A8\u5065\u5EB7\u68C0\u67E5\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStartTimeout}`).action(y(ne)),d.command("status").description("View service status").action(y(ie)),d.command("help").description("Display help information").action(()=>{d.outputHelp()}),d.parse(process.argv),process.argv.slice(2).length||(d.outputHelp(),process.exitCode=0)}ce();
|
|
2
|
+
import{Command as ee}from"commander";import{createRequire as te}from"module";import f from"path";import U from"os";import c from"fs-extra";import{spawn as E}from"cross-spawn";import V from"tree-kill";import{fileURLToPath as q}from"url";import{execFileSync as R}from"child_process";import M from"http";import{createRequire as G}from"module";var I=f.dirname(q(import.meta.url)),H=G(import.meta.url),n={name:"nuwax-file-server",pidDir:f.join(U.tmpdir(),"nuwax-file-server"),pidFileName:"server.pid",lockFileName:"start.lock",defaultStopTimeout:3e4,defaultStartTimeout:3e4,staleLockTimeout:12e4,checkInterval:500};function $(){return f.join(n.pidDir,n.pidFileName)}function F(){return f.join(n.pidDir,n.lockFileName)}function J(){let e=[f.join(I,"..","server.js"),f.join(I,"server.js")];for(let t of e)if(c.existsSync(t))return t;return e[0]}function W(){let e=["../../package.json","../package.json"];for(let t of e)try{let o=H(t);if(o?.version)return o.version}catch{}return"unknown"}function x(){try{let e=$();if(!c.existsSync(e))return null;let t=c.readFileSync(e,"utf8"),o=JSON.parse(t);return!o||typeof o.pid!="number"?null:o}catch(e){return e.code!=="ENOENT"&&console.error(`Read PID file failed: ${e.message}`),null}}function B(e){try{let t=$();c.ensureDirSync(n.pidDir),c.writeFileSync(t,JSON.stringify(e,null,2)),console.debug(`PID file written: ${t}`)}catch(t){throw console.error(`Write PID file failed: ${t.message}`),t}}function v(){try{let e=$();c.existsSync(e)&&(c.removeSync(e),console.debug(`PID file deleted: ${e}`))}catch(e){console.error(`Delete PID file failed: ${e.message}`)}}function g(e){try{return process.kill(e,0),!0}catch(t){return t.code!=="ESRCH"}}function K(e){try{return k()?R("powershell",["-NoProfile","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${e}").CommandLine`],{encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim():R("ps",["-p",String(e),"-o","command="],{encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim()}catch{return""}}function X(e){if(!g(e))return!1;let t=K(e);if(!t)return!1;let o=t.toLowerCase(),s=f.basename(f.join(I,"..","server.js")).toLowerCase(),i=n.name.toLowerCase();return o.includes(s)||o.includes(i)}function z(){c.ensureDirSync(n.pidDir);let e=F(),t=`${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2,10)}`,o=JSON.stringify({pid:process.pid,token:t,createdAt:new Date().toISOString()},null,2);try{let s=c.openSync(e,"wx");return c.writeSync(s,o),{fd:s,token:t}}catch(s){if(s.code!=="EEXIST")throw s;try{let i=c.readFileSync(e,"utf8"),r=JSON.parse(i),a=Number(r?.pid),u=r?.createdAt?new Date(r.createdAt).getTime():0,l=Date.now()-u,C=Number.isFinite(a)&&a>0&&g(a),p=!u||Number.isNaN(l)||l>n.staleLockTimeout;if(!C||p){c.removeSync(e);let S=c.openSync(e,"wx");return c.writeSync(S,o),console.warn("Detected stale start lock, auto cleaned"),{fd:S,token:t}}}catch{c.removeSync(e);let r=c.openSync(e,"wx");return c.writeSync(r,o),console.warn("Detected invalid start lock, auto cleaned"),{fd:r,token:t}}throw s}}function Q(e){let t=e?.fd,o=e?.token;try{t!=null&&c.closeSync(t)}catch{}try{let s=F();if(c.existsSync(s)){let i=!1;if(!o)i=!0;else try{let r=c.readFileSync(s,"utf8");i=JSON.parse(r)?.token===o}catch{i=!0}i&&c.removeSync(s)}}catch{}}function k(){return process.platform==="win32"}async function Y(e=n.defaultStopTimeout){let t=x();return t?X(t.pid)?(console.log(`Existing service process detected (PID: ${t.pid}), stopping before start...`),await h(t.pid,!1)&&await D(t.pid,e)?(v(),{success:!0,message:`Existing process ${t.pid} stopped gracefully`}):(console.warn(`Graceful stop timeout or failed for PID ${t.pid}, force stop...`),await h(t.pid,!0)?await D(t.pid,e)?(v(),{success:!0,message:`Existing process ${t.pid} stopped forcibly`}):{success:!1,message:`Existing process ${t.pid} did not exit after force stop`}:{success:!1,message:`Failed to stop existing process ${t.pid}`})):(console.log(`Found stale PID file (PID: ${t.pid}), clean it...`),v(),{success:!0,message:"Stale PID file cleaned"}):{success:!0,message:"No existing service process"}}async function h(e,t=!1){return new Promise(o=>{if(!g(e)){console.debug(`Process ${e} does not exist, already stopped`),o(!0);return}let s=t?"SIGKILL":"SIGTERM",i=k()?"taskkill":"tree-kill";if(console.debug(`Use ${i} to stop process ${e} (signal: ${s})`),k()){let r=t?["/F","/PID",String(e)]:["/PID",String(e)],a=E("taskkill",r,{stdio:["ignore","pipe","pipe"],windowsHide:!0}),u="";a.stdout.on("data",l=>{u+=l.toString()}),a.stderr.on("data",l=>{u+=l.toString()}),a.on("error",l=>{console.error(`Stop process failed: ${l.message}`),o(!1)}),a.on("close",l=>{l===0?(console.debug(`Process ${e} stopped`),o(!0)):(console.warn(`taskkill exit code: ${l}, output: ${u}`),t?o(!1):h(e,!0).then(o))})}else V(e,s,r=>{r?r.code==="ESRCH"?(console.debug(`Process ${e} does not exist`),o(!0)):(console.error(`Stop process failed: ${r.message}`),o(!1)):(console.debug(`Process ${e} stopped`),o(!0))})})}async function D(e,t=n.defaultStopTimeout){let o=Date.now();for(;g(e);){if(Date.now()-o>t)return console.warn(`Wait for process ${e} to stop timeout (${t}ms)`),!1;await new Promise(i=>setTimeout(i,n.checkInterval))}let s=Date.now()-o;return console.debug(`Process ${e} stopped after ${s}ms`),!0}async function Z(e,t=n.defaultStartTimeout){let o=Number(e);if(!Number.isFinite(o)||o<=0)return!1;let s=Date.now();for(;Date.now()-s<=t;){if(await new Promise(r=>{let a=M.get({host:"127.0.0.1",port:o,path:"/health",timeout:Math.min(2e3,n.checkInterval*4)},u=>{r(u.statusCode>=200&&u.statusCode<300),u.resume()});a.on("timeout",()=>{a.destroy(),r(!1)}),a.on("error",()=>r(!1))}))return!0;await new Promise(r=>setTimeout(r,n.checkInterval))}return!1}async function N(e={}){let{env:t,port:o,config:s}=e,i=null;console.log(`Start service ${n.name}...`);try{i=z()}catch(r){return r.code==="EEXIST"?{success:!1,pid:null,message:"Another start operation is in progress, please retry later"}:{success:!1,pid:null,message:`Acquire start lock failed: ${r.message}`}}try{let r=Number(e.timeout)||n.defaultStopTimeout,a=await Y(r);if(!a.success)return{success:!1,pid:null,message:`Service start blocked: ${a.message}`};let u={...process.env};t&&(u.NODE_ENV=t,console.log(`Environment: ${t}`)),o&&(u.PORT=o,console.log(`Port: ${o}`)),s&&(u.CONFIG_FILE=s,console.log(`Configuration file: ${s}`));let l=J(),p=E("node",[l,...[]],{env:u,stdio:["pipe","pipe","pipe"],detached:!0,cwd:process.cwd()});if(p.stdout.on("data",m=>{process.stdout.write(m)}),p.stderr.on("data",m=>{process.stderr.write(m)}),p.on("error",m=>{console.error(`Start service failed: ${m.message}`)}),await new Promise(m=>setTimeout(m,2e3)),!g(p.pid))return{success:!1,pid:null,message:"Service start failed"};let S={pid:p.pid,startedAt:new Date().toISOString(),env:t||process.env.NODE_ENV||"production",port:o||process.env.PORT||"60000",version:W(),platform:process.platform};B(S);let P=Number(e.startTimeout)||n.defaultStartTimeout;return await Z(S.port,P)?(p.unref(),console.log(`Service started (PID: ${p.pid})`),console.log(`Service address: http://localhost:${S.port}`),{success:!0,pid:p.pid,message:"Service started successfully"}):(console.error(`Service health check timeout (${P}ms), stop failed instance...`),await h(p.pid,!0),v(),{success:!1,pid:null,message:`Service health check timeout (${P}ms)`})}finally{Q(i)}}async function T(e={}){let{force:t=!1,timeout:o=n.defaultStopTimeout}=e;console.log(`Stop service ${n.name}...`);let s=x();if(!s)return{success:!1,message:"Service not found"};if(!g(s.pid))return console.log("Service process has stopped, clean PID file..."),v(),{success:!0,message:"Service has stopped (process has exited)"};if(!await h(s.pid,t))return{success:!1,message:"Stop service failed"};let r=await D(s.pid,o);return v(),r?{success:!0,message:"Service has stopped"}:{success:!1,message:"Service stop timeout"}}async function O(e={}){console.log(`Restart service ${n.name}...`);let t=await T(e);!t.success&&t.message!=="Service not found"&&console.warn(`Stop service failed: ${t.message}`),await new Promise(s=>setTimeout(s,2e3));let o=await N(e);return o.success?{success:!0,pid:o.pid,message:"Service has restarted"}:{success:!1,pid:null,message:`Restart failed: ${o.message}`}}function _(){let e=x();return e?g(e.pid)?{running:!0,pidInfo:e,message:"Service running"}:{running:!1,pidInfo:e,message:"Service process does not exist"}:{running:!1,pidInfo:null,message:"Service not running"}}function L(e){try{let t=new Date(e),o=new Date;if(isNaN(t.getTime()))return"unknown";let s=Math.floor((o-t)/1e3),i=Math.floor(s/3600),r=Math.floor(s%3600/60),a=s%60;return i>0?`${i} hours ${r} minutes ${a} seconds`:r>0?`${r} minutes ${a} seconds`:`${a} seconds`}catch{return"unknown"}}var Pe=te(import.meta.url),A="1.3.0-beta.11",d=new ee,w={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"};function j(e){console.error(`${w.red}ERROR: ${e}${w.reset}`)}function b(e){console.log(`${w.green}${e}${w.reset}`)}function oe(e){console.log(`${w.blue}${e}${w.reset}`)}function y(e){return async(...t)=>{try{let o=await e(...t);o&&o.success===!1?(j(o.message||"Command failed"),process.exitCode=1):process.exitCode=0}catch(o){j(o?.message||"Command failed with unexpected error"),process.exitCode=1}}}async function se(e){let t=await N({env:e.env,port:e.port,config:e.config,timeout:Number(e.timeout),startTimeout:Number(e.startTimeout)});return t.success&&b(`Service started (PID: ${t.pid})`),t}async function re(e){let t=await T({force:e.force,timeout:Number(e.timeout)});return t.success&&b(t.message||"Service stopped"),t}async function ne(e){let t=await O({env:e.env,port:e.port,config:e.config,timeout:Number(e.timeout),startTimeout:Number(e.startTimeout)});return t.success&&b(t.message||"Service restarted"),t}function ie(){let e=_();return oe(`${n.name} service status:`),console.log(""),console.log(` Service name: ${n.name}`),console.log(` Running status: ${e.running?"Running":"Stopped"}`),console.log(` Message: ${e.message}`),console.log(` PID file: ${$()}`),e.pidInfo&&(console.log(` Process ID: ${e.pidInfo.pid}`),console.log(` Environment: ${e.pidInfo.env||"Unknown"}`),console.log(` Port: ${e.pidInfo.port||"Unknown"}`),console.log(` Version: ${e.pidInfo.version||A}`),console.log(` Platform: ${e.pidInfo.platform||process.platform}`),console.log(` Started at: ${e.pidInfo.startedAt||"Unknown"}`),console.log(` Uptime: ${L(e.pidInfo.startedAt)}`)),console.log(""),{success:e.running,message:e.message}}function ce(){d.name("nuwax-file-server").description("Cross-platform file service deployment tool, supporting start/stop/restart/status").version(A,"-v, --version","Display version number").helpOption("-h, --help","Display help information"),d.command("start").allowUnknownOption().description("Start service").option("--env <environment>","\u73AF\u5883: development|production|test","production").option("--port <port>","Service port").option("--config <path>","Custom configuration file path").option("--timeout <ms>","\u505C\u6B62\u65E7\u8FDB\u7A0B\u7B49\u5F85\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStopTimeout}`).option("--start-timeout <ms>","\u542F\u52A8\u5065\u5EB7\u68C0\u67E5\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStartTimeout}`).action(y(se)),d.command("stop").description("Stop service").option("--force","Force stop").option("--timeout <ms>","\u505C\u6B62\u670D\u52A1\u7B49\u5F85\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStopTimeout}`).action(y(re)),d.command("restart").allowUnknownOption().description("Restart service").option("--env <environment>","\u73AF\u5883: development|production|test","production").option("--port <port>","Service port").option("--config <path>","Custom configuration file path").option("--timeout <ms>","\u505C\u6B62\u65E7\u8FDB\u7A0B\u7B49\u5F85\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStopTimeout}`).option("--start-timeout <ms>","\u542F\u52A8\u5065\u5EB7\u68C0\u67E5\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09",`${n.defaultStartTimeout}`).action(y(ne)),d.command("status").description("View service status").action(y(ie)),d.command("help").description("Display help information").action(()=>{d.outputHelp()}),d.parse(process.argv),process.argv.slice(2).length||(d.outputHelp(),process.exitCode=0)}ce();
|
package/dist/env.development
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
NODE_ENV=development
|
|
3
3
|
PORT=60000
|
|
4
4
|
|
|
5
|
+
# 部署模式: docker-compose (默认) 或 k8s
|
|
6
|
+
# docker-compose: 使用 symlink + 模板缓存(本地磁盘 node_modules,启动快)
|
|
7
|
+
# k8s: 不使用 symlink,node_modules 直接放 JuiceFS(多节点共享)
|
|
8
|
+
# DEPLOYMENT_MODE=docker-compose
|
|
9
|
+
|
|
5
10
|
# 日志配置
|
|
6
11
|
LOG_BASE_DIR=/app/logs/project_logs
|
|
7
12
|
LOG_LEVEL=debug
|
|
@@ -43,7 +48,7 @@ DOWNLOAD_MAX_FILE_SIZE_BYTES=104857600
|
|
|
43
48
|
REQUEST_BODY_LIMIT=2000mb
|
|
44
49
|
|
|
45
50
|
# 目录遍历排除(返回内容、归档等)
|
|
46
|
-
TRAVERSE_EXCLUDE_DIRS=dist,node_modules,.pnpm-store,__MACOSX,.attachments
|
|
51
|
+
TRAVERSE_EXCLUDE_DIRS=dist,node_modules,.pnpm-store,__MACOSX,.attachments,.git
|
|
47
52
|
|
|
48
53
|
# 归档遍历排除文件
|
|
49
54
|
BACKUP_TRAVERSE_EXCLUDE_FILES=pnpm-lock.yaml,yarn.lock,package-lock.json
|
|
@@ -83,3 +88,10 @@ LOG_CACHE_MAX_FILE_SIZE=2097152
|
|
|
83
88
|
COMPUTER_WORKSPACE_DIR=/app/computer-project-workspace
|
|
84
89
|
# computer 日志目录
|
|
85
90
|
COMPUTER_LOG_DIR=/app/logs/computer_logs
|
|
91
|
+
|
|
92
|
+
# Git 版本管理配置
|
|
93
|
+
GIT_DEFAULT_AUTHOR_NAME=Nuwax File Server
|
|
94
|
+
GIT_DEFAULT_AUTHOR_EMAIL=git@nuwax.com
|
|
95
|
+
GIT_AUTO_GITIGNORE=true
|
|
96
|
+
GIT_GITIGNORE_ENTRIES=node_modules/|.pnpm-store/|dist/|build/|.idea/|.vscode/|.DS_Store|.agents/|.claude/|.opencode/|.codex/
|
|
97
|
+
GIT_ENABLED=true
|
package/dist/env.production
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
NODE_ENV=production
|
|
3
3
|
PORT=60000
|
|
4
4
|
|
|
5
|
+
# 部署模式: docker-compose (默认) 或 k8s
|
|
6
|
+
# docker-compose: 使用 symlink + 模板缓存(本地磁盘 node_modules,启动快)
|
|
7
|
+
# k8s: 不使用 symlink,node_modules 直接放 JuiceFS(多节点共享)
|
|
8
|
+
# DEPLOYMENT_MODE=docker-compose
|
|
9
|
+
|
|
5
10
|
# 日志配置
|
|
6
11
|
LOG_BASE_DIR=/app/logs/project_logs
|
|
7
12
|
LOG_LEVEL=debug
|
|
@@ -43,7 +48,7 @@ DOWNLOAD_MAX_FILE_SIZE_BYTES=104857600
|
|
|
43
48
|
REQUEST_BODY_LIMIT=2000mb
|
|
44
49
|
|
|
45
50
|
# 目录遍历排除(返回内容、归档等)
|
|
46
|
-
TRAVERSE_EXCLUDE_DIRS=dist,node_modules,.pnpm-store,__MACOSX,.attachments
|
|
51
|
+
TRAVERSE_EXCLUDE_DIRS=dist,node_modules,.pnpm-store,__MACOSX,.attachments,.git
|
|
47
52
|
|
|
48
53
|
# 归档遍历排除文件
|
|
49
54
|
BACKUP_TRAVERSE_EXCLUDE_FILES=pnpm-lock.yaml,yarn.lock,package-lock.json
|
|
@@ -83,3 +88,10 @@ LOG_CACHE_MAX_FILE_SIZE=2097152
|
|
|
83
88
|
COMPUTER_WORKSPACE_DIR=/app/computer-project-workspace
|
|
84
89
|
# computer 日志目录
|
|
85
90
|
COMPUTER_LOG_DIR=/app/logs/computer_logs
|
|
91
|
+
|
|
92
|
+
# Git 版本管理配置
|
|
93
|
+
GIT_DEFAULT_AUTHOR_NAME=Nuwax File Server
|
|
94
|
+
GIT_DEFAULT_AUTHOR_EMAIL=git@nuwax.com
|
|
95
|
+
GIT_AUTO_GITIGNORE=true
|
|
96
|
+
GIT_GITIGNORE_ENTRIES=node_modules/|.pnpm-store/|dist/|build/|.idea/|.vscode/|.DS_Store|.agents/|.claude/|.opencode/|.codex/
|
|
97
|
+
GIT_ENABLED=true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import w from"express";import u from"multer";import{asyncHandler as m,ValidationError as s}from"../utils/error/errorHandler.js";import p from"../service/codeService.js";import{log as c}from"../utils/log/logUtils.js";import b from"../appConfig/index.js";import{extractIsolationContext as h}from"../utils/common/projectPathUtils.js";const y=w.Router(),I=u({storage:u.memoryStorage(),limits:{fileSize:b.UPLOAD_SINGLE_FILE_SIZE_BYTES}}),S=[{path:"/specified-files-update",method:"post",handler:m(async(e,a)=>{const{projectId:o,codeVersion:n,files:t}=e.body||{},l=h(e.body||{});c(o,"INFO","Partial files update",{projectId:o,codeVersion:n,filesCount:t?t.length:0}),t&&Array.isArray(t)&&t.forEach(r=>{if(r&&typeof r.contents=="string"&&r.contents)try{r.contents=decodeURIComponent(r.contents)}catch(f){c(o,"WARN","Decode file content failed",{fileName:r.path,error:f.message})}});const i=await p.specifiedFilesUpdate(String(o),String(n),t,e,l);a.status(200).json(i)})},{path:"/all-files-update",method:"post",handler:m(async(e,a)=>{const{projectId:o,codeVersion:n,files:t,basePath:l,pid:i}=e.body||{},r=h(e.body||{});if(c(o,"INFO","Submit files",{projectId:o,codeVersion:n,basePath:l,pid:i}),!o)throw new s("Project ID cannot be empty",{field:"projectId"});if(n==null)throw new s("codeVersion cannot be empty",{field:"codeVersion"});if(!Array.isArray(t))throw new s("files must be an array",{field:"files"});t&&Array.isArray(t)&&t.forEach(d=>{if(d&&typeof d.contents=="string"&&d.contents)try{d.contents=decodeURIComponent(d.contents)}catch(g){c(o,"WARN","Decode file content failed",{fileName:d.name,error:g.message})}});const f=await p.allFilesUpdate(String(o),String(n),t,e,r);a.status(200).json(f)})},{path:"/upload-single-file",method:"post",middleware:I.single("file"),handler:m(async(e,a)=>{const{projectId:o,codeVersion:n,filePath:t}=e.body||{},l=h(e.body||{}),i=e.file;if(c(o,"INFO","\u4E0A\u4F20\u5355\u4E2A\u6587\u4EF6",{projectId:o,codeVersion:n,filePath:t}),!o)throw new s("Project ID cannot be empty",{field:"projectId"});if(n==null)throw new s("codeVersion cannot be empty",{field:"codeVersion"});if(!i)throw new s("File cannot be empty",{field:"file"});if(!t||typeof t!="string")throw new s("File path cannot be empty",{field:"filePath"});c(o,"INFO","Received file information",{originalname:i.originalname,mimetype:i.mimetype,size:i.size,bufferLength:i.buffer?i.buffer.length:0,bufferIsBuffer:Buffer.isBuffer(i.buffer)});const r={buffer:i.buffer,originalname:i.originalname,mimetype:i.mimetype,size:i.size},f=await p.uploadSingleFile(String(o),String(n),r,t,e,l);a.status(200).json(f)})},{path:"/rollback-version",method:"post",handler:m(async(e,a)=>{const{projectId:o,codeVersion:n,rollbackTo:t}=e.body||{},l=h(e.body||{});if(c(o,"INFO","Rollback version",{projectId:o,codeVersion:n,rollbackTo:t}),!o)throw new s("Project ID cannot be empty",{field:"projectId"});if(n==null)throw new s("codeVersion cannot be empty",{field:"codeVersion"});if(t==null)throw new s("rollbackTo cannot be empty",{field:"rollbackTo"});if(b.GIT_ENABLED)return a.status(200).json({success:!1,deprecated:!0,message:"\u6B64\u63A5\u53E3\u5DF2\u5E9F\u5F03\uFF0C\u8BF7\u4F7F\u7528 /api/git/rollback \u8FDB\u884C\u7248\u672C\u56DE\u6EDA"});const i=await p.rollbackVersion(String(o),String(n),String(t),e,l);a.status(200).json(i)})}];S.forEach(e=>{e.middleware?y[e.method](e.path,e.middleware,e.handler):y[e.method](e.path,e.handler)});export default y;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import N from"express";import S from"multer";import f from"fs";import I from"path";import{ValidationError as z,asyncHandler as p}from"../utils/error/errorHandler.js";import{log as m}from"../utils/log/logUtils.js";import k from"../appConfig/index.js";import{createWorkspace as F,pushSkillsToWorkspace as $,initProjectTemplate as A,executeCommand as b}from"../utils/computer/computerUtils.js";import{getFileList as j,updateFiles as E,uploadFile as O,uploadFiles as P,downloadAllFiles as U}from"../utils/computer/computerFileUtils.js";const g=N.Router(),y=S({storage:S.diskStorage({destination:(e,i,o)=>{try{const t=k.COMPUTER_WORKSPACE_DIR;if(!t)return o(new Error("COMPUTER_WORKSPACE_DIR is not configured, cannot determine upload temporary directory"));const s=e.body?.userId||"unknown",a=e.body?.cId||"unknown",n=I.join(t,String(s),String(a),".tmp");f.existsSync(n)||f.mkdirSync(n,{recursive:!0}),o(null,n)}catch(t){o(t)}},filename:(e,i,o)=>{const t=I.extname(i.originalname)||".zip",s=I.basename(i.originalname,t),a=`${Date.now()}_${Math.round(Math.random()*1e6)}`;o(null,`${s}_${a}${t}`)}}),limits:{fileSize:k.UPLOAD_MAX_FILE_SIZE_BYTES}});function R(e,i,o,t){if(e&&e.name==="MulterError"||e&&(e.name==="ValidationError"||e instanceof z))return t(e);t(e)}const x=[{path:"/create-workspace",method:"post",middleware:y.single("file"),handler:p(async(e,i)=>{const{userId:o,cId:t}=e.body||{},s=e.file||null,a=`computer:${o}:${t}`;m(a,"INFO","Create workspace request",{userId:o,cId:t,hasFile:!!s,fileName:s?.originalname,fileSize:s?.size});const n=await F(o,t,s);i.status(200).json({success:!0,...n})})},{path:"/create-workspace-v2",method:"post",middleware:y.single("file"),handler:p(async(e,i)=>{const{userId:o,cId:t,skillUrls:s,mcpServersConfig:a,hooksConfig:n,permissionsConfig:r,hookScripts:l}=e.body||{},d=e.file||null,u=`computer:${o}:${t}`;let c=s;if(typeof s=="string")try{const w=JSON.parse(s);c=Array.isArray(w)?w:[s]}catch{c=[s]}let h=l;if(typeof l=="string")try{h=JSON.parse(l)}catch{h=null}m(u,"INFO","Create workspace v2 request",{userId:o,cId:t,hasFile:!!d,fileName:d?.originalname,fileSize:d?.size,skillUrlsCount:Array.isArray(c)?c.length:0,hasMcpServersConfig:!!a,hasHooksConfig:!!n,hasPermissionsConfig:!!r,hookScriptsCount:Array.isArray(h)?h.length:0});const C=await F(o,t,d,c,a,r,n,h);i.status(200).json({success:!0,...C})})},{path:"/push-skills-to-workspace",method:"post",middleware:y.single("file"),handler:p(async(e,i)=>{const{userId:o,cId:t}=e.body||{},s=e.file||null,a=`computer:${o}:${t}`;m(a,"INFO","\u63A8\u9001\u6280\u80FD\u5230\u5DE5\u4F5C\u7A7A\u95F4\u8BF7\u6C42",{userId:o,cId:t,hasFile:!!s,fileName:s?.originalname,fileSize:s?.size});const n=await $(o,t,s);i.status(200).json({success:!0,...n})})},{path:"/push-skills-to-workspace-v2",method:"post",middleware:y.single("file"),handler:p(async(e,i)=>{const{userId:o,cId:t,skillUrls:s}=e.body||{},a=e.file||null,n=`computer:${o}:${t}`;let r=s;if(typeof s=="string")try{const d=JSON.parse(s);r=Array.isArray(d)?d:[s]}catch{r=[s]}m(n,"INFO","\u63A8\u9001\u6280\u80FD\u5230\u5DE5\u4F5C\u7A7A\u95F4\u8BF7\u6C42(v2)",{userId:o,cId:t,hasFile:!!a,fileName:a?.originalname,fileSize:a?.size,skillUrlsCount:Array.isArray(r)?r.length:0});const l=await $(o,t,a,r);i.status(200).json({success:!0,...l})})},{path:"/get-file-list",method:"get",handler:p(async(e,i)=>{const{userId:o,cId:t,proxyPath:s}=e.query,a=await j(o,t,s);i.status(200).json({success:!0,...a})})},{path:"/files-update",method:"post",handler:p(async(e,i)=>{const{userId:o,cId:t,files:s}=e.body||{},a=`computer:${o}:${t}`;m(a,"INFO","Files update",{userId:o,cId:t,filesCount:s?s.length:0}),s&&Array.isArray(s)&&s.forEach(r=>{if(r&&typeof r.contents=="string"&&r.contents)try{r.contents=decodeURIComponent(r.contents)}catch(l){m(a,"WARN","Decode file content failed",{fileName:r.name,error:l.message})}});const n=await E(o,t,s);i.status(200).json(n)})},{path:"/upload-file",method:"post",middleware:y.single("file"),handler:p(async(e,i)=>{const{userId:o,cId:t,filePath:s}=e.body||{},a=e.file,n=`computer:${o}:${t}`;m(n,"INFO","Upload single file",{userId:o,cId:t,filePath:s});const l={buffer:await f.promises.readFile(a.path),originalname:a.originalname,mimetype:a.mimetype,size:a.size};try{const d=await O(o,t,l,s);i.status(200).json(d)}finally{f.existsSync(a.path)&&await f.promises.unlink(a.path)}})},{path:"/upload-files",method:"post",middleware:y.array("files"),handler:p(async(e,i)=>{const{userId:o,cId:t,filePaths:s}=e.body||{},a=e.files||[],n=`computer:${o}:${t}`,r=Array.isArray(s)?s:typeof s=="string"?[s]:s;m(n,"INFO","Batch upload files request",{userId:o,cId:t,filesCount:a.length,filePathsCount:Array.isArray(r)?r.length:0});const l=[],d=[];try{for(const c of a){d.push(c.path);const h=await f.promises.readFile(c.path);l.push({buffer:h,originalname:c.originalname,mimetype:c.mimetype,size:c.size})}const u=await P(o,t,l,r);i.status(200).json(u)}finally{for(const u of d)if(f.existsSync(u))try{await f.promises.unlink(u)}catch(c){m(n,"WARN","Clean temporary file failed",{tempPath:u,error:c.message})}}})},{path:"/init-project-template",method:"post",middleware:y.single("file"),handler:p(async(e,i)=>{const{userId:o,cId:t,enableGit:s}=e.body||{},a=e.file||null,n=`computer:${o}:${t}`;m(n,"INFO","Init project template request",{userId:o,cId:t,hasFile:!!a,fileName:a?.originalname,fileSize:a?.size,enableGit:s});const r=await A(o,t,a,s);i.status(200).json({success:!0,...r})})},{path:"/download-all-files",method:"get",handler:p(async(e,i)=>{const{userId:o,cId:t}=e.query||{},s=`computer:${o}:${t}`;m(s,"INFO","Download all files request",{userId:o,cId:t});const{archive:a,zipFileName:n}=await U(o,t);i.setHeader("Content-Type","application/zip");const r=encodeURIComponent(n);i.setHeader("Content-Disposition",`attachment; filename="${r}"; filename*=UTF-8''${r}`),a.on("error",l=>{i.destroy(l)}),a.pipe(i),a.finalize()})},{path:"/execute-command",method:"post",handler:p(async(e,i)=>{const{userId:o,cId:t,command:s}=e.body||{},a=`computer:${o}:${t}`;m(a,"INFO","Execute command request",{userId:o,cId:t,command:s});const n=await b(o,t,s);i.status(200).json({success:!0,...n})})}];x.forEach(e=>{if(e.middleware)g[e.method](e.path,e.middleware,e.handler);else if(e.customHandler){const i=[];e.handler&&i.push(e.handler),e.decodeMiddleware&&i.push(e.decodeMiddleware),i.push(R),i.push(e.customHandler),g[e.method](e.path,...i)}else g[e.method](e.path,e.handler)});export default g;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import l from"express";import{asyncHandler as r,ValidationError as h}from"../utils/error/errorHandler.js";import c from"../service/gitService.js";import{log as d}from"../utils/log/logUtils.js";import{extractIsolationContext as I}from"../utils/common/projectPathUtils.js";const u=l.Router();function p(e){const{workspaceType:o,projectId:t,userId:s,cId:a}=e||{},n=I(e||{});return{workspaceType:o?String(o):void 0,projectId:t?String(t):void 0,userId:s?String(s):void 0,cId:a?String(a):void 0,isolationContext:n}}const y=[{path:"/init",method:"post",handler:r(async(e,o)=>{const t=p(e.body);d(t.projectId||`computer:${t.userId}:${t.cId}`,"INFO","Git init",t);const s=await c.init(t);o.status(200).json(s)})},{path:"/status",method:"get",handler:r(async(e,o)=>{const t=p(e.query);d(t.projectId||`computer:${t.userId}:${t.cId}`,"INFO","Git status",t);const s=await c.status(t);o.status(200).json(s)})},{path:"/commit",method:"post",handler:r(async(e,o)=>{const{message:t,files:s,authorName:a,authorEmail:n}=e.body||{},i={...p(e.body),message:t,files:s,authorName:a,authorEmail:n};if(!t)throw new h("Commit message cannot be empty",{field:"message"});d(i.projectId||`computer:${i.userId}:${i.cId}`,"INFO","Git commit",{...i,filesCount:s?s.length:"all"});const m=await c.commit(i);o.status(200).json(m)})},{path:"/add",method:"post",handler:r(async(e,o)=>{const{files:t}=e.body||{},s={...p(e.body),files:t};d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git add",{...s,filesCount:t?t.length:"all"});const a=await c.add(s);o.status(200).json(a)})},{path:"/unstage",method:"post",handler:r(async(e,o)=>{const{files:t}=e.body||{},s={...p(e.body),files:t};d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git unstage",{...s,filesCount:t?t.length:"all"});const a=await c.unstage(s);o.status(200).json(a)})},{path:"/discard",method:"post",handler:r(async(e,o)=>{const{files:t}=e.body||{},s={...p(e.body),files:t};d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git discard",{...s,filesCount:t?t.length:"all"});const a=await c.discard(s);o.status(200).json(a)})},{path:"/log",method:"get",handler:r(async(e,o)=>{const{maxCount:t,branch:s,skip:a}=e.query||{},n={...p(e.query),maxCount:t?parseInt(t,10):50,skip:a?parseInt(a,10):0,branch:s};d(n.projectId||`computer:${n.userId}:${n.cId}`,"INFO","Git log",n);const i=await c.logHistory(n);o.status(200).json(i)})},{path:"/diff",method:"post",handler:r(async(e,o)=>{const{from:t,to:s,paths:a,source:n}=e.body||{},i={...p(e.body),from:t,to:s,paths:a,source:n};d(i.projectId||`computer:${i.userId}:${i.cId}`,"INFO","Git diff",{from:t,to:s});const m=await c.diff(i);o.status(200).json(m)})},{path:"/file-content",method:"post",handler:r(async(e,o)=>{const{ref:t,filePath:s}=e.body||{},a={...p(e.body),ref:t,filePath:s};d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git file content",{ref:t,filePath:s});const n=await c.fileContent(a);o.status(200).json(n)})},{path:"/reset",method:"post",handler:r(async(e,o)=>{const{target:t,mode:s}=e.body||{},a={...p(e.body),target:t,mode:s||"mixed"};if(!t)throw new h("Reset target cannot be empty",{field:"target"});d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git reset",{target:t,mode:s});const n=await c.reset(a);o.status(200).json(n)})},{path:"/checkout",method:"post",handler:r(async(e,o)=>{const{target:t}=e.body||{},s={...p(e.body),target:t};if(!t)throw new h("Checkout target cannot be empty",{field:"target"});d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git checkout files",{target:t});const a=await c.checkout(s);o.status(200).json(a)})},{path:"/revert",method:"post",handler:r(async(e,o)=>{const{target:t}=e.body||{},s={...p(e.body),target:t};if(!t)throw new h("Revert target cannot be empty",{field:"target"});d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git revert",{target:t});const a=await c.revert(s);o.status(200).json(a)})},{path:"/tags",method:"get",handler:r(async(e,o)=>{const t=p(e.query);d(t.projectId||`computer:${t.userId}:${t.cId}`,"INFO","Git list tags",t);const s=await c.listTags(t);o.status(200).json(s)})},{path:"/tag-create",method:"post",handler:r(async(e,o)=>{const{tagName:t,message:s}=e.body||{},a={...p(e.body),tagName:t,message:s};if(!t)throw new h("Tag name cannot be empty",{field:"tagName"});d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git create tag",{tagName:t});const n=await c.createTag(a);o.status(200).json(n)})},{path:"/tag-delete",method:"post",handler:r(async(e,o)=>{const{tagName:t}=e.body||{},s={...p(e.body),tagName:t};if(!t)throw new h("Tag name cannot be empty",{field:"tagName"});d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git delete tag",{tagName:t});const a=await c.deleteTag(s);o.status(200).json(a)})},{path:"/branches",method:"get",handler:r(async(e,o)=>{const t=p(e.query);d(t.projectId||`computer:${t.userId}:${t.cId}`,"INFO","Git list branches",t);const s=await c.listBranches(t);o.status(200).json(s)})},{path:"/branch-create",method:"post",handler:r(async(e,o)=>{const{branchName:t,startPoint:s}=e.body||{},a={...p(e.body),branchName:t,startPoint:s};if(!t)throw new h("Branch name cannot be empty",{field:"branchName"});d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git create branch",{branchName:t,startPoint:s});const n=await c.createBranch(a);o.status(200).json(n)})},{path:"/branch-switch",method:"post",handler:r(async(e,o)=>{const{branchName:t}=e.body||{},s={...p(e.body),branchName:t};if(!t)throw new h("Branch name cannot be empty",{field:"branchName"});d(s.projectId||`computer:${s.userId}:${s.cId}`,"INFO","Git switch branch",{branchName:t});const a=await c.switchBranch(s);o.status(200).json(a)})},{path:"/branch-delete",method:"post",handler:r(async(e,o)=>{const{branchName:t,force:s}=e.body||{},a={...p(e.body),branchName:t,force:s===!0};if(!t)throw new h("Branch name cannot be empty",{field:"branchName"});d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git delete branch",{branchName:t,force:s});const n=await c.deleteBranch(a);o.status(200).json(n)})},{path:"/stash",method:"post",handler:r(async(e,o)=>{const{message:t,files:s}=e.body||{},a={...p(e.body),message:t,files:s};d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git stash push",a);const n=await c.stashPush(a);o.status(200).json(n)})},{path:"/stash-pop",method:"post",handler:r(async(e,o)=>{const{index:t,files:s}=e.body||{},a={...p(e.body),index:t,files:s};d(a.projectId||`computer:${a.userId}:${a.cId}`,"INFO","Git stash pop",{index:t});const n=await c.stashPop(a);o.status(200).json(n)})},{path:"/stash-list",method:"get",handler:r(async(e,o)=>{const t=p(e.query);d(t.projectId||`computer:${t.userId}:${t.cId}`,"INFO","Git stash list",t);const s=await c.stashList(t);o.status(200).json(s)})}];y.forEach(e=>{e.middleware?u[e.method](e.path,e.middleware,e.handler):u[e.method](e.path,e.handler)});export default u;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import x from"express";import
|
|
1
|
+
import x from"express";import j from"multer";import h from"path";import d from"fs";import P from"iconv-lite";import u from"../service/projectService.js";import{getProjectContent as b,getProjectContentByVersion as E}from"../utils/project/getContentUtils.js";import{uploadAttachmentFile as D}from"../utils/project/uploadAttachmentFileUtils.js";import{copyProject as C}from"../utils/project/copyProjectUtils.js";import m from"../appConfig/index.js";import{log as y}from"../utils/log/logUtils.js";import{extractIsolationContext as l,resolveProjectPath as T}from"../utils/common/projectPathUtils.js";import{ValidationError as i,SystemError as R,asyncHandler as p}from"../utils/error/errorHandler.js";const w=x.Router(),O=j.diskStorage({destination:function(e,n,t){const o=h.join(m.UPLOAD_PROJECT_DIR,"temp");d.existsSync(o)||d.mkdirSync(o,{recursive:!0}),t(null,o)},filename:function(e,n,t){const o=Date.now()+"-"+Math.round(Math.random()*1e6);t(null,n.fieldname+"-"+o+h.extname(n.originalname))}}),I=j({storage:O,fileFilter:function(e,n,t){const o=h.extname(n.originalname).toLowerCase();m.UPLOAD_ALLOWED_EXTENSIONS.includes(o)?t(null,!0):t(new i("File type not allowed",{fileExtension:o,allowedExtensions:m.UPLOAD_ALLOWED_EXTENSIONS}),!1)},limits:{fileSize:m.UPLOAD_MAX_FILE_SIZE_BYTES}}),_=j.diskStorage({destination:function(e,n,t){const o=h.join(m.UPLOAD_PROJECT_DIR,"temp");d.existsSync(o)||d.mkdirSync(o,{recursive:!0}),t(null,o)},filename:function(e,n,t){const o=Date.now()+"-"+Math.round(Math.random()*1e6);t(null,n.fieldname+"-"+o+h.extname(n.originalname))}}),S=[".pdf",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".txt",".md",".csv",".json",".xml",".png",".jpg",".jpeg",".gif",".bmp",".svg",".ico",".webp",".avif",".zip",".rar",".7z",".tar",".gz",".mp4",".avi",".mov",".wmv",".flv",".mp3",".wav",".ogg",".m4a"],A=j({storage:_,fileFilter:function(e,n,t){const o=h.extname(n.originalname).toLowerCase();S.includes(o)?t(null,!0):t(new i("\u9644\u4EF6\u6587\u4EF6\u7C7B\u578B\u4E0D\u88AB\u5141\u8BB8",{fileExtension:o,allowedExtensions:S}),!1)},limits:{fileSize:m.UPLOAD_MAX_FILE_SIZE_BYTES}});function L(e,n,t){if(e.file&&e.file.originalname)try{const o=e.file.originalname,a=P.decode(Buffer.from(e.file.originalname,"latin1"),"utf8");e.file.originalname=a,y("system","INFO","File name decoded successfully",{before:o,after:a})}catch(o){y("system","WARN","File name decoded failed",{originalName:e.file.originalname,error:o.message})}t()}const v=[{path:"/push-skills-to-workspace",method:"post",handler:I.single("file"),customHandler:p(async(e,n)=>{const{projectId:t,skillUrls:o}=e.body||{},a=e.file||null;if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});const s=l(e.body||{}),r=await u.pushSkillsToWorkspace(String(t),a,o,s);n.status(200).json({success:!0,...r})})},{path:"/create-project",method:"post",handler:p(async(e,n)=>{const{projectId:t,templateType:o}=e.body,a=l(e.body||{});if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});const s=await u.createProject(String(t),o,a);n.status(200).json(s)})},{path:"/upload-project",method:"post",handler:I.single("file"),customHandler:p(async(e,n)=>{const{projectId:t,codeVersion:o,pid:a,basePath:s}=e.body,r=l(e.body||{});if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});if(!o)throw new i("Code version cannot be empty",{field:"codeVersion"});if(!e.file)throw new i("Please upload a zip file",{field:"zipFile"});const c=await u.handleFileUpload(String(t),o,e.file);e.file.path=c.filePath;try{const f=await u.uploadProject(String(t),e.file.path,e,o,a,s,r);n.status(200).json(f)}catch(f){try{await u.cleanupProjectDirectory(String(t),r)}catch(g){y(t,"ERROR","Route layer cleanup project directory failed",{projectId:t,error:g.message})}if(e.file&&d.existsSync(e.file.path))try{d.unlinkSync(e.file.path)}catch(g){y(t,"ERROR","Clean upload file failed",{projectId:t,error:g.message})}throw f}})},{path:"/get-project-content",method:"get",handler:p(async(e,n)=>{const{projectId:t,command:o,proxyPath:a}=e.query,s=l(e.query||{});if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});const r=T(t,s);if(!d.existsSync(r))throw new i("Project does not exist",{field:"projectId"});try{const c=await b(r,o,a);n.status(200).json({success:!0,...c})}catch(c){const f=c?.message||"Query failed";n.status(500).json({success:!1,message:f})}})},{path:"/get-project-content-by-version",method:"get",handler:p(async(e,n)=>{const{projectId:t,codeVersion:o,command:a,proxyPath:s}=e.query,r=l(e.query||{});if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});if(!o)throw new i("Code version cannot be empty",{field:"codeVersion"});if(m.GIT_ENABLED)return n.status(200).json({success:!1,deprecated:!0,message:"\u6B64\u63A5\u53E3\u5DF2\u5E9F\u5F03\uFF0C\u8BF7\u4F7F\u7528 /api/git/log + /api/git/diff \u67E5\u770B\u5386\u53F2\u7248\u672C\u5185\u5BB9"});try{const c=await E(String(t),o,a,s,r);n.status(200).json({success:!0,...c})}catch(c){const f=c?.message||"Query failed";n.status(500).json({success:!1,message:f})}})},{path:"/backup-current-version",method:"post",handler:p(async(e,n)=>{const{projectId:t,codeVersion:o}=e.body,a=l(e.body||{});if(m.GIT_ENABLED)return n.status(200).json({success:!1,deprecated:!0,message:"\u6B64\u63A5\u53E3\u5DF2\u5E9F\u5F03\uFF0C\u8BF7\u4F7F\u7528 Git \u7248\u672C\u7BA1\u7406 API\uFF08/api/git/*\uFF09"});const s=await u.backupCurrentVersion(String(t),o,a);n.status(200).json({success:!0,...s})})},{path:"/export-project",method:"post",handler:p(async(e,n)=>{const{projectId:t,codeVersion:o,exportType:a,config:s}=e.body,r=l(e.body||{}),c=await u.exportProject(String(t),o,a,s,r);if(!d.existsSync(c.zipPath))throw new R("Exported zip file does not exist",{zipPath:c.zipPath});const f=h.basename(c.zipPath);n.setHeader("Content-Type","application/zip"),n.setHeader("Content-Disposition",`attachment; filename="${f}"`),n.sendFile(c.zipPath,g=>{g?(y(t,"ERROR","Send zip file failed",{projectId:t,error:g.message}),n.headersSent||n.status(500).json({success:!1,message:"File send failed"})):y(t,"INFO","Zip file send successfully",{projectId:t,zipPath:c.zipPath})})})},{path:"/delete-project",method:"get",handler:p(async(e,n)=>{const{projectId:t,pid:o}=e.query,a=l(e.query||{});if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});const s=await u.deleteProject(String(t),o,e,a);n.status(200).json(s)})},{path:"/upload-attachment-file",method:"post",handler:A.single("file"),decodeMiddleware:L,customHandler:p(async(e,n)=>{const{projectId:t,fileName:o}=e.body,a=l(e.body||{});if(!t)throw new i("Project ID cannot be empty",{field:"projectId"});if(!e.file)throw new i("Please upload a file",{field:"file"});try{const s=await D(String(t),e.file,o,a);n.status(200).json({success:!0,...s})}catch(s){if(e.file&&d.existsSync(e.file.path))try{d.unlinkSync(e.file.path)}catch(r){y(t,"ERROR","Clean upload file failed",{projectId:t,error:r.message})}throw s}})},{path:"/copy-project",method:"post",handler:p(async(e,n)=>{const{sourceProjectId:t,targetProjectId:o}=e.body,a=l({tenantId:e.body?.sourceTenantId||e.body?.tenantId,spaceId:e.body?.sourceSpaceId||e.body?.spaceId,isolationType:e.body?.sourceIsolationType||e.body?.isolationType}),s=l({tenantId:e.body?.targetTenantId||e.body?.tenantId,spaceId:e.body?.targetSpaceId||e.body?.spaceId,isolationType:e.body?.targetIsolationType||e.body?.isolationType});if(!t)throw new i("Source project ID cannot be empty",{field:"sourceProjectId"});if(!o)throw new i("Target project ID cannot be empty",{field:"targetProjectId"});const r=await C(String(t),String(o),{sourceIsolationContext:a,targetIsolationContext:s});n.status(200).json(r)})}];function z(e,n,t,o){if(e instanceof j.MulterError||e.name==="ValidationError"||e instanceof i)return o(e);o(e)}v.forEach(e=>{if(e.customHandler){const n=[];e.handler&&n.push(e.handler),e.decodeMiddleware&&n.push(e.decodeMiddleware),n.push(z),n.push(e.customHandler),w[e.method](e.path,...n)}else w[e.method](e.path,e.handler)});export default w;
|
package/dist/routes/router.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import i from"express";import c from"./buildRoutes.js";import u from"./projectRoutes.js";import l from"./codeRoutes.js";import d from"./computerRoutes.js";import s from"path";import
|
|
1
|
+
import i from"express";import c from"./buildRoutes.js";import u from"./projectRoutes.js";import l from"./codeRoutes.js";import d from"./computerRoutes.js";import h from"./gitRoutes.js";import s from"path";import f from"fs";import{fileURLToPath as g}from"url";const v=s.dirname(g(import.meta.url));let a="1.0.0";try{const o=s.resolve(v,"..","..","package.json");a=JSON.parse(f.readFileSync(o,"utf8")).version||"1.0.0"}catch{}const e=i.Router();e.get("/",(o,r)=>{r.send("Hello")}),e.get("/health",(o,r)=>{const p=Math.floor(process.uptime()),t=process.memoryUsage(),m={heapUsed:Math.round(t.heapUsed/1024/1024*100)/100,heapTotal:Math.round(t.heapTotal/1024/1024*100)/100,rss:Math.round(t.rss/1024/1024*100)/100,external:Math.round(t.external/1024/1024*100)/100},n={status:"ok",timestamp:Date.now(),uptime:p,version:a,platform:process.platform,nodeVersion:process.version,pid:process.pid,memory:m,env:process.env.NODE_ENV||"unknown"};r.json(n)}),e.use("/api/build",c),e.use("/api/project",u),e.use("/api/project",l),e.use("/api/computer",d),e.use("/api/git",h);export default e;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import l from"path";import
|
|
1
|
+
import l from"path";import r from"fs";import R from"../appConfig/index.js";import{restartDevServer as A}from"../utils/build/restartDevUtils.js";import{log as i}from"../utils/log/logUtils.js";import{ValidationError as m,SystemError as E,ResourceError as D}from"../utils/error/errorHandler.js";import"../utils/common/sensitiveUtils.js";import{backupProjectToZip as S,restoreProjectFromZip as z,pruneMissingFiles as U}from"../utils/project/backupUtils.js";import"../utils/common/zipUtils.js";import"../utils/buildJudge/restartJudgeUtils.js";import{resolveProjectPath as x}from"../utils/common/projectPathUtils.js";function L(e,P){const n=e.split(/\r?\n/),g=P.split(/\r?\n/),d=n.length,y=g.length,b=Math.min(d,y);let a=0;for(let o=0;o<b;o++)n[o]!==g[o]&&(n[o]=g[o],a++);if(d>y)for(let o=d-1;o>=y;o--)n.splice(o,1),a++;if(y>d)for(let o=d;o<y;o++)n.push(g[o]),a++;const h=e.includes(`\r
|
|
2
2
|
`)?`\r
|
|
3
3
|
`:`
|
|
4
|
-
`;return{finalContent:n.join(u),changesCount:r}}function ie(e,F){return e===F?{finalContent:e,changesCount:0}:{finalContent:F,changesCount:-1}}async function T(e,F,n,d,P={}){const w=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});if(F==null)throw new m("codeVersion cannot be empty",{field:"codeVersion"});const b=Number(F);if(!Number.isFinite(b))throw new m("codeVersion must be a number",{field:"codeVersion"});if(!Array.isArray(n))throw new m("files must be an array",{field:"files"});for(let s=0;s<n.length;s++){const o=n[s];if(!o||typeof o.operation!="string")throw new m(`files[${s}].operation cannot be empty`,{field:`files[${s}].operation`});if(!o.name||typeof o.name!="string")throw new m(`files[${s}].name cannot be empty`,{field:`files[${s}].name`});const t=o.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(t))throw new m(`files[${s}].operation must be one of create, delete, rename or modify`,{field:`files[${s}].operation`});if(t==="rename"&&!o.renameFrom)throw new m(`files[${s}].renameFrom cannot be empty (rename operation requires)`,{field:`files[${s}].renameFrom`});if(t==="modify"&&typeof o.contents!="string")throw new m(`files[${s}].contents must be a string (modify operation requires)`,{field:`files[${s}].contents`})}const r=x(e,P);if(!a.existsSync(r))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:r}),new O("Project does not exist",{projectId:e});let u="";try{const s=l.join(R.UPLOAD_PROJECT_DIR,e);a.existsSync(s)||a.mkdirSync(s,{recursive:!0});const o=`${e}-v${b}.zip`;u=l.join(s,o),i(e,"DEBUG","Start backing up project",{projectId:e,backupZipPath:u}),await D(e,r,u),i(e,"INFO","Project backed up successfully",{projectId:e,zipPath:u});try{i(e,"DEBUG","Start processing file operations",{projectId:e,filesCount:n.length});for(const t of n){const f=t.operation.toLowerCase(),h=t.name,c=l.normalize(h).replace(/^[\/\\]+/,""),y=l.join(r,c),k=l.resolve(y),g=l.resolve(r);if(!k.startsWith(g+l.sep)&&k!==g){i(e,"WARN","Unsafe file path, skipping",{filePath:c,resolvedPath:k});continue}switch(f){case"create":{await a.promises.mkdir(l.dirname(y),{recursive:!0});const p=t.contents||"";await a.promises.writeFile(y,p,"utf8"),i(e,"INFO","File created successfully",{filePath:c});break}case"delete":{a.existsSync(y)?(await a.promises.unlink(y),i(e,"INFO","File deleted successfully",{filePath:c})):i(e,"WARN","File to delete does not exist",{filePath:c});break}case"rename":{const p=t.renameFrom;if(!p||typeof p!="string"){i(e,"WARN","Rename operation missing renameFrom",{filePath:c});break}const v=l.normalize(p).replace(/^[\/\\]+/,""),N=l.join(r,v),E=l.resolve(N);if(!E.startsWith(g+l.sep)&&E!==g){i(e,"WARN","Unsafe rename source path, skipping",{renameFrom:v,resolvedPath:E});break}a.existsSync(N)?(await a.promises.mkdir(l.dirname(y),{recursive:!0}),await a.promises.rename(N,y),i(e,"INFO","File renamed successfully",{oldPath:v,newPath:c})):i(e,"WARN","File to rename does not exist",{renameFrom:v});break}case"modify":{if(!a.existsSync(y)){i(e,"WARN","File to modify does not exist",{filePath:c});break}const p=await a.promises.readFile(y,"utf8"),v=typeof t.contents=="string"?t.contents:"",{finalContent:N,changesCount:E}=L(p,v);if(E===0){i(e,"INFO","File content unchanged, skipping write",{filePath:c});break}await a.promises.writeFile(y,N,"utf8"),i(e,"INFO","File modified successfully",{filePath:c,changesCount:E});break}default:{i(e,"WARN","Unsupported operation type",{operation:f,filePath:c});break}}}i(e,"DEBUG","Start cleaning empty directories",{projectId:e});try{await C(r,R.TRAVERSE_EXCLUDE_DIRS||[])}catch(t){i(e,"WARN","Failed to clean empty directories",{projectId:e,error:t&&t.message})}return i(e,"INFO","Specified files updated successfully",{projectId:e,filesCount:n.length,elapsedMs:Date.now()-w}),{success:!0,message:"Specified files updated successfully",projectId:e,filesCount:n.length}}catch(t){throw i(e,"ERROR","Failed to process file operations",{projectId:e,error:t&&t.message,elapsedMs:Date.now()-w}),t}}catch(s){throw s.isOperational?s:new S("Failed to backup project",{projectId:e,originalError:s&&s.message})}}async function U(e,F,n,d,P={}){const w=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});const b=Number(F);if(!Number.isFinite(b))throw new m("codeVersion must be a number",{field:"codeVersion"});if(!Array.isArray(n))throw new m("files must be an array",{field:"files"});const r=x(e,P);if(!a.existsSync(r))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:r}),new O("Project does not exist",{projectId:e});let u="";try{const s=l.join(R.UPLOAD_PROJECT_DIR,e);a.existsSync(s)||a.mkdirSync(s,{recursive:!0});const o=`${e}-v${b}.zip`;u=l.join(s,o),i(e,"DEBUG","Start backing up project",{projectId:e,backupZipPath:u}),await D(e,r,u);try{i(e,"DEBUG","Start writing files",{projectId:e,filesCount:n.length});for(const t of n){if(!t||typeof t.name!="string")continue;const f=l.join(r,t.name);if(t.renameFrom&&typeof t.renameFrom=="string"){const p=l.join(r,t.renameFrom);if(a.existsSync(p)){await a.promises.mkdir(l.dirname(f),{recursive:!0}),await a.promises.rename(p,f),i(e,"INFO","File renamed successfully",{projectId:e,oldPath:t.renameFrom,newPath:t.name});continue}}const h=t.binary===!0,c=t.binary===!1,y=!!t.sizeExceeded,k=typeof t.contents=="string"&&t.contents.length>0;if(h){if(a.existsSync(f)){i(e,"INFO","Binary file already exists, skipping write",{filePath:t.name});continue}if(k)try{await a.promises.mkdir(l.dirname(f),{recursive:!0});const p=Buffer.from(t.contents,"base64");await a.promises.writeFile(f,p),i(e,"INFO","Binary file written successfully",{filePath:t.name})}catch(p){i(e,"ERROR","Failed to write binary file",{filePath:t.name,error:p&&p.message})}else i(e,"WARN","Binary file does not exist and has no content, skipping",{filePath:t.name});continue}c&&(!y||y&&k)&&(await a.promises.mkdir(l.dirname(f),{recursive:!0}),await a.promises.writeFile(f,t.contents||"","utf8"))}}catch(t){throw i(e,"ERROR","Failed to write files",{projectId:e,error:t&&t.message,elapsedMs:Date.now()-w}),t}try{i(e,"DEBUG","Start cleaning missing files and empty directories",{projectId:e});const t=new Set(n.filter(f=>f&&typeof f.name=="string").map(f=>l.normalize(f.name)));await V(r,t,R.TRAVERSE_EXCLUDE_DIRS||[]),await C(r,R.TRAVERSE_EXCLUDE_DIRS||[])}catch(t){throw i(e,"ERROR","Failed to clean missing files, starting rollback",{projectId:e,error:t&&t.message,elapsedMs:Date.now()-w}),t}return i(e,"INFO","Files submitted successfully",{projectId:e,filesCount:n.length,elapsedMs:Date.now()-w}),{success:!0,message:"Files submitted successfully",projectId:e,restarted:!1}}catch(s){throw s.isOperational?s:new S("Failed to backup old version",{projectId:e,originalError:s&&s.message})}}async function $(e,F,n,d,P,w={}){const b=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});const r=Number(F);if(!Number.isFinite(r))throw new m("codeVersion must be a number",{field:"codeVersion"});if(!n)throw new m("File cannot be empty",{field:"file"});if(!d||typeof d!="string")throw new m("File path cannot be empty",{field:"filePath"});const u=x(e,w);if(!a.existsSync(u))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:u}),new O("Project does not exist",{projectId:e});const s=l.normalize(d).replace(/^[\/\\]+/,""),o=l.join(u,s),t=l.resolve(o),f=l.resolve(u);if(!t.startsWith(f))throw new m("File path is not safe, cannot exceed project directory",{field:"filePath",providedPath:d,resolvedPath:t});let h="";try{const c=l.join(R.UPLOAD_PROJECT_DIR,e);a.existsSync(c)||a.mkdirSync(c,{recursive:!0});const y=`${e}-v${r}.zip`;h=l.join(c,y),i(e,"DEBUG","Start backing up project",{projectId:e,backupZipPath:h}),await D(e,u,h),i(e,"INFO",`Project backed up: ${h}`,{projectId:e,zipPath:h});try{if(i(e,"DEBUG","Start writing uploaded file",{projectId:e,filePath:s}),await a.promises.mkdir(l.dirname(o),{recursive:!0}),!n.buffer)throw new m("File content format is incorrect, missing buffer",{field:"file"});if(i(e,"INFO","Prepare to write file",{targetPath:o,bufferLength:n.buffer.length,expectedSize:n.size,bufferIsBuffer:Buffer.isBuffer(n.buffer),sizeMatch:n.buffer.length===n.size}),await a.promises.writeFile(o,n.buffer),i(e,"INFO","File uploaded successfully",{projectId:e,filePath:s,targetPath:t,fileSize:n.buffer?n.buffer.length:0,elapsedMs:Date.now()-b}),!1)try{const g=await B(P,e);return i(e,"INFO","Restart development server successfully",{projectId:e,pid:g.pid,port:g.port}),{success:!0,message:"File uploaded and restarted development server successfully",projectId:e,filePath:s,targetPath:t,fileSize:n.buffer?n.buffer.length:0,pid:g.pid,port:g.port,restarted:!0}}catch(g){i(e,"ERROR","Failed to restart development server",{projectId:e,filePath:s,error:g&&g.message})}else return i(e,"INFO","File modification does not require restarting development server",{projectId:e,filePath:s}),{success:!0,message:"File uploaded successfully, no need to restart development server",projectId:e,restarted:!1}}catch(k){throw i(e,"ERROR","Failed to write file",{projectId:e,filePath:s,error:k&&k.message,elapsedMs:Date.now()-b}),k}}catch(c){throw c.isOperational?c:new S("Failed to backup project",{projectId:e,filePath:s,originalError:c&&c.message})}}async function A(e,F,n,d,P={}){const w=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});const b=Number(F);if(!Number.isFinite(b))throw new m("codeVersion must be a number",{field:"codeVersion"});const r=Number(n);if(!Number.isFinite(r))throw new m("rollbackTo must be a number",{field:"rollbackTo"});if(r<0)throw new m("rollbackTo cannot be less than 0",{field:"rollbackTo"});if(r>=b)throw new m("rollbackTo must be less than current codeVersion",{field:"rollbackTo"});const u=x(e,P);if(!a.existsSync(u))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:u}),new O("Project does not exist",{projectId:e});const s=l.join(R.UPLOAD_PROJECT_DIR,e),o=`${e}-v${r}.zip`,t=l.join(s,o);if(!a.existsSync(t))throw i(e,"ERROR","Rollback version backup file does not exist",{projectId:e,rollbackTo:r,zipPath:t}),new O("Rollback version backup file does not exist",{projectId:e,rollbackTo:r});let f="";try{a.existsSync(s)||a.mkdirSync(s,{recursive:!0});const h=`${e}-v${b}.zip`;return f=l.join(s,h),a.existsSync(f)?i(e,"INFO","Current version backup already exists, skipping backup",{projectId:e,zipPath:f}):(await D(e,u,f),i(e,"INFO","Current version backed up",{projectId:e,zipPath:f})),i(e,"DEBUG","Start restoring project from backup",{projectId:e,rollbackToNum:r,rollbackZipPath:t}),await z(e,u,t),i(e,"INFO","Project rolled back successfully",{projectId:e,newVersion:b,toVersion:r,rollbackZipPath:t,elapsedMs:Date.now()-w}),{success:!0,message:"Project rolled back successfully",newVersion:b,rollbackTo:r}}catch(h){if(i(e,"ERROR","Failed to rollback project",{projectId:e,rollbackTo:r,error:h&&h.message,elapsedMs:Date.now()-w}),f&&a.existsSync(f))try{i(e,"INFO","Failed to rollback, trying to restore current version",{projectId:e,backupPath:f}),await z(e,u,f),i(e,"INFO","Current version restored",{projectId:e})}catch(c){i(e,"ERROR","Failed to restore current version",{projectId:e,error:c&&c.message})}throw h.isOperational?h:new S("Failed to rollback project",{projectId:e,rollbackTo:r,originalError:h&&h.message})}}export{T as specifiedFilesUpdate,U as allFilesUpdate,$ as uploadSingleFile,A as rollbackVersion};export default{specifiedFilesUpdate:T,allFilesUpdate:U,uploadSingleFile:$,rollbackVersion:A};
|
|
4
|
+
`;return{finalContent:n.join(h),changesCount:a}}function te(e,P){return e===P?{finalContent:e,changesCount:0}:{finalContent:P,changesCount:-1}}async function T(e,P,n,g,d={}){const y=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});if(P==null)throw new m("codeVersion cannot be empty",{field:"codeVersion"});const b=Number(P);if(!Number.isFinite(b))throw new m("codeVersion must be a number",{field:"codeVersion"});if(!Array.isArray(n))throw new m("files must be an array",{field:"files"});for(let s=0;s<n.length;s++){const o=n[s];if(!o||typeof o.operation!="string")throw new m(`files[${s}].operation cannot be empty`,{field:`files[${s}].operation`});if(!o.name||typeof o.name!="string")throw new m(`files[${s}].name cannot be empty`,{field:`files[${s}].name`});const t=o.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(t))throw new m(`files[${s}].operation must be one of create, delete, rename or modify`,{field:`files[${s}].operation`});if(t==="rename"&&!o.renameFrom)throw new m(`files[${s}].renameFrom cannot be empty (rename operation requires)`,{field:`files[${s}].renameFrom`});if(t==="modify"&&typeof o.contents!="string")throw new m(`files[${s}].contents must be a string (modify operation requires)`,{field:`files[${s}].contents`})}const a=x(e,d);if(!r.existsSync(a))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:a}),new D("Project does not exist",{projectId:e});let h="";try{const s=l.join(R.UPLOAD_PROJECT_DIR,e);r.existsSync(s)||r.mkdirSync(s,{recursive:!0});const o=`${e}-v${b}.zip`;h=l.join(s,o),i(e,"DEBUG","Start backing up project",{projectId:e,backupZipPath:h}),R.GIT_ENABLED||await S(e,a,h),i(e,"INFO","Project backed up successfully",{projectId:e,zipPath:h});try{i(e,"DEBUG","Start processing file operations",{projectId:e,filesCount:n.length});for(const t of n){const u=t.operation.toLowerCase(),c=t.name,f=l.normalize(c).replace(/^[\/\\]+/,""),w=l.join(a,f),k=l.resolve(w),F=l.resolve(a);if(!k.startsWith(F+l.sep)&&k!==F){i(e,"WARN","Unsafe file path, skipping",{filePath:f,resolvedPath:k});continue}switch(u){case"create":{if(t.isDir===!0)r.existsSync(w)?i(e,"INFO","Directory already exists, skipping creation",{filePath:f}):(await r.promises.mkdir(w,{recursive:!0}),i(e,"INFO","Directory created successfully",{filePath:f}));else{await r.promises.mkdir(l.dirname(w),{recursive:!0});const p=t.contents||"";await r.promises.writeFile(w,p,"utf8"),i(e,"INFO","File created successfully",{filePath:f})}break}case"delete":{r.existsSync(w)?(await r.promises.stat(w)).isDirectory()?(await r.promises.rm(w,{recursive:!0,force:!0}),i(e,"INFO","Directory deleted successfully",{filePath:f})):(await r.promises.unlink(w),i(e,"INFO","File deleted successfully",{filePath:f})):i(e,"WARN","File or directory to delete does not exist",{filePath:f});break}case"rename":{const p=t.renameFrom;if(!p||typeof p!="string"){i(e,"WARN","Rename operation missing renameFrom",{filePath:f});break}const v=l.normalize(p).replace(/^[\/\\]+/,""),O=l.join(a,v),N=l.resolve(O);if(!N.startsWith(F+l.sep)&&N!==F){i(e,"WARN","Unsafe rename source path, skipping",{renameFrom:v,resolvedPath:N});break}r.existsSync(O)?(await r.promises.mkdir(l.dirname(w),{recursive:!0}),await r.promises.rename(O,w),i(e,"INFO","File renamed successfully",{oldPath:v,newPath:f})):i(e,"WARN","File to rename does not exist",{renameFrom:v});break}case"modify":{if(!r.existsSync(w)){i(e,"WARN","File to modify does not exist",{filePath:f});break}const p=await r.promises.readFile(w,"utf8"),v=typeof t.contents=="string"?t.contents:"",{finalContent:O,changesCount:N}=L(p,v);if(N===0){i(e,"INFO","File content unchanged, skipping write",{filePath:f});break}await r.promises.writeFile(w,O,"utf8"),i(e,"INFO","File modified successfully",{filePath:f,changesCount:N});break}default:{i(e,"WARN","Unsupported operation type",{operation:u,filePath:f});break}}}return i(e,"INFO","Specified files updated successfully",{projectId:e,filesCount:n.length,elapsedMs:Date.now()-y}),{success:!0,message:"Specified files updated successfully",projectId:e,filesCount:n.length}}catch(t){throw i(e,"ERROR","Failed to process file operations",{projectId:e,error:t&&t.message,elapsedMs:Date.now()-y}),t}}catch(s){throw s.isOperational?s:new E("Failed to backup project",{projectId:e,originalError:s&&s.message})}}async function C(e,P,n,g,d={}){const y=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});const b=Number(P);if(!Number.isFinite(b))throw new m("codeVersion must be a number",{field:"codeVersion"});if(!Array.isArray(n))throw new m("files must be an array",{field:"files"});const a=x(e,d);if(!r.existsSync(a))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:a}),new D("Project does not exist",{projectId:e});let h="";try{const s=l.join(R.UPLOAD_PROJECT_DIR,e);r.existsSync(s)||r.mkdirSync(s,{recursive:!0});const o=`${e}-v${b}.zip`;h=l.join(s,o),i(e,"DEBUG","Start backing up project",{projectId:e,backupZipPath:h}),R.GIT_ENABLED||await S(e,a,h);try{i(e,"DEBUG","Start writing files",{projectId:e,filesCount:n.length});for(const t of n){if(!t||typeof t.name!="string")continue;const u=l.join(a,t.name);if(t.isDir===!0){r.existsSync(u)||(await r.promises.mkdir(u,{recursive:!0}),i(e,"INFO","Directory ensured",{filePath:t.name}));continue}if(t.renameFrom&&typeof t.renameFrom=="string"){const p=l.join(a,t.renameFrom);if(r.existsSync(p)){await r.promises.mkdir(l.dirname(u),{recursive:!0}),await r.promises.rename(p,u),i(e,"INFO","File renamed successfully",{projectId:e,oldPath:t.renameFrom,newPath:t.name});continue}}const c=t.binary===!0,f=t.binary===!1,w=!!t.sizeExceeded,k=typeof t.contents=="string"&&t.contents.length>0;if(c){if(r.existsSync(u)){i(e,"INFO","Binary file already exists, skipping write",{filePath:t.name});continue}if(k)try{await r.promises.mkdir(l.dirname(u),{recursive:!0});const p=Buffer.from(t.contents,"base64");await r.promises.writeFile(u,p),i(e,"INFO","Binary file written successfully",{filePath:t.name})}catch(p){i(e,"ERROR","Failed to write binary file",{filePath:t.name,error:p&&p.message})}else i(e,"WARN","Binary file does not exist and has no content, skipping",{filePath:t.name});continue}f&&(!w||w&&k)&&(await r.promises.mkdir(l.dirname(u),{recursive:!0}),await r.promises.writeFile(u,t.contents||"","utf8"))}}catch(t){throw i(e,"ERROR","Failed to write files",{projectId:e,error:t&&t.message,elapsedMs:Date.now()-y}),t}try{i(e,"DEBUG","Start cleaning missing files",{projectId:e});const t=new Set(n.filter(c=>c&&typeof c.name=="string").map(c=>l.normalize(c.name))),u=n.filter(c=>c&&typeof c.name=="string"&&c.isDir===!0);await U(a,t,R.TRAVERSE_EXCLUDE_DIRS||[]);for(const c of u){const f=l.join(a,c.name);r.existsSync(f)||(await r.promises.mkdir(f,{recursive:!0}),i(e,"INFO","Empty directory recreated",{dirPath:c.name}))}}catch(t){throw i(e,"ERROR","Failed to clean missing files, starting rollback",{projectId:e,error:t&&t.message,elapsedMs:Date.now()-y}),t}return i(e,"INFO","Files submitted successfully",{projectId:e,filesCount:n.length,elapsedMs:Date.now()-y}),{success:!0,message:"Files submitted successfully",projectId:e,restarted:!1}}catch(s){throw s.isOperational?s:new E("Failed to backup old version",{projectId:e,originalError:s&&s.message})}}async function B(e,P,n,g,d,y={}){const b=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});const a=Number(P);if(!Number.isFinite(a))throw new m("codeVersion must be a number",{field:"codeVersion"});if(!n)throw new m("File cannot be empty",{field:"file"});if(!g||typeof g!="string")throw new m("File path cannot be empty",{field:"filePath"});const h=x(e,y);if(!r.existsSync(h))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:h}),new D("Project does not exist",{projectId:e});const s=l.normalize(g).replace(/^[\/\\]+/,""),o=l.join(h,s),t=l.resolve(o),u=l.resolve(h);if(!t.startsWith(u))throw new m("File path is not safe, cannot exceed project directory",{field:"filePath",providedPath:g,resolvedPath:t});let c="";try{const f=l.join(R.UPLOAD_PROJECT_DIR,e);r.existsSync(f)||r.mkdirSync(f,{recursive:!0});const w=`${e}-v${a}.zip`;c=l.join(f,w),i(e,"DEBUG","Start backing up project",{projectId:e,backupZipPath:c}),R.GIT_ENABLED||await S(e,h,c),i(e,"INFO",`Project backed up: ${c}`,{projectId:e,zipPath:c});try{if(i(e,"DEBUG","Start writing uploaded file",{projectId:e,filePath:s}),await r.promises.mkdir(l.dirname(o),{recursive:!0}),!n.buffer)throw new m("File content format is incorrect, missing buffer",{field:"file"});if(i(e,"INFO","Prepare to write file",{targetPath:o,bufferLength:n.buffer.length,expectedSize:n.size,bufferIsBuffer:Buffer.isBuffer(n.buffer),sizeMatch:n.buffer.length===n.size}),await r.promises.writeFile(o,n.buffer),i(e,"INFO","File uploaded successfully",{projectId:e,filePath:s,targetPath:t,fileSize:n.buffer?n.buffer.length:0,elapsedMs:Date.now()-b}),!1)try{const F=await A(d,e);return i(e,"INFO","Restart development server successfully",{projectId:e,pid:F.pid,port:F.port}),{success:!0,message:"File uploaded and restarted development server successfully",projectId:e,filePath:s,targetPath:t,fileSize:n.buffer?n.buffer.length:0,pid:F.pid,port:F.port,restarted:!0}}catch(F){i(e,"ERROR","Failed to restart development server",{projectId:e,filePath:s,error:F&&F.message})}else return i(e,"INFO","File modification does not require restarting development server",{projectId:e,filePath:s}),{success:!0,message:"File uploaded successfully, no need to restart development server",projectId:e,restarted:!1}}catch(k){throw i(e,"ERROR","Failed to write file",{projectId:e,filePath:s,error:k&&k.message,elapsedMs:Date.now()-b}),k}}catch(f){throw f.isOperational?f:new E("Failed to backup project",{projectId:e,filePath:s,originalError:f&&f.message})}}async function $(e,P,n,g,d={}){const y=Date.now();if(!e)throw new m("Project ID cannot be empty",{field:"projectId"});const b=Number(P);if(!Number.isFinite(b))throw new m("codeVersion must be a number",{field:"codeVersion"});const a=Number(n);if(!Number.isFinite(a))throw new m("rollbackTo must be a number",{field:"rollbackTo"});if(a<0)throw new m("rollbackTo cannot be less than 0",{field:"rollbackTo"});if(a>=b)throw new m("rollbackTo must be less than current codeVersion",{field:"rollbackTo"});const h=x(e,d);if(!r.existsSync(h))throw i(e,"ERROR","Project does not exist",{projectId:e,projectPath:h}),new D("Project does not exist",{projectId:e});const s=l.join(R.UPLOAD_PROJECT_DIR,e),o=`${e}-v${a}.zip`,t=l.join(s,o);if(!r.existsSync(t))throw i(e,"ERROR","Rollback version backup file does not exist",{projectId:e,rollbackTo:a,zipPath:t}),new D("Rollback version backup file does not exist",{projectId:e,rollbackTo:a});let u="";try{r.existsSync(s)||r.mkdirSync(s,{recursive:!0});const c=`${e}-v${b}.zip`;return u=l.join(s,c),r.existsSync(u)?i(e,"INFO","Current version backup already exists, skipping backup",{projectId:e,zipPath:u}):(await S(e,h,u),i(e,"INFO","Current version backed up",{projectId:e,zipPath:u})),i(e,"DEBUG","Start restoring project from backup",{projectId:e,rollbackToNum:a,rollbackZipPath:t}),await z(e,h,t),i(e,"INFO","Project rolled back successfully",{projectId:e,newVersion:b,toVersion:a,rollbackZipPath:t,elapsedMs:Date.now()-y}),{success:!0,message:"Project rolled back successfully",newVersion:b,rollbackTo:a}}catch(c){if(i(e,"ERROR","Failed to rollback project",{projectId:e,rollbackTo:a,error:c&&c.message,elapsedMs:Date.now()-y}),u&&r.existsSync(u))try{i(e,"INFO","Failed to rollback, trying to restore current version",{projectId:e,backupPath:u}),await z(e,h,u),i(e,"INFO","Current version restored",{projectId:e})}catch(f){i(e,"ERROR","Failed to restore current version",{projectId:e,error:f&&f.message})}throw c.isOperational?c:new E("Failed to rollback project",{projectId:e,rollbackTo:a,originalError:c&&c.message})}}export{T as specifiedFilesUpdate,C as allFilesUpdate,B as uploadSingleFile,$ as rollbackVersion};export default{specifiedFilesUpdate:T,allFilesUpdate:C,uploadSingleFile:B,rollbackVersion:$};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import I from"path";import F from"fs";import O from"../appConfig/index.js";import{log as c}from"../utils/log/logUtils.js";import{ValidationError as p,BusinessError as E,SystemError as d,ResourceError as G}from"../utils/error/errorHandler.js";import{resolveProjectPath as et}from"../utils/common/projectPathUtils.js";import{getGitInstance as f,isGitRepo as st,ensureGitRepo as w,ensureGitignore as at}from"../utils/git/gitUtils.js";function m(i){const{workspaceType:e,projectId:s,isolationContext:a,userId:t,cId:r}=i||{};if(!e||!["pageApp","taskAgent"].includes(e))throw new p("workspaceType is required and must be pageApp or taskAgent",{field:"workspaceType"});if(e==="taskAgent"){if(!t||!r)throw new p("taskAgent mode requires userId and cId",{field:"userId/cId"});const n=I.join(O.COMPUTER_WORKSPACE_DIR,String(t),String(r));if(!F.existsSync(n))throw new G("Computer workspace does not exist",{userId:t,cId:r});return{targetPath:n,logId:`computer:${t}:${r}`}}if(!s)throw new p("pageApp mode requires projectId",{field:"projectId"});const o=et(s,a||{});if(!F.existsSync(o))throw new G("Project does not exist",{projectId:s});return{targetPath:o,logId:s}}async function C(i={}){const{targetPath:e,logId:s}=m(i);if(st(e))return{success:!0,message:"Git repository already initialized",logId:s,alreadyExists:!0};try{const a=f(e);await a.init(),at(e);const t=O.GIT_DEFAULT_AUTHOR_NAME,r=O.GIT_DEFAULT_AUTHOR_EMAIL;return await a.addConfig("user.name",t,!1,"local"),await a.addConfig("user.email",r,!1,"local"),c(s,"INFO","Git repository initialized",{logId:s,targetPath:e}),{success:!0,message:"Git repository initialized successfully",logId:s,alreadyExists:!1}}catch(a){throw c(s,"ERROR","Failed to initialize Git repository",{logId:s,error:a.message}),new d("Failed to initialize Git repository",{originalError:a.message})}}async function _(i={}){const{targetPath:e,logId:s}=m(i);await w(e);try{const a=f(e),t=await a.status(),r=await a.raw(["status","--porcelain"]),o=[],n=[],l=[],h=[],y=[];if(r)for(const g of r.split(`
|
|
2
|
+
`).filter(Boolean)){const u=g[0],b=g[1],R=g.substring(3);u==="M"?o.push(R):u==="A"?(o.push(R),l.push(R)):u==="D"?(o.push(R),h.push(R)):(u==="R"||u==="C")&&o.push(R),b==="M"?n.push(R):b==="D"?(n.push(R),h.push(R)):b==="?"&&y.push(R)}return{success:!0,logId:s,current:t.current,staged:o,modified:n,created:l,deleted:h,untracked:y,conflicted:t.conflicted,ahead:t.ahead,behind:t.behind,tracking:t.tracking}}catch(a){throw c(s,"ERROR","Failed to get Git status",{logId:s,error:a.message}),new d("Failed to get Git status",{originalError:a.message})}}async function S(i={}){const{message:e,files:s,authorName:a,authorEmail:t}=i;if(!e||typeof e!="string")throw new p("Commit message cannot be empty",{field:"message"});const{targetPath:r,logId:o}=m(i);await w(r);try{const n=f(r);if(Array.isArray(s)&&s.length>0){const y=s.filter(g=>F.existsSync(I.join(r,g)));y.length>0&&await n.add(y)}else await n.add("--all");const l=[];if(a||t){const y=a||O.GIT_DEFAULT_AUTHOR_NAME,g=t||O.GIT_DEFAULT_AUTHOR_EMAIL;l.push(`--author=${y} <${g}>`)}const h=await n.commit(e,l);return c(o,"INFO","Git commit successful",{logId:o,commitHash:h.commit,message:e}),{success:!0,message:"Commit successful",logId:o,commit:h.commit,summary:h.summary}}catch(n){if(n.message&&n.message.includes("nothing to commit"))return{success:!0,message:"Nothing to commit",logId:o,nothingToCommit:!0};throw c(o,"ERROR","Failed to commit",{logId:o,error:n.message}),new d("Failed to commit",{originalError:n.message})}}async function B(i={}){const{files:e}=i,{targetPath:s,logId:a}=m(i);await w(s);try{const t=f(s);return Array.isArray(e)&&e.length>0?await t.add(e):await t.add("--all"),c(a,"INFO","Git add successful",{logId:a,filesCount:e?e.length:"all"}),{success:!0,message:"Files staged successfully",logId:a}}catch(t){throw c(a,"ERROR","Failed to add files",{logId:a,error:t.message}),new d("Failed to add files",{originalError:t.message})}}async function $(i={}){const{files:e}=i,{targetPath:s,logId:a}=m(i);await w(s);try{const t=f(s);return Array.isArray(e)&&e.length>0?(await t.raw(["restore","--staged","--",...e]),c(a,"INFO","Git unstage specified files",{logId:a,files:e}),{success:!0,message:"Specified files unstaged successfully",logId:a,files:e}):(await t.raw(["restore","--staged","."]),c(a,"INFO","Git unstage all files",{logId:a}),{success:!0,message:"All files unstaged successfully",logId:a,files:"all"})}catch(t){throw c(a,"ERROR","Failed to unstage",{logId:a,error:t.message}),new d("Failed to unstage files",{originalError:t.message})}}async function rt(i,e){const s=new Set;for(const t of e){let r=I.dirname(t);for(;r&&r!==".";)s.add(r),r=I.dirname(r)}const a=[...s].sort((t,r)=>{const o=t.split(/[/\\]/).length;return r.split(/[/\\]/).length-o});for(const t of a){const r=I.join(i,t);try{(await F.promises.readdir(r)).length===0&&await F.promises.rmdir(r)}catch{}}}async function v(i={}){const{files:e}=i,{targetPath:s,logId:a}=m(i);await w(s);try{const t=f(s),r=await t.status(),o=new Set([...r.created]),n=Array.isArray(e)&&e.length>0?e:null,l=[],h=[];if(n)for(const g of n)o.has(g)?l.push(g):h.push(g);else{for(const u of r.created)l.push(u);const g=new Set([...r.staged,...r.modified,...r.deleted]);for(const u of g)o.has(u)||h.push(u)}if(h.length>0&&await t.raw(["restore","--staged","--worktree","--",...h]),l.length>0){await t.raw(["rm","--cached","-f","--",...l]);for(const g of l){const u=I.join(s,g);F.existsSync(u)&&await F.promises.unlink(u)}await rt(s,l)}const y=h.length+l.length;return c(a,"INFO","Git discard",{logId:a,trackedFiles:h.length,newFiles:l.length}),{success:!0,message:"Files discarded successfully",logId:a,discardedCount:y,trackedFiles:h,newFiles:l}}catch(t){throw c(a,"ERROR","Failed to discard",{logId:a,error:t.message}),new d("Failed to discard files",{originalError:t.message})}}async function D(i={}){const{maxCount:e=50,branch:s,skip:a=0}=i,{targetPath:t,logId:r}=m(i);await w(t);try{const o=f(t),n=Math.min(Math.max(1,e),500),l=Math.max(0,a),h=[];h.push("--reflog"),s&&h.push(s),h.push(`--max-count=${n}`),l>0&&h.push(`--skip=${l}`);const g=(await o.log(h)).all.map(u=>({hash:u.hash,date:u.date,message:u.message,author_name:u.author_name,author_email:u.author_email}));return{success:!0,logId:r,commits:g,total:g.length}}catch(o){throw c(r,"ERROR","Failed to get Git log",{logId:r,error:o.message}),new d("Failed to get Git log",{originalError:o.message})}}async function L(i={}){const{source:e="worktree",from:s,to:a,paths:t}=i,{targetPath:r,logId:o}=m(i);await w(r);try{const n=f(r);let l=[];switch(e){case"staged":l.push("--cached");break;case"commit":if(s&&a)l.push(`${s}..${a}`);else if(s)if((await n.raw(["log","--format=%P","-1",s])).trim())l.push(`${s}^..${s}`);else{const u=Array.isArray(t)&&t.length>0?["--",...t]:[],[b,R]=await Promise.all([n.raw(["diff-tree","-p",s,...u]),n.raw(["diff-tree","--numstat","-r",s,...u])]),N=[];let T=0,x=0;for(const tt of R.split(`
|
|
3
|
+
`).filter(Boolean)){const A=tt.split(" ");if(A.length===3){const P=parseInt(A[0],10)||0,k=parseInt(A[1],10)||0;N.push({file:A[2],changes:P+k,insertions:P,deletions:k,binary:!1}),T+=P,x+=k}}return{success:!0,logId:o,source:e,diff:b,summary:{files:N,insertions:T,deletions:x}}}break;case"worktree":default:l.push("HEAD");break}Array.isArray(t)&&t.length>0&&l.push("--",...t);const[h,y]=await Promise.all([n.diff(l),n.diffSummary(l)]);return{success:!0,logId:o,source:e,diff:h,summary:{files:y.files.map(g=>({file:g.file,changes:g.changes,insertions:g.insertions,deletions:g.deletions,binary:g.binary})),insertions:y.insertions,deletions:y.deletions}}}catch(n){throw c(o,"ERROR","Failed to get Git diff",{logId:o,error:n.message}),new d("Failed to get Git diff",{originalError:n.message})}}async function M(i={}){const{ref:e="HEAD",filePath:s}=i;if(!s)throw new p("filePath is required",{field:"filePath"});const{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a);let o;if(e==="worktree"){const n=I.join(a,s);F.existsSync(n)?o=F.readFileSync(n,"utf-8"):o=""}else if(e==="staged"||e==="")try{o=await r.show([`:${s}`])}catch{o=""}else try{o=await r.show([`${e}:${s}`])}catch{o=""}return{success:!0,logId:t,filePath:s,ref:e,content:o}}catch(r){throw r instanceof G?r:(c(t,"ERROR","Failed to get file content",{logId:t,ref:e,filePath:s,error:r.message}),new d("Failed to get file content",{originalError:r.message}))}}async function H(i={}){const{target:e,mode:s="mixed"}=i;if(!e)throw new p("Reset target cannot be empty",{field:"target"});if(!["soft","mixed","hard"].includes(s))throw new p("Mode must be soft, mixed or hard",{field:"mode"});const{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a),o=await r.log({maxCount:1}),n=o.latest?o.latest.hash:null;return await r.reset([`--${s}`,e]),c(t,"INFO","Git reset successful",{logId:t,target:e,mode:s,previousHead:n}),{success:!0,message:`Reset (${s}) to ${e} successful`,logId:t,target:e,mode:s,previousHead:n}}catch(r){throw c(t,"ERROR","Failed to reset",{logId:t,target:e,mode:s,error:r.message}),new d("Failed to reset",{originalError:r.message})}}async function j(i={}){const{target:e}=i;if(!e)throw new p("Checkout target cannot be empty",{field:"target"});const{targetPath:s,logId:a}=m(i);await w(s);try{return await f(s).raw(["checkout",e,"--","."]),c(a,"INFO","Git checkout files successful",{logId:a,target:e}),{success:!0,message:`Checkout files from ${e} successful`,logId:a,target:e}}catch(t){throw c(a,"ERROR","Failed to checkout files",{logId:a,target:e,error:t.message}),new d("Failed to checkout files",{originalError:t.message})}}async function U(i={}){const{target:e}=i;if(!e)throw new p("Revert target cannot be empty",{field:"target"});const{targetPath:s,logId:a}=m(i);await w(s);try{const t=f(s),r=await t.status();if(!r.isClean())throw new E("\u5DE5\u4F5C\u533A\u6709\u672A\u63D0\u4EA4\u7684\u4FEE\u6539\uFF0C\u8BF7\u5148 commit \u6216 stash \u540E\u518D revert",{staged:r.staged,modified:r.modified});return await t.revert(e),c(a,"INFO","Git revert successful",{logId:a,target:e}),{success:!0,message:`Revert ${e} successful`,logId:a,target:e}}catch(t){if(t instanceof E)throw t;try{await git.raw(["revert","--abort"])}catch{}throw t.message&&t.message.includes("CONFLICT")?new E("Revert \u4EA7\u751F\u51B2\u7A81\uFF0C\u65E0\u6CD5\u81EA\u52A8\u5408\u5E76\uFF0C\u8BE5 commit \u6682\u4E0D\u652F\u6301 revert",{target:e}):(c(a,"ERROR","Failed to revert",{logId:a,target:e,error:t.message}),new d("Failed to revert",{originalError:t.message}))}}async function z(i={}){const{targetPath:e,logId:s}=m(i);await w(e);try{const t=await f(e).tags();return{success:!0,logId:s,tags:t.all,latest:t.latest}}catch(a){throw c(s,"ERROR","Failed to list tags",{logId:s,error:a.message}),new d("Failed to list tags",{originalError:a.message})}}async function q(i={}){const{tagName:e,message:s}=i;if(!e)throw new p("Tag name cannot be empty",{field:"tagName"});const{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a);return s?await r.addAnnotatedTag(e,s):await r.addTag(e),c(t,"INFO","Git tag created",{logId:t,tagName:e,annotated:!!s}),{success:!0,message:"Tag created successfully",logId:t,tagName:e}}catch(r){throw c(t,"ERROR","Failed to create tag",{logId:t,tagName:e,error:r.message}),new d("Failed to create tag",{originalError:r.message})}}async function W(i={}){const{tagName:e}=i;if(!e)throw new p("Tag name cannot be empty",{field:"tagName"});const{targetPath:s,logId:a}=m(i);await w(s);try{return await f(s).tag(["-d",e]),c(a,"INFO","Git tag deleted",{logId:a,tagName:e}),{success:!0,message:"Tag deleted successfully",logId:a,tagName:e}}catch(t){throw c(a,"ERROR","Failed to delete tag",{logId:a,tagName:e,error:t.message}),new d("Failed to delete tag",{originalError:t.message})}}async function K(i={}){const{targetPath:e,logId:s}=m(i);await w(e);try{const t=await f(e).branch();return{success:!0,logId:s,branches:t.branches,current:t.current}}catch(a){throw c(s,"ERROR","Failed to list branches",{logId:s,error:a.message}),new d("Failed to list branches",{originalError:a.message})}}async function V(i={}){const{branchName:e,startPoint:s}=i;if(!e)throw new p("Branch name cannot be empty",{field:"branchName"});const{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a);return s?await r.checkoutBranch(e,s):await r.checkoutLocalBranch(e),c(t,"INFO","Git branch created",{logId:t,branchName:e,startPoint:s}),{success:!0,message:"Branch created and switched to",logId:t,branchName:e}}catch(r){throw c(t,"ERROR","Failed to create branch",{logId:t,branchName:e,error:r.message}),new d("Failed to create branch",{originalError:r.message})}}async function J(i={}){const{branchName:e}=i;if(!e)throw new p("Branch name cannot be empty",{field:"branchName"});const{targetPath:s,logId:a}=m(i);await w(s);try{const t=f(s),r=await t.status();if(!r.isClean())throw new E("Working directory is not clean, please commit or stash your changes before switching branches",{staged:r.staged,modified:r.modified,untracked:r.not_added});return await t.checkout(e),c(a,"INFO","Git branch switched",{logId:a,branchName:e}),{success:!0,message:"Branch switched successfully",logId:a,branchName:e}}catch(t){throw t instanceof E?t:(c(a,"ERROR","Failed to switch branch",{logId:a,branchName:e,error:t.message}),new d("Failed to switch branch",{originalError:t.message}))}}async function Q(i={}){const{branchName:e,force:s=!1}=i;if(!e)throw new p("Branch name cannot be empty",{field:"branchName"});const{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a);if((await r.status()).current===e)throw new E("Cannot delete the current branch, please switch to another branch first");return s?await r.deleteLocalBranch(e,!0):await r.deleteLocalBranch(e),c(t,"INFO","Git branch deleted",{logId:t,branchName:e,force:s}),{success:!0,message:"Branch deleted successfully",logId:t,branchName:e}}catch(r){throw r instanceof E?r:(c(t,"ERROR","Failed to delete branch",{logId:t,branchName:e,error:r.message}),new d("Failed to delete branch",{originalError:r.message}))}}async function X(i={}){const{message:e,files:s}=i,{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a),o=["push"];return e&&o.push("-m",e),Array.isArray(s)&&s.length>0&&(await r.add(s),o.push("--",...s)),await r.stash(o),c(t,"INFO","Git stash push",{logId:t,stashMessage:e}),{success:!0,message:"Changes stashed successfully",logId:t}}catch(r){throw c(t,"ERROR","Failed to stash",{logId:t,error:r.message}),new d("Failed to stash changes",{originalError:r.message})}}async function Y(i={}){const{index:e,files:s}=i,{targetPath:a,logId:t}=m(i);await w(a);try{const r=f(a),o=await r.stashList().catch(()=>({all:[],total:0}));if((o.all&&o.all.length||o.total||0)===0)throw new E("No stash entries to restore");const l=e!=null?`stash@{${Number(e)}}`:"stash@{0}";return Array.isArray(s)&&s.length>0?(await r.raw(["checkout",l,"--",...s]),c(t,"INFO","Git stash pop (partial)",{logId:t,index:e,files:s}),{success:!0,message:"Stash restored (partial)",logId:t,index:e,files:s}):(await r.stash(["pop",l]),c(t,"INFO","Git stash pop",{logId:t,index:e}),{success:!0,message:"Stash restored successfully",logId:t})}catch(r){throw r instanceof E?r:(c(t,"ERROR","Failed to pop stash",{logId:t,index:e,error:r.message}),new d("Failed to pop stash",{originalError:r.message}))}}async function Z(i={}){const{targetPath:e,logId:s}=m(i);await w(e);try{const a=f(e),t=await a.stashList(),r=[];for(let o=0;o<t.all.length;o++){const n=t.all[o];let l=[];try{l=(await a.raw(["stash","show",`stash@{${o}}`,"--name-only"])).trim().split(`
|
|
4
|
+
`).filter(Boolean)}catch{}r.push({index:o,hash:n.hash,message:n.message,date:n.date,files:l})}return{success:!0,logId:s,stashes:r,total:r.length}}catch(a){throw c(s,"ERROR","Failed to list stashes",{logId:s,error:a.message}),new d("Failed to list stashes",{originalError:a.message})}}export{C as init,_ as status,S as commit,B as add,$ as unstage,v as discard,D as logHistory,L as diff,M as fileContent,H as reset,j as checkout,U as revert,z as listTags,q as createTag,W as deleteTag,K as listBranches,V as createBranch,J as switchBranch,Q as deleteBranch,X as stashPush,Y as stashPop,Z as stashList};export default{init:C,status:_,commit:S,add:B,unstage:$,discard:v,logHistory:D,diff:L,fileContent:M,reset:H,checkout:j,revert:U,listTags:z,createTag:q,deleteTag:W,listBranches:K,createBranch:V,switchBranch:J,deleteBranch:Q,stashPush:X,stashPop:Y,stashList:Z};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{log as s}from"../utils/log/logUtils.js";import P from"../appConfig/index.js";import u from"path";import i from"fs";import{extractZip as T}from"../utils/common/zipUtils.js";import"../utils/build/startDevUtils.js";import"../utils/build/restartDevUtils.js";import{stopDevServer as M}from"../utils/build/stopDevUtils.js";import{ValidationError as h,BusinessError as B,SystemError as R,FileError as b,ResourceError as _}from"../utils/error/errorHandler.js";import{sanitizeSensitivePaths as C}from"../utils/common/sensitiveUtils.js";import{removeNodeModules as Y}from"../utils/buildDependency/dependencyManager.js";import{backupProjectToZip as j,copyDirectoryFiltered as I}from"../utils/project/backupUtils.js";import{createPnpmNpmrc as W}from"../utils/common/npmrcUtils.js";import{resolveProjectPath as v}from"../utils/common/projectPathUtils.js";import{ensurePrimaryAgentDirs as ee,syncAgents as te}from"../utils/common/AgentWorkspaceUtils.js";async function L(e,t="react",l={}){const a=Date.now();if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});const r=["react","vue3"];if(!r.includes(t))throw new h("Template type is invalid, only supports react or vue3",{field:"templateType",templateType:t,allowedValues:r});const c={react:P.INIT_PROJECT_NAME_REACT||"react-vite-template",vue3:P.INIT_PROJECT_NAME_VUE3||"vue3-vite-template"}[t],w=v(e,l);if(i.existsSync(w))throw new B(`Project directory ${e} already exists`,{projectId:e,projectPath:w});try{i.mkdirSync(w,{recursive:!0}),s(e,"INFO",`Project directory created successfully: ${w}`,{projectId:e});const f=P.INIT_PROJECT_DIR,y=u.join(f,`${c}.zip`),m=u.join(f,c);if(s(e,"DEBUG","Start checking template directory",{templateDir:m,templateZipPath:y}),!i.existsSync(m)){if(!i.existsSync(y))throw s(e,"ERROR",`Initialization template does not exist: ${y}`,{projectId:e,templateZipPath:y}),new _("Initialization template does not exist",{});if(s(e,"INFO",`Template directory does not exist, starting to unzip template: ${y}`,{projectId:e,templateZipPath:y}),await T(y,m),s(e,"INFO","Template unzip completed",{projectId:e}),!i.existsSync(m))throw new R("Template unzip directory still does not exist",{})}s(e,"DEBUG","Start copying template content to project directory",{templateDir:m,projectPath:w});const d=await i.promises.readdir(m,{withFileTypes:!0});for(const D of d){const n=u.join(m,D.name),p=u.join(w,D.name);D.isDirectory()?(await i.promises.mkdir(p,{recursive:!0}),await I(n,p)):D.isFile()&&(await i.promises.mkdir(u.dirname(p),{recursive:!0}),await i.promises.copyFile(n,p))}return s(e,"DEBUG","Start creating .npmrc configuration file",{projectPath:w}),await W(w,e),s(e,"INFO",`Project ${e} initialized successfully`,{projectId:e,elapsedMs:Date.now()-a}),{success:!0,message:`Project ${e} created successfully`,projectPath:w}}catch(f){throw s(e,"ERROR",`Project ${e} initialization failed: ${f.message}`,{projectId:e,elapsedMs:Date.now()-a}),new R(`Project ${e} initialization failed: ${f.message}`,{projectId:e,projectPath:w,originalError:f.message})}}async function ie(e){const t=await i.promises.readdir(e,{withFileTypes:!0}),l=P.TOP_LEVEL_NOISE_PATTERNS,a=t.filter(r=>{const o=r.name;return o.startsWith(".")?!1:!l.some(c=>c.endsWith("*")?o.startsWith(c.slice(0,-1)):o===c)});if(a.length===1&&a[0].isDirectory()){const r=u.join(e,a[0].name),o=u.join(e,"..",`temp_${Date.now()}`);await i.promises.rename(r,o);const c=await i.promises.readdir(o);for(const w of c){const f=u.join(o,w),y=u.join(e,w);await i.promises.rename(f,y)}await i.promises.rmdir(o)}}async function A(e,t={}){if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});const l=v(e,t);if(i.existsSync(l))try{s(e,"INFO",`Start cleaning project directory: ${l}`,{projectId:e}),await i.promises.rm(l,{recursive:!0,force:!0}),s(e,"INFO",`Project directory cleaned up: ${l}`,{projectId:e})}catch(a){throw s(e,"ERROR",`Failed to clean project directory: ${a.message}`,{projectId:e,projectPath:l,originalError:a.message}),new R(`Failed to clean project directory: ${a.message}`,{projectId:e,projectPath:l,originalError:a.message})}else s(e,"INFO",`Project directory does not exist, no need to clean: ${l}`,{projectId:e})}function se(e){if(!i.existsSync(e))return!0;try{return i.readdirSync(e).filter(a=>!a.startsWith(".")&&a!=="node_modules").length===0}catch(t){const l=u.basename(e);return s(l,"ERROR",`Failed to check if directory is empty: ${t.message}`,{dirPath:e}),!0}}async function G(e,t,l,a,r,o,c={}){const w=Date.now(),f=v(e,c);try{if(se(f))s(e,"INFO","Project directory is empty, directly deploying new project",{projectId:e});else{s(e,"INFO","Project directory is not empty, starting to backup current version",{projectId:e});const m=parseInt(a)-1,d=u.join(P.UPLOAD_PROJECT_DIR,e),D=u.join(d,`${e}-v${m}.zip`);if(i.existsSync(D))s(e,"INFO",`Backup file already exists, skipping backup: ${D}`,{projectId:e});else try{await $(e,m,c),s(e,"INFO",`Current version backed up: ${D}`,{projectId:e})}catch(n){throw s(e,"ERROR",`Failed to backup current version: ${n.message}`,{projectId:e}),new R(`Failed to backup current version: ${n.message}`,{projectId:e,originalError:n.message})}if(r&&!isNaN(Number(r))){const n=Number(r);s(e,"INFO",`Stopping old version dev server, PID: ${n}`,{projectId:e});try{await M(l,e,n,{strict:!0}),s(e,"INFO","Old version dev server stopped",{projectId:e})}catch(p){s(e,"WARN",`Failed to stop old version dev server: ${p.message}`,{projectId:e,pid:n})}}i.existsSync(f)&&(s(e,"INFO",`Cleaning project directory: ${f}`,{projectId:e}),await i.promises.rm(f,{recursive:!0,force:!0}))}return i.mkdirSync(f,{recursive:!0}),s(e,"INFO",`Project directory created successfully: ${f}`,{projectId:e}),s(e,"DEBUG","Start extracting zip file to project directory",{projectId:e,zipFilePath:t}),await T(t,f),s(e,"DEBUG","Zip file extracted successfully",{projectId:e}),s(e,"DEBUG","Check and remove top level folder",{projectId:e}),await ie(f),s(e,"DEBUG","Check and remove node_modules folder",{projectId:e}),await Y(f),s(e,"DEBUG","Start creating .npmrc configuration file",{projectId:e}),await W(f,e),s(e,"INFO",`Project ${e} uploaded successfully`,{projectId:e,codeVersion:a,elapsedMs:Date.now()-w}),{success:!0,message:`Project ${e} uploaded successfully`,projectId:e,codeVersion:a}}catch(y){s(e,"ERROR",`Failed to upload project: ${y.message}`,{projectId:e,elapsedMs:Date.now()-w});try{await A(e,c),s(e,"INFO","Failed to upload project, project directory cleaned up",{projectId:e})}catch(m){s(e,"ERROR",`Failed to clean project directory: ${m.message}`,{projectId:e,originalError:m.message})}throw y.isOperational?y:new R(`Failed to upload project: ${y.message}`,{projectId:e,projectPath:f,zipFilePath:t,originalError:y.message})}}async function $(e,t,l={}){if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});if(t==null)throw new h("codeVersion cannot be empty",{field:"codeVersion"});const a=Number(t);if(!Number.isFinite(a))throw new h("codeVersion must be a number",{field:"codeVersion"});const r=v(e,l);if(!i.existsSync(r))throw new _("Project does not exist",{projectId:e});const o=u.join(P.UPLOAD_PROJECT_DIR,e);i.existsSync(o)||i.mkdirSync(o,{recursive:!0});const c=`${e}-v${a}.zip`,w=u.join(o,c);return s(e,"DEBUG","Start backing up project to zip",{projectId:e,versionNum:a,outZipPath:w}),await j(e,r,w)}async function J(e,t,l){if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});if(!t)throw new h("codeVersion cannot be empty",{field:"codeVersion"});if(!l)throw new h("Please upload a zip file",{field:"zipFile"});const a=u.join(P.UPLOAD_PROJECT_DIR,e);i.existsSync(a)||i.mkdirSync(a,{recursive:!0});const r=l.path,o=u.join(a,`${e}-v${t}.zip`);try{return i.renameSync(r,o),s(e,"INFO","File saved successfully",{projectId:e,codeVersion:t,filePath:o}),{success:!0,filePath:o}}catch(c){if(s(e,"ERROR","Failed to move file",{projectId:e,codeVersion:t,error:c.message}),i.existsSync(r))try{i.unlinkSync(r)}catch(w){s(e,"ERROR","Failed to clean temporary file",{projectId:e,error:w.message})}throw new R("Failed to save file",{projectId:e,codeVersion:t,originalError:c.message})}}function re(e){if(!e)return[];if(Array.isArray(e))return e.map(t=>typeof t=="string"?t.trim():"").filter(Boolean);if(typeof e=="string"){const t=e.trim();if(!t)return[];try{const l=JSON.parse(t);if(Array.isArray(l))return l.map(a=>typeof a=="string"?a.trim():"").filter(Boolean)}catch{}return[t]}return[]}async function Z(e){i.existsSync(e)&&await i.promises.rm(e,{recursive:!0,force:!0})}async function V(e,t){try{await i.promises.rename(e,t)}catch(l){if(l.code==="EXDEV"){async function a(r,o){if((await i.promises.lstat(r)).isDirectory()){await i.promises.mkdir(o,{recursive:!0});const w=await i.promises.readdir(r);for(const f of w)await a(u.join(r,f),u.join(o,f))}else await i.promises.copyFile(r,o)}await a(e,t),await i.promises.rm(e,{recursive:!0,force:!0})}else throw l}}async function ae(e,t){const l=u.join(e,t);if(i.existsSync(l)&&(await i.promises.lstat(l)).isDirectory())return l;const a=await i.promises.readdir(e,{withFileTypes:!0});for(const r of a){if(!r.isDirectory())continue;const o=u.join(e,r.name,t);if(i.existsSync(o)&&(await i.promises.lstat(o)).isDirectory())return o}return null}async function ne(e,t,l){let a;try{a=await fetch(e)}catch(w){throw new b(`Failed to download skill zip from url: ${e}`,{url:e,reason:w.message})}if(!a.ok)throw new b(`Failed to download skill zip from url: ${e}`,{url:e,status:a.status,statusText:a.statusText});const r=a.headers.get("content-type")||"";r&&!r.includes("zip")&&!r.includes("octet-stream")&&s(l,"WARN","Downloaded skill url content-type is not typical zip",{url:e,contentType:r});const o=await a.arrayBuffer(),c=Buffer.from(o);await i.promises.writeFile(t,c)}async function X(e,t,l,a={}){const r=String(e),o=Date.now(),c=re(l),w=[],f=[];if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});if(!t&&c.length===0)throw new h("file or skillUrls cannot both be empty",{field:"file|skillUrls"});if(t){if(!t.path)throw new h("Uploaded file has no valid path",{field:"file.path"});if(u.extname(t.originalname||t.filename||"").toLowerCase()!==".zip")throw new h("Only zip files are supported",{field:"file",originalName:t?.originalname})}const y=v(String(e),a);if(!i.existsSync(y))throw new h("Project does not exist",{field:"projectId"});const{skillsDir:m,agentTypes:d}=await ee(y),D=u.join(P.UPLOAD_PROJECT_DIR,"temp");try{await i.promises.mkdir(m,{recursive:!0}),await i.promises.mkdir(D,{recursive:!0});const n=new Set;if(t){const k=u.join(D,`project_skill_push_${Date.now()}_${Math.round(Math.random()*1e6)}`);f.push(k),await i.promises.mkdir(k,{recursive:!0}),await T(t.path,k);const N=await ae(k,"skills");if(!N)s(r,"WARN","skills directory not found in uploaded zip",{projectId:e,extractRoot:k});else{const E=(await i.promises.readdir(N,{withFileTypes:!0})).filter(F=>F.isDirectory()&&!F.name.startsWith("."));for(const F of E){const S=u.join(N,F.name),x=u.join(m,F.name);i.existsSync(x)&&await Z(x),await V(S,x),n.add(F.name)}}}for(let k=0;k<c.length;k+=1){const N=c[k],O=u.join(D,`project_skill_url_${Date.now()}_${k}_${Math.round(Math.random()*1e6)}.zip`);w.push(O);const E=u.join(D,`project_skill_url_extract_${Date.now()}_${k}_${Math.round(Math.random()*1e6)}`);f.push(E),await i.promises.mkdir(E,{recursive:!0}),await ne(N,O,r),await T(O,E);const S=(await i.promises.readdir(E,{withFileTypes:!0})).filter(g=>g.isDirectory()&&!g.name.startsWith(".")),U=S.find(g=>g.name==="skills")?(await i.promises.readdir(u.join(E,"skills"),{withFileTypes:!0})).filter(g=>g.isDirectory()&&!g.name.startsWith(".")).map(g=>({name:g.name,sourcePath:u.join(E,"skills",g.name)})):S.map(g=>({name:g.name,sourcePath:u.join(E,g.name)}));if(U.length===0){s(r,"WARN","No skill directory found after extracting skill url zip",{projectId:e,skillUrl:N,extractRoot:E});continue}for(const g of U){const z=u.join(m,g.name);i.existsSync(z)&&await Z(z),await V(g.sourcePath,z),n.add(g.name)}}const p=Array.from(n),Q=p.length>0?`Pushed ${p.length} skills: ${p.join(", ")}`:"No valid skill directories found in file or skillUrls";return s(r,"INFO","Push skills to project workspace completed",{projectId:e,updatedSkills:p,skillUrlsCount:c.length,hasFile:!!t,agentTypes:d,elapsedMs:Date.now()-o}),await te(y),{message:Q,projectPath:y,updatedSkills:p}}catch(n){throw s(r,"ERROR","Push skills to project workspace failed",{projectId:e,error:n.message,elapsedMs:Date.now()-o}),n instanceof h||n instanceof b||n instanceof B||n instanceof R?n:new R(`Failed to push skills to project workspace: ${n.message}`,{projectId:e})}finally{for(const n of f)try{i.existsSync(n)&&await i.promises.rm(n,{recursive:!0,force:!0})}catch(p){s(r,"WARN","Failed to clean extracted skill temp dir",{extractRoot:n,error:p.message})}for(const n of w)try{i.existsSync(n)&&await i.promises.unlink(n)}catch(p){s(r,"WARN","Failed to clean downloaded skill zip",{zipPath:n,error:p.message})}try{t?.path&&i.existsSync(t.path)&&await i.promises.unlink(t.path)}catch(n){s(r,"WARN","Failed to clean uploaded skill zip",{tempZipPath:t?.path,error:n.message})}}}async function q(e,t,l,a={}){const r=Date.now();if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});const o=null;try{if(t&&!isNaN(Number(t))){const m=Number(t);s(o,"INFO",`[delete-project] Stopping development server, PID: ${m}`,{projectId:e,pid:m});try{await M(l,e,m,{strict:!0}),s(o,"INFO","[delete-project] Development server stopped",{projectId:e})}catch(d){s(o,"WARN",`[delete-project] Failed to stop development server: ${d.message}`,{projectId:e,pid:m})}}const c=[u.join(P.UPLOAD_PROJECT_DIR,e),v(e,a),u.join(P.DIST_TARGET_DIR,e),u.join(P.LOG_BASE_DIR,e)],w=[],f=[];for(const m of c)if(i.existsSync(m))try{await i.promises.rm(m,{recursive:!0,force:!0}),w.push(m),s(o,"INFO",`[delete-project] Directory deleted successfully: ${m}`,{projectId:e})}catch(d){f.push({path:m,error:d.message}),s(o,"ERROR",`[delete-project] Directory deleted failed: ${m}`,{projectId:e,error:d.message})}else s(o,"INFO",`[delete-project] Directory does not exist, skipping deletion: ${m}`,{projectId:e});const y={success:!0,message:`Project ${e} deleted successfully`,projectId:e,deletedDirectories:w,failedDirectories:f};return f.length>0&&(y.message+=`, but ${f.length} directories deleted failed`,s(o,"WARN","[delete-project] Some directories deleted failed",{projectId:e,failedDirs:f})),s(o,"INFO",`[delete-project] Project deleted successfully: ${e}`,{projectId:e,elapsedMs:Date.now()-r}),y}catch(c){throw s(o,"ERROR",`[delete-project] Failed to delete project: ${c.message}`,{projectId:e,originalError:c.message,elapsedMs:Date.now()-r}),c.isOperational?c:new R(`Failed to delete project: ${c.message}`,{projectId:e,originalError:c.message})}}async function H(e,t,l,a,r={}){const o=Date.now();if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});if(t==null)throw new h("codeVersion cannot be empty",{field:"codeVersion"});const c=Number(t);if(!Number.isFinite(c))throw new h("codeVersion must be a number",{field:"codeVersion"});const w=v(e,r);if(!i.existsSync(w))throw new _("Project does not exist",{projectId:e});const f=u.join(P.UPLOAD_PROJECT_DIR,e),y=`${e}-v${c}.zip`,m=u.join(f,y);if(l!=="LATEST"){if(i.existsSync(m))return s(e,"INFO",`Using existing export file: ${m}`,{projectId:e,zipPath:m}),{success:!0,projectId:e,zipPath:m};throw new _(`Specified version zip file does not exist: ${m}`,{projectId:e,zipPath:m})}const d=u.join(w,"cpage_config.json");let D=!1;try{if(a)try{const p=JSON.stringify(a,null,2);await i.promises.writeFile(d,p,"utf8"),D=!0,s(e,"INFO",`Configuration file created successfully: ${d}`,{projectId:e,configFilePath:d})}catch(p){throw s(e,"ERROR",`Failed to create configuration file: ${p.message}`,{projectId:e,error:p.message}),new b("Failed to create configuration file",{projectId:e,configFilePath:d,originalError:p.message})}s(e,"DEBUG","Start executing export and packaging",{projectId:e,codeVersion:t});const n=await $(e,t,r);return s(e,"INFO",`Project exported successfully: ${n}`,{projectId:e,zipPath:n,elapsedMs:Date.now()-o}),{success:!0,projectId:e,zipPath:n}}catch(n){throw s(e,"ERROR",`Failed to export project: ${n?.message}`,{projectId:e,elapsedMs:Date.now()-o}),n.isOperational?n:new R("Failed to export project",{projectId:e,originalError:n&&n.message?C(n.message):n&&n.message})}finally{if(D&&i.existsSync(d))try{await i.promises.unlink(d),s(e,"INFO",`Temporary configuration file deleted successfully: ${d}`,{projectId:e,configFilePath:d})}catch(n){s(e,"WARN",`Failed to delete temporary configuration file: ${n.message}`,{projectId:e,error:n.message})}}}async function K(e,t,l={}){const a=Date.now();if(!e)throw new h("Project ID cannot be empty",{field:"projectId"});if(t==null)throw new h("codeVersion cannot be empty",{field:"codeVersion"});try{const r=await $(e,t,l);return s(e,"INFO",`Current version backed up successfully: ${r}`,{projectId:e,zipPath:r,elapsedMs:Date.now()-a}),{success:!0,projectId:e,zipPath:r}}catch(r){throw s(e,"ERROR",`Failed to backup current version: ${r?.message}`,{projectId:e,elapsedMs:Date.now()-a}),r.isOperational?r:new R("Failed to backup current version",{projectId:e,originalError:r&&r.message?C(r.message):r&&r.message})}}export{L as createProject,G as uploadProject,K as backupCurrentVersion,H as exportProject,$ as backupProjectOfVersion,A as cleanupProjectDirectory,J as handleFileUpload,X as pushSkillsToWorkspace,q as deleteProject};export default{createProject:L,uploadProject:G,backupCurrentVersion:K,exportProject:H,backupProjectOfVersion:$,cleanupProjectDirectory:A,handleFileUpload:J,pushSkillsToWorkspace:X,deleteProject:q};
|
|
1
|
+
import{log as i}from"../utils/log/logUtils.js";import E from"../appConfig/index.js";import p from"path";import t from"fs";import{extractZip as _}from"../utils/common/zipUtils.js";import"../utils/build/startDevUtils.js";import"../utils/build/restartDevUtils.js";import{stopDevServer as B}from"../utils/build/stopDevUtils.js";import{ValidationError as y,BusinessError as W,SystemError as P,FileError as b,ResourceError as A}from"../utils/error/errorHandler.js";import{sanitizeSensitivePaths as G}from"../utils/common/sensitiveUtils.js";import{removeNodeModules as j}from"../utils/buildDependency/dependencyManager.js";import{backupProjectToZip as I,copyDirectoryFiltered as ee}from"../utils/project/backupUtils.js";import{createPnpmNpmrc as L}from"../utils/common/npmrcUtils.js";import{copyNodeModulesFromCache as C}from"../utils/common/templateCacheUtils.js";import{resolveProjectPath as F}from"../utils/common/projectPathUtils.js";import{ensurePrimaryAgentDirs as te,syncAgents as ie}from"../utils/common/AgentWorkspaceUtils.js";async function J(e,r="react",c={}){const o=Date.now();if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});const s=["react","vue3"];if(!s.includes(r))throw new y("Template type is invalid, only supports react or vue3",{field:"templateType",templateType:r,allowedValues:s});const m={react:E.INIT_PROJECT_NAME_REACT||"react-vite-template",vue3:E.INIT_PROJECT_NAME_VUE3||"vue3-vite-template"}[r],u=F(e,c);if(t.existsSync(u))throw new W(`Project directory ${e} already exists`,{projectId:e,projectPath:u});try{t.mkdirSync(u,{recursive:!0}),i(e,"INFO",`Project directory created successfully: ${u}`,{projectId:e});const f=E.INIT_PROJECT_DIR,w=p.join(f,`${m}.zip`),l=p.join(f,m);if(i(e,"DEBUG","Start checking template directory",{templateDir:l,templateZipPath:w}),!t.existsSync(l)){if(!t.existsSync(w))throw i(e,"ERROR",`Initialization template does not exist: ${w}`,{projectId:e,templateZipPath:w}),new A("Initialization template does not exist",{});if(i(e,"INFO",`Template directory does not exist, starting to unzip template: ${w}`,{projectId:e,templateZipPath:w}),await _(w,l),i(e,"INFO","Template unzip completed",{projectId:e}),!t.existsSync(l))throw new P("Template unzip directory still does not exist",{})}i(e,"DEBUG","Start copying template content to project directory",{templateDir:l,projectPath:u});const d=await t.promises.readdir(l,{withFileTypes:!0});for(const a of d){const h=p.join(l,a.name),N=p.join(u,a.name);a.isDirectory()?(await t.promises.mkdir(N,{recursive:!0}),await ee(h,N)):a.isFile()&&(await t.promises.mkdir(p.dirname(N),{recursive:!0}),await t.promises.copyFile(h,N))}i(e,"DEBUG","Try copying node_modules from template cache",{projectPath:u});const D=await C(u,e);if(D.cached&&i(e,"INFO",`node_modules copied from cache: ${D.templateType}`,{elapsed:D.elapsed}),i(e,"DEBUG","Start creating .npmrc configuration file",{projectPath:u}),await L(u,e),i(e,"INFO",`Project ${e} initialized successfully`,{projectId:e,elapsedMs:Date.now()-o}),E.GIT_ENABLED)try{const a=await import("./gitService.js");await a.init({workspaceType:"pageApp",projectId:e,isolationContext:c}),await a.commit({workspaceType:"pageApp",projectId:e,isolationContext:c,message:`init project: ${e}`})}catch(a){i(e,"WARN","Git init/commit failed, skipping",{error:a.message})}return{success:!0,message:`Project ${e} created successfully`,projectPath:u}}catch(f){throw i(e,"ERROR",`Project ${e} initialization failed: ${f.message}`,{projectId:e,elapsedMs:Date.now()-o}),new P(`Project ${e} initialization failed: ${f.message}`,{projectId:e,projectPath:u,originalError:f.message})}}async function re(e){const r=await t.promises.readdir(e,{withFileTypes:!0}),c=E.TOP_LEVEL_NOISE_PATTERNS,o=r.filter(s=>{const n=s.name;return n.startsWith(".")?!1:!c.some(m=>m.endsWith("*")?n.startsWith(m.slice(0,-1)):n===m)});if(o.length===1&&o[0].isDirectory()){const s=p.join(e,o[0].name),n=p.join(e,`temp_${Date.now()}`);try{await t.promises.rename(s,n)}catch(u){if(u.code==="EXDEV")await t.promises.cp(s,n,{recursive:!0}),await t.promises.rm(s,{recursive:!0,force:!0});else throw u}const m=await t.promises.readdir(n);for(const u of m){const f=p.join(n,u),w=p.join(e,u);try{await t.promises.rename(f,w)}catch(l){if(l.code==="EXDEV")await t.promises.cp(f,w,{recursive:!0}),await t.promises.rm(f,{recursive:!0,force:!0});else throw l}}await t.promises.rm(n,{recursive:!0,force:!0})}}async function U(e,r={}){if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});const c=F(e,r);if(t.existsSync(c))try{i(e,"INFO",`Start cleaning project directory: ${c}`,{projectId:e}),await t.promises.rm(c,{recursive:!0,force:!0}),i(e,"INFO",`Project directory cleaned up: ${c}`,{projectId:e})}catch(o){throw i(e,"ERROR",`Failed to clean project directory: ${o.message}`,{projectId:e,projectPath:c,originalError:o.message}),new P(`Failed to clean project directory: ${o.message}`,{projectId:e,projectPath:c,originalError:o.message})}else i(e,"INFO",`Project directory does not exist, no need to clean: ${c}`,{projectId:e})}function se(e){if(!t.existsSync(e))return!0;try{return t.readdirSync(e).filter(o=>!o.startsWith(".")&&o!=="node_modules").length===0}catch(r){const c=p.basename(e);return i(c,"ERROR",`Failed to check if directory is empty: ${r.message}`,{dirPath:e}),!0}}async function Z(e,r,c,o,s,n,m={}){const u=Date.now(),f=F(e,m);try{if(se(f))i(e,"INFO","Project directory is empty, directly deploying new project",{projectId:e});else{i(e,"INFO","Project directory is not empty, starting to backup current version",{projectId:e});const l=parseInt(o)-1,d=p.join(E.UPLOAD_PROJECT_DIR,e),D=p.join(d,`${e}-v${l}.zip`);if(t.existsSync(D))i(e,"INFO",`Backup file already exists, skipping backup: ${D}`,{projectId:e});else try{E.GIT_ENABLED||await S(e,l,m),i(e,"INFO",`Current version backed up: ${D}`,{projectId:e})}catch(a){throw i(e,"ERROR",`Failed to backup current version: ${a.message}`,{projectId:e}),new P(`Failed to backup current version: ${a.message}`,{projectId:e,originalError:a.message})}if(s&&!isNaN(Number(s))){const a=Number(s);i(e,"INFO",`Stopping old version dev server, PID: ${a}`,{projectId:e});try{await B(c,e,a,{strict:!0}),i(e,"INFO","Old version dev server stopped",{projectId:e})}catch(h){i(e,"WARN",`Failed to stop old version dev server: ${h.message}`,{projectId:e,pid:a})}}t.existsSync(f)&&(i(e,"INFO",`Cleaning project directory: ${f}`,{projectId:e}),await t.promises.rm(f,{recursive:!0,force:!0}))}if(t.mkdirSync(f,{recursive:!0}),i(e,"INFO",`Project directory created successfully: ${f}`,{projectId:e}),i(e,"DEBUG","Start extracting zip file to project directory",{projectId:e,zipFilePath:r}),await _(r,f),i(e,"DEBUG","Zip file extracted successfully",{projectId:e}),i(e,"DEBUG","Check and remove top level folder",{projectId:e}),await re(f),i(e,"DEBUG","Check and remove node_modules folder",{projectId:e}),await j(f),i(e,"DEBUG","Try copying node_modules from template cache",{projectId:e}),await C(f,e),i(e,"DEBUG","Start creating .npmrc configuration file",{projectId:e}),await L(f,e),E.GIT_ENABLED)try{const l=await import("./gitService.js");await l.init({workspaceType:"pageApp",projectId:e,isolationContext:m}),await l.commit({workspaceType:"pageApp",projectId:e,isolationContext:m,message:`upload project v${o}`})}catch(l){i(e,"WARN","Git commit failed after upload, skipping",{error:l.message})}return i(e,"INFO",`Project ${e} uploaded successfully`,{projectId:e,codeVersion:o,elapsedMs:Date.now()-u}),{success:!0,message:`Project ${e} uploaded successfully`,projectId:e,codeVersion:o}}catch(w){i(e,"ERROR",`Failed to upload project: ${w.message}`,{projectId:e,elapsedMs:Date.now()-u});try{await U(e,m),i(e,"INFO","Failed to upload project, project directory cleaned up",{projectId:e})}catch(l){i(e,"ERROR",`Failed to clean project directory: ${l.message}`,{projectId:e,originalError:l.message})}throw w.isOperational?w:new P(`Failed to upload project: ${w.message}`,{projectId:e,projectPath:f,zipFilePath:r,originalError:w.message})}}async function S(e,r,c={}){if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});if(r==null)throw new y("codeVersion cannot be empty",{field:"codeVersion"});const o=Number(r);if(!Number.isFinite(o))throw new y("codeVersion must be a number",{field:"codeVersion"});const s=F(e,c);if(!t.existsSync(s))throw new A("Project does not exist",{projectId:e});const n=p.join(E.UPLOAD_PROJECT_DIR,e);t.existsSync(n)||t.mkdirSync(n,{recursive:!0});const m=`${e}-v${o}.zip`,u=p.join(n,m);return i(e,"DEBUG","Start backing up project to zip",{projectId:e,versionNum:o,outZipPath:u}),await I(e,s,u)}async function V(e,r,c){if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});if(!r)throw new y("codeVersion cannot be empty",{field:"codeVersion"});if(!c)throw new y("Please upload a zip file",{field:"zipFile"});const o=p.join(E.UPLOAD_PROJECT_DIR,e);t.existsSync(o)||t.mkdirSync(o,{recursive:!0});const s=c.path,n=p.join(o,`${e}-v${r}.zip`);try{try{t.renameSync(s,n)}catch(m){if(m.code==="EXDEV")t.copyFileSync(s,n),t.unlinkSync(s);else throw m}return i(e,"INFO","File saved successfully",{projectId:e,codeVersion:r,filePath:n}),{success:!0,filePath:n}}catch(m){if(i(e,"ERROR","Failed to move file",{projectId:e,codeVersion:r,error:m.message}),t.existsSync(s))try{t.unlinkSync(s)}catch(u){i(e,"ERROR","Failed to clean temporary file",{projectId:e,error:u.message})}throw new P("Failed to save file",{projectId:e,codeVersion:r,originalError:m.message})}}function ae(e){if(!e)return[];if(Array.isArray(e))return e.map(r=>typeof r=="string"?r.trim():"").filter(Boolean);if(typeof e=="string"){const r=e.trim();if(!r)return[];try{const c=JSON.parse(r);if(Array.isArray(c))return c.map(o=>typeof o=="string"?o.trim():"").filter(Boolean)}catch{}return[r]}return[]}async function X(e){t.existsSync(e)&&await t.promises.rm(e,{recursive:!0,force:!0})}async function q(e,r){try{await t.promises.rename(e,r)}catch(c){if(c.code==="EXDEV"){async function o(s,n){if((await t.promises.lstat(s)).isDirectory()){await t.promises.mkdir(n,{recursive:!0});const u=await t.promises.readdir(s);for(const f of u)await o(p.join(s,f),p.join(n,f))}else await t.promises.copyFile(s,n)}await o(e,r),await t.promises.rm(e,{recursive:!0,force:!0})}else throw c}}async function oe(e,r){const c=p.join(e,r);if(t.existsSync(c)&&(await t.promises.lstat(c)).isDirectory())return c;const o=await t.promises.readdir(e,{withFileTypes:!0});for(const s of o){if(!s.isDirectory())continue;const n=p.join(e,s.name,r);if(t.existsSync(n)&&(await t.promises.lstat(n)).isDirectory())return n}return null}async function ne(e,r,c){let o;try{o=await fetch(e)}catch(u){throw new b(`Failed to download skill zip from url: ${e}`,{url:e,reason:u.message})}if(!o.ok)throw new b(`Failed to download skill zip from url: ${e}`,{url:e,status:o.status,statusText:o.statusText});const s=o.headers.get("content-type")||"";s&&!s.includes("zip")&&!s.includes("octet-stream")&&i(c,"WARN","Downloaded skill url content-type is not typical zip",{url:e,contentType:s});const n=await o.arrayBuffer(),m=Buffer.from(n);await t.promises.writeFile(r,m)}async function H(e,r,c,o={}){const s=String(e),n=Date.now(),m=ae(c),u=[],f=[];if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});if(!r&&m.length===0)throw new y("file or skillUrls cannot both be empty",{field:"file|skillUrls"});if(r){if(!r.path)throw new y("Uploaded file has no valid path",{field:"file.path"});if(p.extname(r.originalname||r.filename||"").toLowerCase()!==".zip")throw new y("Only zip files are supported",{field:"file",originalName:r?.originalname})}const w=F(String(e),o);if(!t.existsSync(w))throw new y("Project does not exist",{field:"projectId"});const{skillsDir:l,agentTypes:d}=await te(w),D=p.join(E.UPLOAD_PROJECT_DIR,"temp");try{await t.promises.mkdir(l,{recursive:!0}),await t.promises.mkdir(D,{recursive:!0});const a=new Set;if(r){const k=p.join(D,`project_skill_push_${Date.now()}_${Math.round(Math.random()*1e6)}`);f.push(k),await t.promises.mkdir(k,{recursive:!0}),await _(r.path,k);const $=await oe(k,"skills");if(!$)i(s,"WARN","skills directory not found in uploaded zip",{projectId:e,extractRoot:k});else{const R=(await t.promises.readdir($,{withFileTypes:!0})).filter(v=>v.isDirectory()&&!v.name.startsWith("."));for(const v of R){const T=p.join($,v.name),x=p.join(l,v.name);t.existsSync(x)&&await X(x),await q(T,x),a.add(v.name)}}}for(let k=0;k<m.length;k+=1){const $=m[k],O=p.join(D,`project_skill_url_${Date.now()}_${k}_${Math.round(Math.random()*1e6)}.zip`);u.push(O);const R=p.join(D,`project_skill_url_extract_${Date.now()}_${k}_${Math.round(Math.random()*1e6)}`);f.push(R),await t.promises.mkdir(R,{recursive:!0}),await ne($,O,s),await _(O,R);const T=(await t.promises.readdir(R,{withFileTypes:!0})).filter(g=>g.isDirectory()&&!g.name.startsWith(".")),M=T.find(g=>g.name==="skills")?(await t.promises.readdir(p.join(R,"skills"),{withFileTypes:!0})).filter(g=>g.isDirectory()&&!g.name.startsWith(".")).map(g=>({name:g.name,sourcePath:p.join(R,"skills",g.name)})):T.map(g=>({name:g.name,sourcePath:p.join(R,g.name)}));if(M.length===0){i(s,"WARN","No skill directory found after extracting skill url zip",{projectId:e,skillUrl:$,extractRoot:R});continue}for(const g of M){const z=p.join(l,g.name);t.existsSync(z)&&await X(z),await q(g.sourcePath,z),a.add(g.name)}}const h=Array.from(a),N=h.length>0?`Pushed ${h.length} skills: ${h.join(", ")}`:"No valid skill directories found in file or skillUrls";return i(s,"INFO","Push skills to project workspace completed",{projectId:e,updatedSkills:h,skillUrlsCount:m.length,hasFile:!!r,agentTypes:d,elapsedMs:Date.now()-n}),await ie(w),{message:N,projectPath:w,updatedSkills:h}}catch(a){throw i(s,"ERROR","Push skills to project workspace failed",{projectId:e,error:a.message,elapsedMs:Date.now()-n}),a instanceof y||a instanceof b||a instanceof W||a instanceof P?a:new P(`Failed to push skills to project workspace: ${a.message}`,{projectId:e})}finally{for(const a of f)try{t.existsSync(a)&&await t.promises.rm(a,{recursive:!0,force:!0})}catch(h){i(s,"WARN","Failed to clean extracted skill temp dir",{extractRoot:a,error:h.message})}for(const a of u)try{t.existsSync(a)&&await t.promises.unlink(a)}catch(h){i(s,"WARN","Failed to clean downloaded skill zip",{zipPath:a,error:h.message})}try{r?.path&&t.existsSync(r.path)&&await t.promises.unlink(r.path)}catch(a){i(s,"WARN","Failed to clean uploaded skill zip",{tempZipPath:r?.path,error:a.message})}}}async function K(e,r,c,o={}){const s=Date.now();if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});const n=null;try{if(r&&!isNaN(Number(r))){const l=Number(r);i(n,"INFO",`[delete-project] Stopping development server, PID: ${l}`,{projectId:e,pid:l});try{await B(c,e,l,{strict:!0}),i(n,"INFO","[delete-project] Development server stopped",{projectId:e})}catch(d){i(n,"WARN",`[delete-project] Failed to stop development server: ${d.message}`,{projectId:e,pid:l})}}const m=[p.join(E.UPLOAD_PROJECT_DIR,e),F(e,o),p.join(E.DIST_TARGET_DIR,e),p.join(E.LOG_BASE_DIR,e)],u=[],f=[];for(const l of m)if(t.existsSync(l))try{await t.promises.rm(l,{recursive:!0,force:!0}),u.push(l),i(n,"INFO",`[delete-project] Directory deleted successfully: ${l}`,{projectId:e})}catch(d){f.push({path:l,error:d.message}),i(n,"ERROR",`[delete-project] Directory deleted failed: ${l}`,{projectId:e,error:d.message})}else i(n,"INFO",`[delete-project] Directory does not exist, skipping deletion: ${l}`,{projectId:e});const w={success:!0,message:`Project ${e} deleted successfully`,projectId:e,deletedDirectories:u,failedDirectories:f};return f.length>0&&(w.message+=`, but ${f.length} directories deleted failed`,i(n,"WARN","[delete-project] Some directories deleted failed",{projectId:e,failedDirs:f})),i(n,"INFO",`[delete-project] Project deleted successfully: ${e}`,{projectId:e,elapsedMs:Date.now()-s}),w}catch(m){throw i(n,"ERROR",`[delete-project] Failed to delete project: ${m.message}`,{projectId:e,originalError:m.message,elapsedMs:Date.now()-s}),m.isOperational?m:new P(`Failed to delete project: ${m.message}`,{projectId:e,originalError:m.message})}}async function Q(e,r,c,o,s={}){const n=Date.now();if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});if(r==null)throw new y("codeVersion cannot be empty",{field:"codeVersion"});const m=Number(r);if(!Number.isFinite(m))throw new y("codeVersion must be a number",{field:"codeVersion"});const u=F(e,s);if(!t.existsSync(u))throw new A("Project does not exist",{projectId:e});const f=p.join(E.UPLOAD_PROJECT_DIR,e),w=`${e}-v${m}.zip`,l=p.join(f,w);if(c!=="LATEST"){if(t.existsSync(l))return i(e,"INFO",`Using existing export file: ${l}`,{projectId:e,zipPath:l}),{success:!0,projectId:e,zipPath:l};throw new A(`Specified version zip file does not exist: ${l}`,{projectId:e,zipPath:l})}const d=p.join(u,"cpage_config.json");let D=!1;try{if(o)try{const h=JSON.stringify(o,null,2);await t.promises.writeFile(d,h,"utf8"),D=!0,i(e,"INFO",`Configuration file created successfully: ${d}`,{projectId:e,configFilePath:d})}catch(h){throw i(e,"ERROR",`Failed to create configuration file: ${h.message}`,{projectId:e,error:h.message}),new b("Failed to create configuration file",{projectId:e,configFilePath:d,originalError:h.message})}i(e,"DEBUG","Start executing export and packaging",{projectId:e,codeVersion:r});const a=await S(e,r,s);return i(e,"INFO",`Project exported successfully: ${a}`,{projectId:e,zipPath:a,elapsedMs:Date.now()-n}),{success:!0,projectId:e,zipPath:a}}catch(a){throw i(e,"ERROR",`Failed to export project: ${a?.message}`,{projectId:e,elapsedMs:Date.now()-n}),a.isOperational?a:new P("Failed to export project",{projectId:e,originalError:a&&a.message?G(a.message):a&&a.message})}finally{if(D&&t.existsSync(d))try{await t.promises.unlink(d),i(e,"INFO",`Temporary configuration file deleted successfully: ${d}`,{projectId:e,configFilePath:d})}catch(a){i(e,"WARN",`Failed to delete temporary configuration file: ${a.message}`,{projectId:e,error:a.message})}}}async function Y(e,r,c={}){const o=Date.now();if(!e)throw new y("Project ID cannot be empty",{field:"projectId"});if(r==null)throw new y("codeVersion cannot be empty",{field:"codeVersion"});try{const s=await S(e,r,c);return i(e,"INFO",`Current version backed up successfully: ${s}`,{projectId:e,zipPath:s,elapsedMs:Date.now()-o}),{success:!0,projectId:e,zipPath:s}}catch(s){throw i(e,"ERROR",`Failed to backup current version: ${s?.message}`,{projectId:e,elapsedMs:Date.now()-o}),s.isOperational?s:new P("Failed to backup current version",{projectId:e,originalError:s&&s.message?G(s.message):s&&s.message})}}export{J as createProject,Z as uploadProject,Y as backupCurrentVersion,Q as exportProject,S as backupProjectOfVersion,U as cleanupProjectDirectory,V as handleFileUpload,H as pushSkillsToWorkspace,K as deleteProject};export default{createProject:J,uploadProject:Z,backupCurrentVersion:Y,exportProject:Q,backupProjectOfVersion:S,cleanupProjectDirectory:U,handleFileUpload:V,pushSkillsToWorkspace:H,deleteProject:K};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import v from"path";import c from"fs";import{log as t}from"../log/logUtils.js";import{BusinessError as f,FileError as g,ResourceError as S}from"../error/errorHandler.js";import{isProjectStarting as w,addStartingProject as p,removeStartingProject as P,startDev_NonBlocking as N}from"./processManager.js";import h from"../error/errorCodes.js";import{stopDevServer as y}from"./stopDevUtils.js";import{removeNodeModules as F}from"../buildDependency/dependencyManager.js";import{createPnpmNpmrc as d}from"../common/npmrcUtils.js";import{copyNodeModulesFromCache as O}from"../common/templateCacheUtils.js";import{extractIsolationContext as R,resolveProjectPath as E}from"../common/projectPathUtils.js";async function x(r,e){t(e,"INFO","Start restarting development server",{projectId:e,requestId:r.requestId});const l=R(r?.query||{}),s=E(e,l),o=v.join(s,"package.json");if(!c.existsSync(o))throw t(e,"WARN","Project missing package.json file",{projectId:e,requestId:r.requestId}),new S("Project missing package.json file",{projectId:e,projectPath:s});let a;try{a=JSON.parse(c.readFileSync(o,"utf8"))}catch(i){throw new g("package.json file format error",{projectId:e,jsonFilePath:o,originalError:i.message})}const n=a.scripts.dev;if(!n)throw t(e,"WARN","Project missing dev script",{projectId:e,requestId:r.requestId}),new f("Project missing dev script",{projectId:e});if(w(e))throw new f("\u8BE5\u9879\u76EE\u6B63\u5728\u91CD\u542F\u4E2D\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",{projectId:e,code:h.PROJECT_STARTING});p(e);try{const i=r.query.pid;await y(r,e,i,{strict:!1,waitForStop:!0}),t(e,"INFO","Remove node_modules and restore from cache",{projectId:e,requestId:r.requestId}),await F(s,e),await O(s,e),t(e,"INFO","Ensure .npmrc with copy mode",{projectId:e,requestId:r.requestId}),await d(s,e),t(e,"INFO","Start starting dev server",{projectId:e,requestId:r.requestId});const{pid:m,port:u}=await N({req:r,projectId:e,projectPath:s,devScript:n});return t(e,"INFO","Dev server restart completed",{projectId:e,pid:m,port:u,requestId:r.requestId}),{success:!0,message:"Development server restart successfully",projectId:e,pid:m,port:u}}finally{P(e)}}export{x as restartDevServer};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import u from"path";import i from"fs";import{log as o}from"../log/logUtils.js";import{BusinessError as h,FileError as w,ResourceError as x}from"../error/errorHandler.js";import N from"../error/errorCodes.js";import{isProjectStarting as _,addStartingProject as A,removeStartingProject as L,startDev_NonBlocking as k}from"./processManager.js";import{removeNodeModules as O}from"../buildDependency/dependencyManager.js";import{createPnpmNpmrc as E}from"../common/npmrcUtils.js";import{copyNodeModulesFromCache as D}from"../common/templateCacheUtils.js";import{extractIsolationContext as F,resolveProjectPath as q}from"../common/projectPathUtils.js";async function W(t,e){if(_(e))throw new h("\u8BE5\u9879\u76EE\u6B63\u5728\u542F\u52A8\u4E2D\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",{projectId:e,code:N.PROJECT_STARTING});A(e);try{o(e,"INFO","Start starting development server",{projectId:e,requestId:t.requestId});const y=F(t?.query||{}),r=q(e,y),c=u.join(r,"package.json");if(!i.existsSync(c))throw o(e,"WARN","Project missing package.json file",{projectId:e,requestId:t.requestId}),new x("Project missing package.json file",{projectId:e,projectPath:r});let p;try{p=JSON.parse(i.readFileSync(c,"utf8"))}catch(s){throw new w("package.json file format error",{projectId:e,jsonFilePath:c,originalError:s.message})}const d=p.scripts.dev;if(!d)throw o(e,"WARN","Project missing dev script",{projectId:e,requestId:t.requestId}),new h("Project missing dev script, please add dev script in package.json",{projectId:e});try{if(process.platform==="linux"){const n=typeof process.report?.getReport=="function"?process.report.getReport():null,l=n&&n.header&&n.header.glibcVersionRuntime,m=!l,f=u.join(r,"node_modules",".pnpm");if(i.existsSync(f)){const g=await i.promises.readdir(f,{withFileTypes:!0}),S=g.some(a=>a.isDirectory()&&(a.name||"").includes("@rollup+rollup-linux-x64-gnu")),P=g.some(a=>a.isDirectory()&&(a.name||"").includes("@rollup+rollup-linux-x64-musl"));(m&&S||!m&&P)&&(o(e,"WARN","Detected Rollup native package does not match libc, clean dependencies and reinstall",{projectId:e,isMusl:m,glibcVersion:l||null}),await O(r,e))}}}catch(s){o(e,"WARN","Linux native package matching detection failed (ignore continue)",{error:s&&s.message})}try{process.env.ROLLUP_WASM=process.env.ROLLUP_WASM||"1",process.env.ROLLUP_DISABLE_NATIVE=process.env.ROLLUP_DISABLE_NATIVE||"1"}catch{}try{const s=u.join(r,"node_modules");let n=!1;try{i.lstatSync(s).isSymbolicLink()&&(i.existsSync(s)||(o(e,"WARN","node_modules symlink is broken, will restore from cache",{projectId:e,requestId:t.requestId}),n=!0))}catch{o(e,"INFO","node_modules not found, will try to copy from cache",{projectId:e,requestId:t.requestId}),n=!0}n&&(o(e,"INFO","Attempting to restore node_modules from template cache",{projectId:e,requestId:t.requestId}),await D(r,e))}catch(s){o(e,"WARN","Failed to check/restore node_modules, will proceed anyway",{projectId:e,requestId:t.requestId,error:s&&s.message})}await E(r,e),o(e,"INFO","Start executing dev script in non-blocking mode",{projectId:e,requestId:t.requestId});const{pid:R,port:v}=await k({req:t,projectId:e,projectPath:r,devScript:d});return{success:!0,message:"Development server started",projectId:e,pid:R,port:v}}finally{L(e)}}export{W as startDevServer};
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import{exec as
|
|
2
|
-
Command: pnpm install --prefer-offline
|
|
3
|
-
`;e(
|
|
4
|
-
`;e(
|
|
5
|
-
Exit code: ${
|
|
6
|
-
${
|
|
7
|
-
`;return e(
|
|
8
|
-
${
|
|
9
|
-
`;e(
|
|
10
|
-
`;if(e(
|
|
11
|
-
${
|
|
1
|
+
import{exec as R,spawn as O}from"child_process";import g from"path";import r from"fs";import{log as i}from"../log/logUtils.js";function v(o){try{return r.statSync(o).mtimeMs}catch{return 0}}function W(o){const c=g.join(o,"package.json"),s=g.join(o,"package-lock.json"),l=g.join(o,"yarn.lock"),n=g.join(o,"node_modules");if(!r.existsSync(n))return!0;const e=v(c),k=Math.max(v(s),v(l)),t=v(n);return Math.max(e,k)>t}async function b(o,c=null){const s=g.join(o,"node_modules"),l=c||g.basename(o);let n=!1,a=!1,e=null;try{const t=await r.promises.lstat(s);n=!0,a=t.isSymbolicLink(),a&&(e=await r.promises.readlink(s))}catch{}if(n)if(a){i(l,"INFO","Found node_modules symlink, removing symlink only (preserving target as cache)",{projectPath:o,symlink:s,target:e});try{await r.promises.unlink(s),i(l,"INFO","Symlink removed, target preserved for cache reuse",{nodeModulesPath:s,target:e})}catch(t){i(l,"WARN",`Failed to remove node_modules symlink: ${t.message}`,{error:t.message})}}else{i(l,"INFO","Found node_modules folder, deleting",{projectPath:o,nodeModulesPath:s});try{await r.promises.rm(s,{recursive:!0,force:!0}),i(l,"INFO","node_modules folder deleted successfully",{projectPath:o,nodeModulesPath:s})}catch(t){i(l,"WARN",`Failed to delete node_modules folder: ${t.message}`,{projectPath:o,nodeModulesPath:s,error:t.message})}}const k=["package-lock.json","yarn.lock","pnpm-lock.yaml"];for(const t of k){const y=g.join(o,t);if(r.existsSync(y)){i(l,"INFO",`Found ${t} file, deleting`,{projectPath:o,lockFilePath:y});try{await r.promises.unlink(y),i(l,"INFO",`${t} file deleted successfully`,{projectPath:o,lockFilePath:y})}catch($){i(l,"WARN",`Failed to delete ${t} file: ${$.message}`,{projectPath:o,lockFilePath:y,error:$.message})}}}}async function T(o,c){const s=g.join(o,"node_modules"),l=c||g.basename(o);if(r.existsSync(s)){try{if((await r.promises.lstat(s)).isSymbolicLink()&&!r.existsSync(s)){i(l,"WARN","Broken node_modules symlink detected, removing before install",{projectPath:o,nodeModulesPath:s}),await r.promises.unlink(s);return}}catch(n){i(l,"WARN",`Failed to check node_modules symlink: ${n.message}`)}try{const n=g.join(s,".modules.yaml");if(r.existsSync(n)){const e=(await r.promises.readFile(n,"utf8")).match(/storeDir:\s*(.+)/);if(e){const k=e[1].trim(),t=process.env.npm_config_store_dir||process.env.PNPM_STORE_DIR||"";if(t&&k!==t){i(l,"WARN","Store path mismatch detected (node_modules from different store), removing .modules.yaml to let pnpm rewrite it (preserving node_modules)",{projectPath:o,recordedStoreDir:k,currentStoreDir:t});try{await r.promises.unlink(n),i(l,"INFO","Removed stale .modules.yaml, node_modules preserved",{modulesYamlPath:n})}catch{}}}}}catch(n){i(l,"WARN",`Pre-flight store check failed: ${n.message}`)}}}async function A(o,c,s,l={}){await T(s,c);const{outStream:n,tempOutStream:a,safeWrite:e}=l,k=g.join(s,"node_modules"),t=r.existsSync(k),y=process.env.npm_config_store_dir||process.env.PNPM_STORE_DIR||"",$=y?` --store-dir=${y}`:"";return n&&a&&e?new Promise((x,w)=>{const N=`cd ${s} && pnpm install --prefer-offline${$}`,f=Date.now(),M=`Start installing dependencies (${t?"incremental (node_modules from cache)":"full install"})
|
|
2
|
+
Command: pnpm install --prefer-offline${$}
|
|
3
|
+
`;e(n,M,"Main log"),e(a,M,"Temp log");let E=0;const h=setInterval(()=>{E++;const d=`Installing dependencies... (Elapsed time: ${Math.floor((Date.now()-f)/1e3)} seconds)
|
|
4
|
+
`;e(n,d,"Main log"),e(a,d,"Temp log")},5e3),S=O("sh",["-c",N],{cwd:s,env:process.env,stdio:["ignore","pipe","pipe"]});let D="",p="";S.stdout&&S.stdout.on("data",m=>{const d=m.toString();D+=d,e(n,d,"Main log"),e(a,d,"Temp log")}),S.stderr&&S.stderr.on("data",m=>{const d=m.toString();p+=d,e(n,d,"Main log"),e(a,d,"Temp log")}),S.on("exit",(m,d)=>{clearInterval(h);const _=((Date.now()-f)/1e3).toFixed(2);if(m!==0){const u=`Dependency installation failed (Elapsed time: ${_} seconds)
|
|
5
|
+
Exit code: ${m}, Signal: ${d}
|
|
6
|
+
${p||"No error information"}
|
|
7
|
+
`;return e(n,u,"Main log"),e(a,u,"Temp log"),w(new Error(`Dependency installation failed: Exit code ${m}, Signal ${d}
|
|
8
|
+
${p}`))}if(p&&p.includes("Error")&&!p.includes("warning")){const u=`Warning occurred during dependency installation: ${p}
|
|
9
|
+
`;e(n,u,"Main log"),e(a,u,"Temp log")}const I=`\u2713 Dependency installation successful (Elapsed time: ${_} seconds)
|
|
10
|
+
`;if(e(n,I,"Main log"),e(a,I,"Temp log"),D||p){const u=`Installation details:
|
|
11
|
+
${D||"(No standard output)"}${p?`
|
|
12
12
|
Warning information:
|
|
13
|
-
`+
|
|
14
|
-
`;e(
|
|
15
|
-
`;e(
|
|
16
|
-
`;e(
|
|
17
|
-
${f
|
|
13
|
+
`+p:""}
|
|
14
|
+
`;e(n,u,"Main log"),e(a,u,"Temp log")}else{const u=`(Silent mode: No detailed output, dependency successfully linked from store to node_modules)
|
|
15
|
+
`;e(n,u,"Main log"),e(a,u,"Temp log")}x(D)}),S.on("error",m=>{clearInterval(h);const _=`Dependency installation process error (Elapsed time: ${((Date.now()-f)/1e3).toFixed(2)} seconds): ${m.message}
|
|
16
|
+
`;e(n,_,"Main log"),e(a,_,"Temp log"),w(new Error(`Dependency installation failed: ${m.message}`))})}):new Promise((x,w)=>{const N=`cd ${s} && pnpm install --prefer-offline${$} --reporter=silent --loglevel=error`;i(c,"INFO","Start executing dependency installation command",{command:N,projectPath:s,installMode:t?"incremental":"full"}),R(N,{maxBuffer:10*1024*1024,env:process.env},(f,F,M)=>{if(f)return i(c,"ERROR","Dependency installation failed",{error:f.message,code:f.code,stderr:M||f.message,stdout:F||""}),w(new Error(`Dependency installation failed: ${f.message}
|
|
17
|
+
${M||f.message}`));M&&M.includes("Error")&&!M.includes("warning")&&i(c,"WARN","Warning or error occurred during dependency installation",{stderr:M}),i(c,"INFO","Dependency installation completed",{stdout:F.substring(0,500)}),x(F)})})}export{v as getFileMtime,W as shouldInstallDeps,A as installDependencies,b as removeNodeModules,T as preflightCleanNodeModules};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import a from"fs";import f from"path";import{log as m,getCSTDateTimeString as D}from"../log/logUtils.js";import{detectFilesystemType as x}from"./templateCacheUtils.js";async function $(t,y=null){const i=y||f.basename(t),e=f.join(t,".npmrc"),o=x(t),s="copy",r=process.env.npm_config_store_dir||process.env.PNPM_STORE_DIR||"",S=r?`store-dir=${r}
|
|
2
|
+
`:"",h=`# pnpm \u4F18\u5316\u914D\u7F6E
|
|
3
|
+
# \u81EA\u52A8\u751F\u6210\u4E8E ${D()}
|
|
4
|
+
# \u6587\u4EF6\u7CFB\u7EDF\u7C7B\u578B: ${o}
|
|
5
|
+
package-import-method=${s}
|
|
5
6
|
auto-install-peers=true
|
|
6
7
|
registry=https://registry.npmmirror.com
|
|
7
|
-
`;try{
|
|
8
|
+
${S}`;try{const n=a.existsSync(e);if(n){const l=a.readFileSync(e,"utf8"),d=l.match(/^\s*package-import-method\s*=\s*(\S+)/m),u=l.match(/^\s*store-dir\s*=\s*(\S+)/m),g=d?d[1]:null,c=u?u[1]:null;if(g===s&&(!r||c===r))return m(i,"INFO",".npmrc already optimal, skip creation",{projectPath:t,npmrcPath:e,importMethod:s,storeDir:c||"(not set)",fsType:o}),{success:!0,created:!1,message:".npmrc already optimal",npmrcPath:e,importMethod:s,storeDir:c,fsType:o};m(i,"INFO",".npmrc needs update",{projectPath:t,npmrcPath:e,existingMethod:g,newMethod:s,existingStoreDir:c||"(not set)",newStoreDir:r||"(not set)",fsType:o})}await a.promises.writeFile(e,h,"utf8");const p=n?"updated":"created";return m(i,"INFO",`.npmrc ${p} successfully`,{projectPath:t,npmrcPath:e,importMethod:s,storeDir:r||"(not set)",fsType:o}),{success:!0,created:!0,message:`.npmrc ${p} successfully`,npmrcPath:e,importMethod:s,storeDir:r,fsType:o}}catch(n){return m(i,"WARN",`.npmrc file creation failed: ${n.message}`,{projectPath:t,npmrcPath:e,error:n.message}),{success:!1,created:!1,message:`.npmrc file creation failed: ${n.message}`,error:n.message}}}export{$ as createPnpmNpmrc};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import n from"fs";import m from"path";import{execSync as k}from"child_process";import{log as s}from"../log/logUtils.js";function $(){return process.env.DEPLOYMENT_MODE==="k8s"}function _(){if(process.env.TEMPLATE_CACHE_DIR)return process.env.TEMPLATE_CACHE_DIR;if(n.existsSync("/local-cache"))return"/local-cache/templates";const o=process.env.PROJECT_WORKSPACE_DIR||"/app/project_workspace";return m.join(m.dirname(o),".template-cache")}function F(o){try{const e=n.readFileSync("/proc/mounts","utf8").split(`
|
|
2
|
+
`);let t="",c="local";for(const a of e){const r=a.split(/\s+/);if(r.length<3)continue;const d=r[1],u=r[2];o.startsWith(d)&&d.length>t.length&&(t=d,c=u)}return c.startsWith("fuse")?"fuse":"local"}catch{return"local"}}function D(o){try{const l=m.join(o,"package.json");if(!n.existsSync(l))return null;const e=JSON.parse(n.readFileSync(l,"utf8")),t=(e.name||"").toLowerCase(),c=e.dependencies||{};if(t.includes("vue3")||t.includes("vue-vite"))return"vue3";if(t.includes("react-vite")||t.includes("react"))return"react";if(c.vue)return"vue3";if(c.react)return"react"}catch{}return null}async function T(o){const l=_(),e=$();s("CACHE","INFO",`Template cache base dir: ${l}`,{deploymentMode:e?"k8s":"docker-compose"}),s("CACHE","INFO",`pnpm store dir: ${process.env.npm_config_store_dir||process.env.PNPM_STORE_DIR||"(default: ~/.local/share/pnpm/store)"}`);const t=[{name:"vue3",zip:"vue3-vite-template.zip"},{name:"react",zip:"react-vite-template.zip"}];n.mkdirSync(l,{recursive:!0});for(const c of t){const a=m.join(l,c.name),r=m.join(a,".cache-ready");if(n.existsSync(r)){s("CACHE","INFO",`Template cache already ready: ${c.name}`,{cachePath:a});continue}const d=m.join(o,c.zip);if(!n.existsSync(d)){s("CACHE","WARN",`Template zip not found, skip: ${d}`);continue}s("CACHE","INFO",`Warming up template cache: ${c.name}`,{zipPath:d,cachePath:a,k8sMode:e});const u=Date.now();try{if(n.mkdirSync(a,{recursive:!0}),k(`unzip -o "${d}" -d "${a}"`,{stdio:"pipe"}),O(a),!e){const p=process.env.npm_config_store_dir||process.env.PNPM_STORE_DIR||"",v=p?`store-dir=${p}
|
|
3
|
+
`:"",N=p?` --store-dir=${p}`:"",i=`# Template cache npmrc - auto-generated
|
|
4
|
+
package-import-method=copy
|
|
5
|
+
auto-install-peers=true
|
|
6
|
+
registry=https://registry.npmmirror.com
|
|
7
|
+
${v}`;n.writeFileSync(m.join(a,".npmrc"),i,"utf8"),s("CACHE","INFO",`Running pnpm install for ${c.name} cache...`),k(`cd "${a}" && pnpm install --prefer-offline${N}`,{stdio:"pipe",timeout:18e4,env:process.env})}n.writeFileSync(r,new Date().toISOString(),"utf8");const f=((Date.now()-u)/1e3).toFixed(1);s("CACHE","INFO",`Template cache ready: ${c.name}`,{elapsed:`${f}s`,cachePath:a,k8sMode:e,hasNodeModules:!e})}catch(f){s("CACHE","ERROR",`Template cache failed: ${c.name}`,{error:f.message});try{n.rmSync(a,{recursive:!0,force:!0})}catch{}}}}function g(o){const l=process.env.NODE_MODULES_LOCAL_DIR||"/local-cache/node-modules";return m.join(l,o,"node_modules")}async function C(o,l){const e=l||m.basename(o),t=m.join(o,"node_modules");let c=!1;try{if(n.lstatSync(t).isSymbolicLink()){if(n.existsSync(t))return s(e,"INFO","node_modules is a valid symlink, skip cache copy"),{cached:!1,reason:"already-exists"};c=!0,s(e,"WARN","node_modules is a broken symlink, will recreate",{symlink:t})}else return s(e,"INFO","node_modules already exists, skip cache copy"),{cached:!1,reason:"already-exists"}}catch{}const a=g(l);if(n.existsSync(a)){const i=Date.now();if(c)try{n.unlinkSync(t),s(e,"INFO","Removed broken symlink before recreating")}catch{}n.symlinkSync(a,t,"dir");const h=((Date.now()-i)/1e3).toFixed(3);return s(e,"INFO","Reused existing node_modules cache via symlink",{cache:a,symlink:t,elapsed:`${h}s`}),{cached:!0,elapsed:`${h}s`,strategy:"reuse-cache"}}const r=D(o);if(!r)return s(e,"WARN","Cannot detect template type, skip cache copy",{projectPath:o}),{cached:!1,reason:"unknown-template"};const d=_(),u=m.join(d,r,"node_modules"),f=m.join(d,r,".cache-ready");if(!n.existsSync(f)||!n.existsSync(u))return s(e,"INFO",`Template cache not ready: ${r}`,{cacheNodeModules:u}),{cached:!1,reason:"cache-not-ready"};const p=Date.now();if(F(o)==="fuse"){const i=g(l),h=m.dirname(i);s(e,"INFO",`JuiceFS detected, using symlink strategy: ${r}`,{source:u,localTarget:i,symlink:t});try{if(n.mkdirSync(h,{recursive:!0}),k(`cp -a "${u}" "${i}"`,{timeout:6e4,stdio:"pipe"}),c)try{n.unlinkSync(t),s(e,"INFO","Removed broken symlink before creating new one")}catch(S){s(e,"WARN",`Failed to remove broken symlink: ${S.message}`)}n.symlinkSync(i,t,"dir");const y=((Date.now()-p)/1e3).toFixed(1);return s(e,"INFO","node_modules ready via symlink",{templateType:r,elapsed:`${y}s`,symlink:`${t} \u2192 ${i}`}),{cached:!0,templateType:r,elapsed:y,strategy:"symlink"}}catch(y){s(e,"WARN",`Failed to setup node_modules symlink: ${y.message}`,{templateType:r});try{n.rmSync(i,{recursive:!0,force:!0})}catch{}try{n.unlinkSync(t)}catch{}return{cached:!1,reason:"symlink-failed",error:y.message}}}else{s(e,"INFO",`Local filesystem, using direct copy: ${r}`,{source:u,target:t});try{k(`cp -a "${u}" "${t}"`,{timeout:6e4,stdio:"pipe"});const i=((Date.now()-p)/1e3).toFixed(1);return s(e,"INFO","node_modules copied from cache successfully",{templateType:r,elapsed:`${i}s`}),{cached:!0,templateType:r,elapsed:i,strategy:"copy"}}catch(i){s(e,"WARN",`Failed to copy node_modules from cache: ${i.message}`,{templateType:r});try{n.rmSync(t,{recursive:!0,force:!0})}catch{}return{cached:!1,reason:"copy-failed",error:i.message}}}}function O(o){try{const e=n.readdirSync(o,{withFileTypes:!0}).filter(t=>!t.name.startsWith(".")&&t.name!=="node_modules");if(e.length===1&&e[0].isDirectory()){const t=m.join(o,e[0].name),c=m.join(o,`..tmp_${Date.now()}`);n.renameSync(t,c);const a=n.readdirSync(c);for(const r of a)n.renameSync(m.join(c,r),m.join(o,r));n.rmdirSync(c)}}catch{}}export{_ as getCacheBaseDir,g as getLocalNodeModulesPath,F as detectFilesystemType,D as detectTemplateType,$ as isK8sMode,T as warmupTemplateCache,C as copyNodeModulesFromCache};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import m from"fs";import h from"path";import N from"archiver";import I from"../../appConfig/index.js";import{log as r}from"../log/logUtils.js";import{ValidationError as g,SystemError as D}from"../error/errorHandler.js";const A=100*1024*1024,P=I.DOWNLOAD_MAX_FILE_SIZE_BYTES||A;async function O(f,u,a,c){const n=[],w=await m.promises.readdir(f,{withFileTypes:!0});w.sort((i,s)=>i.isDirectory()&&!s.isDirectory()?-1:!i.isDirectory()&&s.isDirectory()?1:i.name.toLowerCase().localeCompare(s.name.toLowerCase()));for(const i of w){const s=h.join(f,i.name);if(!(i.name.startsWith(".")||(I.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(i.name))&&!(i.isDirectory()&&I.TRAVERSE_EXCLUDE_DIRS.includes(i.name)))if(i.isDirectory()){const e=await O(s,u,a,c);if(e.length===0){const l=u||f,p=h.relative(l,s).replace(/\\/g,"/");n.push({name:p,isDir:!0})}else n.push(...e)}else try{const e=u||f,l=h.relative(e,s).replace(/\\/g,"/"),p=i.isSymbolicLink();let o=null;if(c){const y=l.split("/").map(R=>encodeURIComponent(R)).join("/");o=`${c}/${y}`}const t={name:l,isDir:!1,fileProxyUrl:o,isLink:p};n.push(t)}catch(e){r(a,"WARN",`\u5904\u7406\u6587\u4EF6\u5931\u8D25: ${s}`,{error:e.message})}}return n}async function k(f,u,a,c,n=""){const w=await m.promises.readdir(h.join(f,n),{withFileTypes:!0});let i=0;for(const s of w){const d=n?h.join(n,s.name):s.name,e=d.split(h.sep).filter(Boolean);if(e.some(t=>t.startsWith(".")))continue;const l=e[e.length-1];if(u.includes(l)||e.some(t=>a.includes(t)))continue;const p=h.join(f,d);let o;try{o=await m.promises.lstat(p)}catch(t){r(c,"WARN","Error occurred when getting file stats, skipping",{filePath:d.replace(/\\/g,"/"),error:t.message});continue}o.isSymbolicLink()||o.nlink>1||(o.isDirectory()?i+=await k(f,u,a,c,d):o.isFile()&&(i+=o.size))}return i}async function $(f,u,a){const c=Date.now(),n=`computer:${f}:${u}`,w=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!u)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});const i=String(f),s=String(u),d=h.join(w,i,s);if(!m.existsSync(d))return r(n,"INFO","Directory does not exist, returning empty list",{targetDir:d,userId:i,cId:s}),{files:[]};r(n,"DEBUG","Start getting user file list",{targetDir:d,userId:i,cId:s});try{const e=await O(d,d,n,a);return r(n,"INFO","User file list obtained successfully",{fileCount:e.length,targetDir:d,userId:i,cId:s,elapsedMs:Date.now()-c}),{files:e}}catch(e){throw r(n,"ERROR","Failed to get user file list",{targetDir:d,userId:i,cId:s,error:e.message,elapsedMs:Date.now()-c}),new D(`Failed to get file list: ${e.message}`,{targetDir:d,originalError:e.message})}}async function _(f,u,a){const c=Date.now(),n=`computer:${f}:${u}`,w=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!Array.isArray(a))throw new g("files must be an array",{field:"files"});const i=String(f),s=String(u),d=h.join(w,i,s);m.existsSync(d)||m.mkdirSync(d,{recursive:!0});for(let e=0;e<a.length;e++){const l=a[e];if(!l||typeof l.operation!="string")throw new g(`files[${e}].operation cannot be empty`,{field:`files[${e}].operation`});if(!l.name||typeof l.name!="string")throw new g(`files[${e}].name cannot be empty`,{field:`files[${e}].name`});const p=l.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(p))throw new g(`files[${e}].operation must be one of create, delete, rename or modify`,{field:`files[${e}].operation`});if(p==="rename"&&!l.renameFrom)throw new g(`files[${e}].renameFrom cannot be empty (rename operation requires)`,{field:`files[${e}].renameFrom`});if(p==="modify"&&l.isDir!==!0&&typeof l.contents!="string")throw new g(`files[${e}].contents must be a string (modify operation requires)`,{field:`files[${e}].contents`})}r(n,"DEBUG","Start updating user files",{userId:i,cId:s,filesCount:a.length});try{for(const e of a){const l=e.operation.toLowerCase(),p=e.name,o=h.normalize(p).replace(/^[\/\\]+/,""),t=h.join(d,o),y=h.resolve(t),R=h.resolve(d);if(!y.startsWith(R+h.sep)&&y!==R){r(n,"WARN","File path is not secure, skipping",{filePath:o,resolvedPath:y});continue}switch(l){case"create":{if(e.isDir===!0){if(m.existsSync(t)){if((await m.promises.stat(t)).isFile())throw new g("Cannot create directory, file with the same name already exists",{filePath:o});r(n,"INFO","Directory already exists, skipping creation",{filePath:o});break}await m.promises.mkdir(t,{recursive:!0}),r(n,"INFO","Directory created successfully",{filePath:o});break}if(m.existsSync(t)){if((await m.promises.stat(t)).isDirectory())throw new g("Cannot create file, directory with the same name already exists",{filePath:o});r(n,"INFO","File already exists, skipping creation",{filePath:o});break}await m.promises.mkdir(h.dirname(t),{recursive:!0});const F=e.contents||"";await m.promises.writeFile(t,F,"utf8"),r(n,"INFO","File created successfully",{filePath:o});break}case"delete":{m.existsSync(t)?(await m.promises.stat(t)).isDirectory()?(await m.promises.rm(t,{recursive:!0,force:!0}),r(n,"INFO","Directory deleted successfully",{filePath:o})):(await m.promises.unlink(t),r(n,"INFO","File deleted successfully",{filePath:o})):r(n,"WARN","The file or directory to be deleted does not exist",{filePath:o});break}case"rename":{const F=e.renameFrom;if(!F||typeof F!="string"){r(n,"WARN","Rename operation missing renameFrom",{filePath:o});break}const E=h.normalize(F).replace(/^[\/\\]+/,""),S=h.join(d,E),b=h.resolve(S);if(!b.startsWith(R+h.sep)&&b!==R){r(n,"WARN","Source path is not secure, skipping rename",{sourcePath:E,targetPath:o});break}if(m.existsSync(S)){const C=(await m.promises.stat(S)).isDirectory();await m.promises.mkdir(h.dirname(t),{recursive:!0}),await m.promises.rename(S,t),r(n,"INFO",C?"Directory renamed successfully":"File renamed successfully",{sourcePath:E,targetPath:o})}else r(n,"WARN","The file or directory to be renamed does not exist",{sourcePath:E});break}case"modify":{if(!m.existsSync(t)){r(n,"WARN","The file to be modified does not exist",{filePath:o});break}if((await m.promises.stat(t)).isDirectory()){r(n,"INFO","The target is a directory, skipping modification",{filePath:o});break}const E=typeof e.contents=="string"?e.contents:"";if(await m.promises.readFile(t,"utf8")===E){r(n,"INFO","File content has no changes, skipping write",{filePath:o});break}await m.promises.writeFile(t,E,"utf8"),r(n,"INFO","File modified successfully",{filePath:o});break}default:{r(n,"WARN","Unsupported operation type",{operation:l,filePath:o});break}}}return r(n,"INFO","User files updated successfully",{userId:i,cId:s,filesCount:a.length,elapsedMs:Date.now()-c}),{success:!0,message:"User files updated successfully",userId:i,cId:s,filesCount:a.length}}catch(e){throw r(n,"ERROR","User files updated failed",{userId:i,cId:s,error:e.message,elapsedMs:Date.now()-c}),new D(`User files updated failed: ${e.message}`,{userId:i,cId:s,originalError:e.message})}}async function z(f,u,a,c){const n=Date.now(),w=`computer:${f}:${u}`,i=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!a)throw new g("file cannot be empty",{field:"file"});if(!c||typeof c!="string")throw new g("filePath cannot be empty",{field:"filePath"});const s=String(f),d=String(u),e=h.join(i,s,d);m.existsSync(e)||m.mkdirSync(e,{recursive:!0});const l=h.normalize(c).replace(/^[\/\\]+/,""),p=h.join(e,l),o=h.resolve(p),t=h.resolve(e);if(!o.startsWith(t+h.sep)&&o!==t)throw new g("File path is not secure, cannot exceed user directory",{field:"filePath",providedPath:c,resolvedPath:o});try{if(await m.promises.mkdir(h.dirname(p),{recursive:!0}),a.buffer)await m.promises.writeFile(p,a.buffer);else if(typeof a.contents=="string")await m.promises.writeFile(p,a.contents,"utf8");else throw new g("File content format is incorrect",{field:"file",hasBuffer:!!a.buffer,hasContents:typeof a.contents});return r(w,"INFO","File uploaded successfully",{userId:s,cId:d,filePath:l,targetPath:o,fileSize:a.buffer?a.buffer.length:a.contents?a.contents.length:0,elapsedMs:Date.now()-n}),{success:!0,message:"File uploaded successfully",fileSize:a.buffer?a.buffer.length:a.contents?a.contents.length:0}}catch(y){throw r(w,"ERROR","File upload failed",{userId:s,cId:d,filePath:l,error:y.message,elapsedMs:Date.now()-n}),new D(`File upload failed: ${y.message}`,{userId:s,cId:d,filePath:l,originalError:y.message})}}async function T(f,u,a,c){const n=Date.now(),w=`computer:${f}:${u}`;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!Array.isArray(a))throw new g("files must be an array",{field:"files"});if(!Array.isArray(c))throw new g("filePaths must be an array",{field:"filePaths"});if(a.length!==c.length)throw new g(`File count (${a.length}) does not match path count (${c.length})`,{field:"filePaths"});r(w,"DEBUG","Start batch uploading files",{userId:f,cId:u,filesCount:a.length});const i=[];try{for(let e=0;e<a.length;e++){const l=a[e],p=c[e];if(!l){r(w,"WARN","Empty file object encountered in batch upload, skipping",{index:e,filePath:p}),i.push({success:!1,filePath:p,error:"Empty file object"});continue}if(!p||typeof p!="string"){r(w,"WARN","Invalid file path in batch upload, skipping",{index:e,originalname:l.originalname}),i.push({success:!1,filePath:p||"",originalname:l.originalname,error:"Invalid file path"});continue}try{const o=await z(f,u,l,p);i.push({success:!0,filePath:p,originalname:l.originalname,...o})}catch(o){r(w,"ERROR","Single file upload failed in batch upload",{filePath:p,originalname:l.originalname,error:o.message}),i.push({success:!1,filePath:p,originalname:l.originalname,error:o.message})}}const s=i.filter(e=>e.success).length,d=i.filter(e=>!e.success).length;return r(w,"INFO","Batch upload files completed",{userId:f,cId:u,totalCount:a.length,successCount:s,failCount:d,elapsedMs:Date.now()-n}),{success:!0,message:"Batch upload completed",totalCount:a.length,successCount:s,failCount:d,results:i}}catch(s){throw r(w,"ERROR","Batch upload files failed",{userId:f,cId:u,error:s.message,elapsedMs:Date.now()-n}),new D(`Batch upload files failed: ${s.message}`,{userId:f,cId:u,originalError:s.message})}}async function v(f,u){const a=Date.now(),c=`computer:${f}:${u}`,n=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!n)throw new D("COMPUTER_WORKSPACE_DIR is not configured, cannot create zip");const w=String(f),i=String(u),s=h.join(n,w,i);if(!m.existsSync(s)){const t=`${w}_${i}.zip`;r(c,"WARN","Workspace directory does not exist, returning empty zip",{targetDir:s,userId:w,cId:i,zipFileName:t});const y=N("zip",{zlib:{level:9}});return y.append(null,{name:`${w}_${i}/`,type:"directory"}),y.on("warning",R=>{if(R.code==="ENOENT")r(c,"WARN","Encountered file problem when creating empty zip",{message:R.message,code:R.code});else throw r(c,"ERROR","Encountered warning when creating empty zip",{message:R.message,code:R.code}),R}),y.on("error",R=>{r(c,"ERROR","Failed to create empty zip",{message:R.message})}),{archive:y,zipFileName:t}}const d=`${w}_${i}.zip`;r(c,"DEBUG","Start creating workspace directory zip",{targetDir:s,zipFileName:d});const e=N("zip",{zlib:{level:9}}),l=I.CONTENT_TRAVERSE_EXCLUDE_FILES||[],p=I.TRAVERSE_EXCLUDE_DIRS||[],o=await k(s,l,p,c);if(o>P){const t=P/1024/1024,y=(o/1024/1024).toFixed(2);throw r(c,"WARN","Download rejected due to oversized workspace",{targetDir:s,downloadableSize:o,maxSizeBytes:P}),new g(`Download failed: total file size ${y}MB exceeds limit ${t}MB`,{field:"downloadSize",downloadableSize:o,maxSizeBytes:P})}return e.directory(s,`${w}_${i}`,t=>{const y=t.name||"",R=y.split(/[\/\\]/).filter(Boolean);if(R.some(E=>E.startsWith(".")))return!1;const F=R[R.length-1];if(l.includes(F)||R.some(E=>p.includes(E)))return!1;try{const E=h.join(s,y),S=m.lstatSync(E);if(S.isSymbolicLink()||S.nlink>1)return!1}catch(E){return r(c,"WARN","Error occurred when detecting link file, skipping",{filePath:y,error:E.message}),!1}return t}),e.on("warning",t=>{if(t.code==="ENOENT")r(c,"WARN","Encountered file problem when creating zip",{message:t.message,code:t.code});else throw r(c,"ERROR","Encountered warning when creating zip",{message:t.message,code:t.code}),t}),e.on("error",t=>{r(c,"ERROR","Failed to create zip",{message:t.message,elapsedMs:Date.now()-a})}),e.on("end",()=>{r(c,"INFO","Workspace directory zip created successfully",{targetDir:s,zipFileName:d,elapsedMs:Date.now()-a})}),{archive:e,zipFileName:d}}export{$ as getFileList,_ as updateFiles,z as uploadFile,T as uploadFiles,v as downloadAllFiles};
|
|
1
|
+
import m from"fs";import h from"path";import N from"archiver";import I from"../../appConfig/index.js";import{log as r}from"../log/logUtils.js";import{ValidationError as g,SystemError as D}from"../error/errorHandler.js";const A=100*1024*1024,P=I.DOWNLOAD_MAX_FILE_SIZE_BYTES||A;async function O(f,u,a,c){const n=[],w=await m.promises.readdir(f,{withFileTypes:!0});w.sort((t,s)=>t.isDirectory()&&!s.isDirectory()?-1:!t.isDirectory()&&s.isDirectory()?1:t.name.toLowerCase().localeCompare(s.name.toLowerCase()));for(const t of w){const s=h.join(f,t.name);if(!(t.name.startsWith(".")&&t.name!==".gitignore"||(I.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(t.name))&&!(t.isDirectory()&&I.TRAVERSE_EXCLUDE_DIRS.includes(t.name)))if(t.isDirectory()){const e=await O(s,u,a,c);if(e.length===0){const l=u||f,p=h.relative(l,s).replace(/\\/g,"/");n.push({name:p,isDir:!0})}else n.push(...e)}else try{const e=u||f,l=h.relative(e,s).replace(/\\/g,"/"),p=t.isSymbolicLink();let o=null;if(c){const y=l.split("/").map(R=>encodeURIComponent(R)).join("/");o=`${c}/${y}`}const i={name:l,isDir:!1,fileProxyUrl:o,isLink:p};n.push(i)}catch(e){r(a,"WARN",`\u5904\u7406\u6587\u4EF6\u5931\u8D25: ${s}`,{error:e.message})}}return n}async function k(f,u,a,c,n=""){const w=await m.promises.readdir(h.join(f,n),{withFileTypes:!0});let t=0;for(const s of w){const d=n?h.join(n,s.name):s.name,e=d.split(h.sep).filter(Boolean);if(e.some(i=>i.startsWith(".")))continue;const l=e[e.length-1];if(u.includes(l)||e.some(i=>a.includes(i)))continue;const p=h.join(f,d);let o;try{o=await m.promises.lstat(p)}catch(i){r(c,"WARN","Error occurred when getting file stats, skipping",{filePath:d.replace(/\\/g,"/"),error:i.message});continue}o.isSymbolicLink()||o.nlink>1||(o.isDirectory()?t+=await k(f,u,a,c,d):o.isFile()&&(t+=o.size))}return t}async function $(f,u,a){const c=Date.now(),n=`computer:${f}:${u}`,w=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!u)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});const t=String(f),s=String(u),d=h.join(w,t,s);if(!m.existsSync(d))return r(n,"INFO","Directory does not exist, returning empty list",{targetDir:d,userId:t,cId:s}),{files:[]};r(n,"DEBUG","Start getting user file list",{targetDir:d,userId:t,cId:s});try{const e=await O(d,d,n,a);return r(n,"INFO","User file list obtained successfully",{fileCount:e.length,targetDir:d,userId:t,cId:s,elapsedMs:Date.now()-c}),{files:e}}catch(e){throw r(n,"ERROR","Failed to get user file list",{targetDir:d,userId:t,cId:s,error:e.message,elapsedMs:Date.now()-c}),new D(`Failed to get file list: ${e.message}`,{targetDir:d,originalError:e.message})}}async function _(f,u,a){const c=Date.now(),n=`computer:${f}:${u}`,w=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!Array.isArray(a))throw new g("files must be an array",{field:"files"});const t=String(f),s=String(u),d=h.join(w,t,s);m.existsSync(d)||m.mkdirSync(d,{recursive:!0});for(let e=0;e<a.length;e++){const l=a[e];if(!l||typeof l.operation!="string")throw new g(`files[${e}].operation cannot be empty`,{field:`files[${e}].operation`});if(!l.name||typeof l.name!="string")throw new g(`files[${e}].name cannot be empty`,{field:`files[${e}].name`});const p=l.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(p))throw new g(`files[${e}].operation must be one of create, delete, rename or modify`,{field:`files[${e}].operation`});if(p==="rename"&&!l.renameFrom)throw new g(`files[${e}].renameFrom cannot be empty (rename operation requires)`,{field:`files[${e}].renameFrom`});if(p==="modify"&&l.isDir!==!0&&typeof l.contents!="string")throw new g(`files[${e}].contents must be a string (modify operation requires)`,{field:`files[${e}].contents`})}r(n,"DEBUG","Start updating user files",{userId:t,cId:s,filesCount:a.length});try{for(const e of a){const l=e.operation.toLowerCase(),p=e.name,o=h.normalize(p).replace(/^[\/\\]+/,""),i=h.join(d,o),y=h.resolve(i),R=h.resolve(d);if(!y.startsWith(R+h.sep)&&y!==R){r(n,"WARN","File path is not secure, skipping",{filePath:o,resolvedPath:y});continue}switch(l){case"create":{if(e.isDir===!0){if(m.existsSync(i)){if((await m.promises.stat(i)).isFile())throw new g("Cannot create directory, file with the same name already exists",{filePath:o});r(n,"INFO","Directory already exists, skipping creation",{filePath:o});break}await m.promises.mkdir(i,{recursive:!0}),r(n,"INFO","Directory created successfully",{filePath:o});break}if(m.existsSync(i)){if((await m.promises.stat(i)).isDirectory())throw new g("Cannot create file, directory with the same name already exists",{filePath:o});r(n,"INFO","File already exists, skipping creation",{filePath:o});break}await m.promises.mkdir(h.dirname(i),{recursive:!0});const F=e.contents||"";await m.promises.writeFile(i,F,"utf8"),r(n,"INFO","File created successfully",{filePath:o});break}case"delete":{m.existsSync(i)?(await m.promises.stat(i)).isDirectory()?(await m.promises.rm(i,{recursive:!0,force:!0}),r(n,"INFO","Directory deleted successfully",{filePath:o})):(await m.promises.unlink(i),r(n,"INFO","File deleted successfully",{filePath:o})):r(n,"WARN","The file or directory to be deleted does not exist",{filePath:o});break}case"rename":{const F=e.renameFrom;if(!F||typeof F!="string"){r(n,"WARN","Rename operation missing renameFrom",{filePath:o});break}const E=h.normalize(F).replace(/^[\/\\]+/,""),S=h.join(d,E),b=h.resolve(S);if(!b.startsWith(R+h.sep)&&b!==R){r(n,"WARN","Source path is not secure, skipping rename",{sourcePath:E,targetPath:o});break}if(m.existsSync(S)){const C=(await m.promises.stat(S)).isDirectory();await m.promises.mkdir(h.dirname(i),{recursive:!0}),await m.promises.rename(S,i),r(n,"INFO",C?"Directory renamed successfully":"File renamed successfully",{sourcePath:E,targetPath:o})}else r(n,"WARN","The file or directory to be renamed does not exist",{sourcePath:E});break}case"modify":{if(!m.existsSync(i)){r(n,"WARN","The file to be modified does not exist",{filePath:o});break}if((await m.promises.stat(i)).isDirectory()){r(n,"INFO","The target is a directory, skipping modification",{filePath:o});break}const E=typeof e.contents=="string"?e.contents:"";if(await m.promises.readFile(i,"utf8")===E){r(n,"INFO","File content has no changes, skipping write",{filePath:o});break}await m.promises.writeFile(i,E,"utf8"),r(n,"INFO","File modified successfully",{filePath:o});break}default:{r(n,"WARN","Unsupported operation type",{operation:l,filePath:o});break}}}return r(n,"INFO","User files updated successfully",{userId:t,cId:s,filesCount:a.length,elapsedMs:Date.now()-c}),{success:!0,message:"User files updated successfully",userId:t,cId:s,filesCount:a.length}}catch(e){throw r(n,"ERROR","User files updated failed",{userId:t,cId:s,error:e.message,elapsedMs:Date.now()-c}),new D(`User files updated failed: ${e.message}`,{userId:t,cId:s,originalError:e.message})}}async function z(f,u,a,c){const n=Date.now(),w=`computer:${f}:${u}`,t=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!a)throw new g("file cannot be empty",{field:"file"});if(!c||typeof c!="string")throw new g("filePath cannot be empty",{field:"filePath"});const s=String(f),d=String(u),e=h.join(t,s,d);m.existsSync(e)||m.mkdirSync(e,{recursive:!0});const l=h.normalize(c).replace(/^[\/\\]+/,""),p=h.join(e,l),o=h.resolve(p),i=h.resolve(e);if(!o.startsWith(i+h.sep)&&o!==i)throw new g("File path is not secure, cannot exceed user directory",{field:"filePath",providedPath:c,resolvedPath:o});try{if(await m.promises.mkdir(h.dirname(p),{recursive:!0}),a.buffer)await m.promises.writeFile(p,a.buffer);else if(typeof a.contents=="string")await m.promises.writeFile(p,a.contents,"utf8");else throw new g("File content format is incorrect",{field:"file",hasBuffer:!!a.buffer,hasContents:typeof a.contents});return r(w,"INFO","File uploaded successfully",{userId:s,cId:d,filePath:l,targetPath:o,fileSize:a.buffer?a.buffer.length:a.contents?a.contents.length:0,elapsedMs:Date.now()-n}),{success:!0,message:"File uploaded successfully",fileSize:a.buffer?a.buffer.length:a.contents?a.contents.length:0}}catch(y){throw r(w,"ERROR","File upload failed",{userId:s,cId:d,filePath:l,error:y.message,elapsedMs:Date.now()-n}),new D(`File upload failed: ${y.message}`,{userId:s,cId:d,filePath:l,originalError:y.message})}}async function T(f,u,a,c){const n=Date.now(),w=`computer:${f}:${u}`;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!Array.isArray(a))throw new g("files must be an array",{field:"files"});if(!Array.isArray(c))throw new g("filePaths must be an array",{field:"filePaths"});if(a.length!==c.length)throw new g(`File count (${a.length}) does not match path count (${c.length})`,{field:"filePaths"});r(w,"DEBUG","Start batch uploading files",{userId:f,cId:u,filesCount:a.length});const t=[];try{for(let e=0;e<a.length;e++){const l=a[e],p=c[e];if(!l){r(w,"WARN","Empty file object encountered in batch upload, skipping",{index:e,filePath:p}),t.push({success:!1,filePath:p,error:"Empty file object"});continue}if(!p||typeof p!="string"){r(w,"WARN","Invalid file path in batch upload, skipping",{index:e,originalname:l.originalname}),t.push({success:!1,filePath:p||"",originalname:l.originalname,error:"Invalid file path"});continue}try{const o=await z(f,u,l,p);t.push({success:!0,filePath:p,originalname:l.originalname,...o})}catch(o){r(w,"ERROR","Single file upload failed in batch upload",{filePath:p,originalname:l.originalname,error:o.message}),t.push({success:!1,filePath:p,originalname:l.originalname,error:o.message})}}const s=t.filter(e=>e.success).length,d=t.filter(e=>!e.success).length;return r(w,"INFO","Batch upload files completed",{userId:f,cId:u,totalCount:a.length,successCount:s,failCount:d,elapsedMs:Date.now()-n}),{success:!0,message:"Batch upload completed",totalCount:a.length,successCount:s,failCount:d,results:t}}catch(s){throw r(w,"ERROR","Batch upload files failed",{userId:f,cId:u,error:s.message,elapsedMs:Date.now()-n}),new D(`Batch upload files failed: ${s.message}`,{userId:f,cId:u,originalError:s.message})}}async function v(f,u){const a=Date.now(),c=`computer:${f}:${u}`,n=I.COMPUTER_WORKSPACE_DIR;if(!f)throw new g("userId cannot be empty",{field:"userId"});if(!u)throw new g("cId cannot be empty",{field:"cId"});if(!n)throw new D("COMPUTER_WORKSPACE_DIR is not configured, cannot create zip");const w=String(f),t=String(u),s=h.join(n,w,t);if(!m.existsSync(s)){const i=`${w}_${t}.zip`;r(c,"WARN","Workspace directory does not exist, returning empty zip",{targetDir:s,userId:w,cId:t,zipFileName:i});const y=N("zip",{zlib:{level:9}});return y.append(null,{name:`${w}_${t}/`,type:"directory"}),y.on("warning",R=>{if(R.code==="ENOENT")r(c,"WARN","Encountered file problem when creating empty zip",{message:R.message,code:R.code});else throw r(c,"ERROR","Encountered warning when creating empty zip",{message:R.message,code:R.code}),R}),y.on("error",R=>{r(c,"ERROR","Failed to create empty zip",{message:R.message})}),{archive:y,zipFileName:i}}const d=`${w}_${t}.zip`;r(c,"DEBUG","Start creating workspace directory zip",{targetDir:s,zipFileName:d});const e=N("zip",{zlib:{level:9}}),l=I.CONTENT_TRAVERSE_EXCLUDE_FILES||[],p=I.TRAVERSE_EXCLUDE_DIRS||[],o=await k(s,l,p,c);if(o>P){const i=P/1024/1024,y=(o/1024/1024).toFixed(2);throw r(c,"WARN","Download rejected due to oversized workspace",{targetDir:s,downloadableSize:o,maxSizeBytes:P}),new g(`Download failed: total file size ${y}MB exceeds limit ${i}MB`,{field:"downloadSize",downloadableSize:o,maxSizeBytes:P})}return e.directory(s,`${w}_${t}`,i=>{const y=i.name||"",R=y.split(/[\/\\]/).filter(Boolean);if(R.some(E=>E.startsWith(".")))return!1;const F=R[R.length-1];if(l.includes(F)||R.some(E=>p.includes(E)))return!1;try{const E=h.join(s,y),S=m.lstatSync(E);if(S.isSymbolicLink()||S.nlink>1)return!1}catch(E){return r(c,"WARN","Error occurred when detecting link file, skipping",{filePath:y,error:E.message}),!1}return i}),e.on("warning",i=>{if(i.code==="ENOENT")r(c,"WARN","Encountered file problem when creating zip",{message:i.message,code:i.code});else throw r(c,"ERROR","Encountered warning when creating zip",{message:i.message,code:i.code}),i}),e.on("error",i=>{r(c,"ERROR","Failed to create zip",{message:i.message,elapsedMs:Date.now()-a})}),e.on("end",()=>{r(c,"INFO","Workspace directory zip created successfully",{targetDir:s,zipFileName:d,elapsedMs:Date.now()-a})}),{archive:e,zipFileName:d}}export{$ as getFileList,_ as updateFiles,z as uploadFile,T as uploadFiles,v as downloadAllFiles};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"fs";import o from"path";import J from"../../appConfig/index.js";import{extractZip as M}from"../common/zipUtils.js";import{ValidationError as S,SystemError as U,FileError as C}from"../error/errorHandler.js";import{log as n}from"../log/logUtils.js";import{ensurePrimaryAgentDirs as G,syncAgents as B}from"../common/AgentWorkspaceUtils.js";function K(e){if(!e)return[];if(Array.isArray(e))return e.map(i=>typeof i=="string"?i.trim():"").filter(Boolean);if(typeof e=="string"){const i=e.trim();if(!i)return[];try{const a=JSON.parse(i);if(Array.isArray(a))return a.map(f=>typeof f=="string"?f.trim():"").filter(Boolean)}catch{}return[i]}return[]}async function L(e,i,a){let f;try{f=await fetch(e)}catch(E){throw new C(`Failed to download skill zip from url: ${e}`,{url:e,reason:E.message})}if(!f.ok)throw new C(`Failed to download skill zip from url: ${e}`,{url:e,status:f.status,statusText:f.statusText});const u=f.headers.get("content-type")||"";u&&!u.includes("zip")&&!u.includes("octet-stream")&&n(a,"WARN","Downloaded skill url content-type is not typical zip",{url:e,contentType:u});const s=await f.arrayBuffer(),y=Buffer.from(s);await t.promises.writeFile(i,y)}async function V(e="computer"){const i=J.COMPUTER_WORKSPACE_DIR;if(!i)throw new S("COMPUTER_WORKSPACE_DIR configuration does not exist",{field:"COMPUTER_WORKSPACE_DIR"});return t.existsSync(i)||(await t.promises.mkdir(i,{recursive:!0}),n(e,"INFO","Created user workspace root directory",{workspaceRoot:i})),i}async function Z(e,i){const a=o.join(e,i);if(t.existsSync(a)&&(await t.promises.lstat(a)).isDirectory())return a;const f=await t.promises.readdir(e,{withFileTypes:!0});for(const u of f){if(!u.isDirectory())continue;const s=o.join(e,u.name),y=o.join(s,i);if(t.existsSync(y)&&(await t.promises.lstat(y)).isDirectory())return y}return null}async function ot(e){return Z(e,"skills")}const X=".dynamic_add.lock";function Y(e){const i=o.join(e,X);return t.existsSync(i)&&t.statSync(i).isFile()}async function A(e){t.existsSync(e)&&await t.promises.rm(e,{recursive:!0,force:!0})}async function T(e,i){try{await t.promises.rename(e,i)}catch(a){if(a.code==="EXDEV"){async function f(u,s){if((await t.promises.lstat(u)).isDirectory()){await t.promises.mkdir(s,{recursive:!0});const E=await t.promises.readdir(u);for(const N of E)await f(o.join(u,N),o.join(s,N))}else await t.promises.copyFile(u,s)}await f(e,i),await t.promises.rm(e,{recursive:!0,force:!0})}else throw a}}async function q(e,i,a,f){const u=Date.now(),s=`computer:${e}:${i}`,y=K(f),E=[],N=[],P=new Set;if(!e)throw new S("userId cannot be empty",{field:"userId"});if(!i)throw new S("cId cannot be empty",{field:"cId"});const x=await V(s),D=o.join(x,String(e),String(i),".tmp"),_=o.join(x,String(e),String(i)),{skillsDir:g,agentsDir:l,agentTypes:R}=await G(_);t.existsSync(_)||await t.promises.mkdir(_,{recursive:!0});const v=o.join(D,`preserved_skills_${Date.now()}_${Math.round(Math.random()*1e6)}`);if(t.existsSync(g)){const p=(await t.promises.readdir(g,{withFileTypes:!0})).filter(m=>m.isDirectory()&&Y(o.join(g,m.name)));if(p.length>0){await t.promises.mkdir(v,{recursive:!0});for(const m of p){const d=o.join(g,m.name),h=o.join(v,m.name);await T(d,h)}n(s,"INFO","\u4FDD\u7559\u542B .dynamic_add.lock \u7684 skill",{preserved:p.map(m=>m.name)})}}if(await A(g),await A(l),await t.promises.mkdir(g,{recursive:!0}),await t.promises.mkdir(l,{recursive:!0}),t.existsSync(v)){const r=await t.promises.readdir(v,{withFileTypes:!0});for(const p of r)if(p.isDirectory()){const m=o.join(v,p.name),d=o.join(g,p.name);await T(m,d)}await A(v)}const k=t.existsSync(g),F=t.existsSync(l);if(n(s,"INFO","Deleted old skills and agents directories completed",{userId:e,cId:i,targetSkillsDir:g,targetAgentsDir:l,agentTypes:R,skillsExists:k,agentsExists:F}),!a&&y.length===0)return await B(_),n(s,"INFO","Created workspace (no uploaded file, no skills and agents)",{userId:e,cId:i,workspaceRoot:x,skillsDir:null,agentsDir:null,agentTypes:R,elapsedMs:Date.now()-u}),{message:"Workspace created (no uploaded file, no skills and agents)",workspaceRoot:x};if(a){if(!a.path)throw new S("Uploaded file has no valid path",{field:"file.path"});if(o.extname(a.originalname||a.filename||"").toLowerCase()!==".zip")throw new S("Only zip files are supported",{field:"file",originalName:a.originalname})}n(s,"DEBUG","Start processing workspace resources",{userId:e,cId:i,workspaceRoot:x,hasUploadedZip:!!a,skillUrlsCount:y.length});try{t.existsSync(D)||await t.promises.mkdir(D,{recursive:!0});const r=[];if(a){const d=o.join(D,`skill_extract_${Date.now()}_${Math.round(Math.random()*1e6)}`);N.push(d),await t.promises.mkdir(d,{recursive:!0}),n(s,"DEBUG","Start extracting uploaded zip file",{extractRoot:d}),await M(a.path,d),n(s,"DEBUG","Uploaded zip file extracted successfully",{extractRoot:d});const h=await Z(d,"skills"),j=await Z(d,"agents");if(h){await t.promises.mkdir(g,{recursive:!0});const c=await t.promises.readdir(h,{withFileTypes:!0});for(const z of c){if(!z.isDirectory())continue;const $=o.join(h,z.name),O=o.join(g,z.name);t.existsSync(O)&&await A(O),await T($,O),P.add(z.name)}r.push("skills"),n(s,"INFO","skills updated to workspace",{userId:e,cId:i,workspaceRoot:x,targetSkillsDir:g,agentTypes:R})}else n(s,"INFO","skills directory not found in uploaded zip, skipping",{userId:e,cId:i,extractRoot:d});j?(await A(l),await T(j,l),r.push("agents"),n(s,"INFO","agents updated to workspace",{userId:e,cId:i,workspaceRoot:x,targetAgentsDir:l,agentTypes:R})):n(s,"INFO","agents directory not found in uploaded zip, skipping",{userId:e,cId:i,extractRoot:d})}for(let d=0;d<y.length;d+=1){const h=y[d],j=o.join(D,`skill_url_${Date.now()}_${d}_${Math.round(Math.random()*1e6)}.zip`);E.push(j);const c=o.join(D,`skill_url_extract_${Date.now()}_${d}_${Math.round(Math.random()*1e6)}`);N.push(c),await t.promises.mkdir(c,{recursive:!0}),n(s,"INFO","Start download skill zip from url",{userId:e,cId:i,skillUrl:h}),await L(h,j,s),n(s,"INFO","Skill zip downloaded, start extracting",{userId:e,cId:i,skillUrl:h,downloadedZipPath:j}),await M(j,c);const $=(await t.promises.readdir(c,{withFileTypes:!0})).filter(w=>w.isDirectory()&&!w.name.startsWith(".")),b=$.find(w=>w.name==="skills")?(await t.promises.readdir(o.join(c,"skills"),{withFileTypes:!0})).filter(w=>w.isDirectory()&&!w.name.startsWith(".")).map(w=>({name:w.name,sourcePath:o.join(c,"skills",w.name)})):$.map(w=>({name:w.name,sourcePath:o.join(c,w.name)}));if(b.length===0){n(s,"WARN","No skill directory found after extracting skill url zip",{userId:e,cId:i,skillUrl:h,extractRoot:c});continue}await t.promises.mkdir(g,{recursive:!0});for(const w of b){const W=o.join(g,w.name);t.existsSync(W)&&await A(W),await T(w.sourcePath,W),P.add(w.name),n(s,"INFO","Skill from url updated to workspace",{userId:e,cId:i,skillUrl:h,skillName:w.name,destSkillPath:W})}r.includes("skills")||r.push("skills")}r.length===0&&n(s,"WARN","No valid skills or agents directories found",{userId:e,cId:i,hasUploadedZip:!!a,skillUrlsCount:y.length});const p=Array.from(P),m=r.length>0?`Workspace created successfully, ${r.join(" and ")} updated`:"Workspace created successfully (skills and agents directories not found)";return n(s,"INFO",m,{userId:e,cId:i,updatedDirs:r,updatedSkills:p,agentTypes:R,elapsedMs:Date.now()-u}),await B(_),{message:m,workspaceRoot:x,updatedSkills:p}}catch(r){throw n(s,"ERROR","Failed to process workspace resources",{userId:e,cId:i,error:r.message,elapsedMs:Date.now()-u}),r instanceof S||r instanceof C||r instanceof U?r:new U(`Failed to create workspace: ${r.message}`,{userId:e,cId:i})}finally{try{for(const r of N)t.existsSync(r)&&await t.promises.rm(r,{recursive:!0,force:!0})}catch(r){n(s,"WARN","Failed to clean up temporary extracted zip",{error:r.message})}for(const r of E)try{t.existsSync(r)&&await t.promises.unlink(r)}catch(p){n(s,"WARN","Failed to clean up downloaded skill zip file",{downloadedZipPath:r,error:p.message})}try{a&&a.path&&t.existsSync(a.path)&&await t.promises.unlink(a.path)}catch(r){n(s,"WARN","Failed to clean up uploaded zip file",{tempZipPath:a?.path,error:r.message})}}}async function H(e,i,a,f){const u=Date.now(),s=`computer:${e}:${i}`,y=K(f),E=[],N=[];if(!e)throw new S("userId cannot be empty",{field:"userId"});if(!i)throw new S("cId cannot be empty",{field:"cId"});if(!a&&y.length===0)throw new S("file or skillUrls cannot both be empty",{field:"file|skillUrls"});if(a){if(!a.path)throw new S("Uploaded file has no valid path",{field:"file.path"});if(o.extname(a.originalname||a.filename||"").toLowerCase()!==".zip")throw new S("Only zip files are supported",{field:"file",originalName:a?.originalname})}const P=await V(s),x=o.join(P,String(e),String(i),".tmp"),D=o.join(P,String(e),String(i)),{skillsDir:_,agentTypes:g}=await G(D);try{t.existsSync(D)||await t.promises.mkdir(D,{recursive:!0}),t.existsSync(_)||await t.promises.mkdir(_,{recursive:!0}),t.existsSync(x)||await t.promises.mkdir(x,{recursive:!0});const l=[],R=new Set;if(a){const k=o.join(x,`skill_push_${Date.now()}_${Math.round(Math.random()*1e6)}`);N.push(k),await t.promises.mkdir(k,{recursive:!0}),n(s,"DEBUG","Start extracting skill zip file",{extractRoot:k}),await M(a.path,k),n(s,"DEBUG","Skill zip file extracted successfully",{extractRoot:k});const F=await Z(k,"skills");if(!F)n(s,"WARN","skills directory not found in uploaded zip",{userId:e,cId:i,extractRoot:k});else{const p=(await t.promises.readdir(F,{withFileTypes:!0})).filter(m=>m.isDirectory()&&!m.name.startsWith("."));if(p.length===0)n(s,"WARN","skills directory in uploaded zip has no skill subdirectories",{userId:e,cId:i,skillsDir:F});else for(const m of p){const d=o.join(F,m.name),h=o.join(_,m.name);t.existsSync(h)&&await A(h),await T(d,h),R.add(m.name),n(s,"INFO","skill pushed to workspace from uploaded zip",{userId:e,cId:i,skillName:m.name,destSkillPath:h})}}}for(let k=0;k<y.length;k+=1){const F=y[k],r=o.join(x,`skill_push_url_${Date.now()}_${k}_${Math.round(Math.random()*1e6)}.zip`);E.push(r);const p=o.join(x,`skill_push_url_extract_${Date.now()}_${k}_${Math.round(Math.random()*1e6)}`);N.push(p),await t.promises.mkdir(p,{recursive:!0}),n(s,"INFO","Start download skill zip for push from url",{userId:e,cId:i,skillUrl:F}),await L(F,r,s),await M(r,p);const d=(await t.promises.readdir(p,{withFileTypes:!0})).filter(c=>c.isDirectory()&&!c.name.startsWith(".")),j=d.find(c=>c.name==="skills")?(await t.promises.readdir(o.join(p,"skills"),{withFileTypes:!0})).filter(c=>c.isDirectory()&&!c.name.startsWith(".")).map(c=>({name:c.name,sourcePath:o.join(p,"skills",c.name)})):d.map(c=>({name:c.name,sourcePath:o.join(p,c.name)}));if(j.length===0){n(s,"WARN","No skill directory found after extracting push skill url zip",{userId:e,cId:i,skillUrl:F,extractRoot:p});continue}for(const c of j){const z=o.join(_,c.name);t.existsSync(z)&&await A(z),await T(c.sourcePath,z),R.add(c.name),n(s,"INFO","skill pushed to workspace from url zip",{userId:e,cId:i,skillUrl:F,skillName:c.name,destSkillPath:z})}}for(const k of R)l.push(k);const v=l.length>0?`Pushed ${l.length} skills: ${l.join(", ")}`:"No valid skill directories found in file or skillUrls";return n(s,"INFO",v,{userId:e,cId:i,updatedSkills:l,agentTypes:g,elapsedMs:Date.now()-u}),await B(D),{message:v,workspaceRoot:P,updatedSkills:l}}catch(l){throw n(s,"ERROR","Failed to push skill to workspace",{userId:e,cId:i,error:l.message,elapsedMs:Date.now()-u}),l instanceof S||l instanceof C||l instanceof U?l:new U(`Failed to push skill to workspace: ${l.message}`,{userId:e,cId:i})}finally{try{for(const l of N)t.existsSync(l)&&await t.promises.rm(l,{recursive:!0,force:!0})}catch(l){n(s,"WARN","Failed to clean up temporary extracted zip",{error:l.message})}for(const l of E)try{t.existsSync(l)&&await t.promises.unlink(l)}catch(R){n(s,"WARN","Failed to clean up downloaded skill zip file",{downloadedZipPath:l,error:R.message})}try{a&&a.path&&t.existsSync(a.path)&&await t.promises.unlink(a.path)}catch(l){n(s,"WARN","Failed to clean up uploaded zip file",{tempZipPath:a?.path,error:l.message})}}}export{q as createWorkspace,H as pushSkillsToWorkspace};
|
|
1
|
+
import t from"fs";import a from"path";import{exec as tt}from"child_process";import X from"../../appConfig/index.js";import{extractZip as B}from"../common/zipUtils.js";import{ValidationError as k,SystemError as U,FileError as Z}from"../error/errorHandler.js";import{log as o}from"../log/logUtils.js";import{ensurePrimaryAgentDirs as Y,syncAgents as V}from"../common/AgentWorkspaceUtils.js";function Q(e){if(!e)return[];if(Array.isArray(e))return e.map(i=>typeof i=="string"?i.trim():"").filter(Boolean);if(typeof e=="string"){const i=e.trim();if(!i)return[];try{const s=JSON.parse(i);if(Array.isArray(s))return s.map(w=>typeof w=="string"?w.trim():"").filter(Boolean)}catch{}return[i]}return[]}async function I(e,i,s){let w;try{w=await fetch(e)}catch(N){throw new Z(`Failed to download skill zip from url: ${e}`,{url:e,reason:N.message})}if(!w.ok)throw new Z(`Failed to download skill zip from url: ${e}`,{url:e,status:w.status,statusText:w.statusText});const u=w.headers.get("content-type")||"";u&&!u.includes("zip")&&!u.includes("octet-stream")&&o(s,"WARN","Downloaded skill url content-type is not typical zip",{url:e,contentType:u});const n=await w.arrayBuffer(),x=Buffer.from(n);await t.promises.writeFile(i,x)}async function K(e="computer"){const i=X.COMPUTER_WORKSPACE_DIR;if(!i)throw new k("COMPUTER_WORKSPACE_DIR configuration does not exist",{field:"COMPUTER_WORKSPACE_DIR"});return t.existsSync(i)||(await t.promises.mkdir(i,{recursive:!0}),o(e,"INFO","Created user workspace root directory",{workspaceRoot:i})),i}async function H(e,i){const s=a.join(e,i);if(t.existsSync(s)&&(await t.promises.lstat(s)).isDirectory())return s;const w=await t.promises.readdir(e,{withFileTypes:!0});for(const u of w){if(!u.isDirectory())continue;const n=a.join(e,u.name),x=a.join(n,i);if(t.existsSync(x)&&(await t.promises.lstat(x)).isDirectory())return x}return null}async function ht(e){return H(e,"skills")}const et=".dynamic_add.lock";function it(e){const i=a.join(e,et);return t.existsSync(i)&&t.statSync(i).isFile()}async function T(e){t.existsSync(e)&&await t.promises.rm(e,{recursive:!0,force:!0})}async function W(e,i){try{await t.promises.rename(e,i)}catch(s){if(s.code==="EXDEV"){async function w(u,n){if((await t.promises.lstat(u)).isDirectory()){await t.promises.mkdir(n,{recursive:!0});const N=await t.promises.readdir(u);for(const h of N)await w(a.join(u,h),a.join(n,h))}else await t.promises.copyFile(u,n)}await w(e,i),await t.promises.rm(e,{recursive:!0,force:!0})}else throw s}}async function st(e,i,s,w,u,n){const x=i&&i.trim(),N=s&&s.trim(),h=w&&w.trim(),l=Array.isArray(u)&&u.length>0;if(!x&&!N&&!h&&!l)return;const d=a.join(e,".claude");try{if(t.existsSync(d)||await t.promises.mkdir(d,{recursive:!0}),x)try{const m={mcpServers:JSON.parse(i)},S=a.join(e,".mcp.json");await t.promises.writeFile(S,JSON.stringify(m,null,2),"utf-8"),o(n,"INFO","Written .mcp.json to workspace root")}catch(m){o(n,"WARN","Failed to parse/write mcpServersConfig, skipping",{error:m.message})}const p={};if(N)try{p.hooks=JSON.parse(s)}catch(m){o(n,"WARN","Failed to parse hooksConfig, skipping",{error:m.message})}if(h)try{p.permissions=JSON.parse(w)}catch(m){o(n,"WARN","Failed to parse permissionsConfig, skipping",{error:m.message})}if(Object.keys(p).length>0){const m=a.join(d,"settings.json");await t.promises.writeFile(m,JSON.stringify(p,null,2),"utf-8"),o(n,"INFO","Written .claude/settings.json",{keys:Object.keys(p)})}if(l){const m=a.join(d,"hooks");t.existsSync(m)||await t.promises.mkdir(m,{recursive:!0});for(const S of u){if(!S||!S.path||!S.content)continue;const c=a.normalize(S.path);if(c.startsWith("..")||a.isAbsolute(c)){o(n,"WARN","Hook script path contains traversal, skipping",{path:S.path});continue}const y=a.join(d,c),z=a.dirname(y);t.existsSync(z)||await t.promises.mkdir(z,{recursive:!0}),await t.promises.writeFile(y,S.content,"utf-8"),await t.promises.chmod(y,493),o(n,"INFO","Written hook script",{path:S.path})}}}catch(p){o(n,"WARN","Failed to write .claude/settings or .mcp.json or hook scripts, not blocking workspace creation",{error:p.message})}}async function at(e,i,s,w,u,n,x,N){const h=Date.now(),l=`computer:${e}:${i}`,d=Q(w),p=[],m=[],S=new Set;if(!e)throw new k("userId cannot be empty",{field:"userId"});if(!i)throw new k("cId cannot be empty",{field:"cId"});const c=await K(l),y=a.join(c,String(e),String(i),".tmp"),z=a.join(c,String(e),String(i)),{skillsDir:f,agentsDir:j,agentTypes:A}=await Y(z);t.existsSync(z)||await t.promises.mkdir(z,{recursive:!0});const D=a.join(y,`preserved_skills_${Date.now()}_${Math.round(Math.random()*1e6)}`);if(t.existsSync(f)){const F=(await t.promises.readdir(f,{withFileTypes:!0})).filter(O=>O.isDirectory()&&it(a.join(f,O.name)));if(F.length>0){await t.promises.mkdir(D,{recursive:!0});for(const O of F){const g=a.join(f,O.name),P=a.join(D,O.name);await W(g,P)}o(l,"INFO","\u4FDD\u7559\u542B .dynamic_add.lock \u7684 skill",{preserved:F.map(O=>O.name)})}}if(await T(f),await T(j),await t.promises.mkdir(f,{recursive:!0}),await t.promises.mkdir(j,{recursive:!0}),t.existsSync(D)){const r=await t.promises.readdir(D,{withFileTypes:!0});for(const F of r)if(F.isDirectory()){const O=a.join(D,F.name),g=a.join(f,F.name);await W(O,g)}await T(D)}const _=t.existsSync(f),M=t.existsSync(j);o(l,"INFO","Deleted old skills and agents directories completed",{userId:e,cId:i,targetSkillsDir:f,targetAgentsDir:j,agentTypes:A,skillsExists:_,agentsExists:M});const E=a.join(z,".mcp.json");t.existsSync(E)&&await t.promises.unlink(E);const b=a.join(z,".claude","settings.json");if(t.existsSync(b)&&await t.promises.unlink(b),await st(z,u,x,n,N,l),!s&&d.length===0)return await V(z),o(l,"INFO","Created workspace (no uploaded file, no skills and agents)",{userId:e,cId:i,workspaceRoot:c,skillsDir:null,agentsDir:null,agentTypes:A,elapsedMs:Date.now()-h}),{message:"Workspace created (no uploaded file, no skills and agents)",workspaceRoot:c};if(s){if(!s.path)throw new k("Uploaded file has no valid path",{field:"file.path"});if(a.extname(s.originalname||s.filename||"").toLowerCase()!==".zip")throw new k("Only zip files are supported",{field:"file",originalName:s.originalname})}o(l,"DEBUG","Start processing workspace resources",{userId:e,cId:i,workspaceRoot:c,hasUploadedZip:!!s,skillUrlsCount:d.length});try{t.existsSync(y)||await t.promises.mkdir(y,{recursive:!0});const r=[];if(s){const g=a.join(y,`skill_extract_${Date.now()}_${Math.round(Math.random()*1e6)}`);m.push(g),await t.promises.mkdir(g,{recursive:!0}),o(l,"DEBUG","Start extracting uploaded zip file",{extractRoot:g}),await B(s.path,g),o(l,"DEBUG","Uploaded zip file extracted successfully",{extractRoot:g});const P=await H(g,"skills"),$=await H(g,"agents");if(P){await t.promises.mkdir(f,{recursive:!0});const v=await t.promises.readdir(P,{withFileTypes:!0});for(const C of v){if(!C.isDirectory())continue;const G=a.join(P,C.name),L=a.join(f,C.name);t.existsSync(L)&&await T(L),await W(G,L),S.add(C.name)}r.push("skills"),o(l,"INFO","skills updated to workspace",{userId:e,cId:i,workspaceRoot:c,targetSkillsDir:f,agentTypes:A})}else o(l,"INFO","skills directory not found in uploaded zip, skipping",{userId:e,cId:i,extractRoot:g});$?(await T(j),await W($,j),r.push("agents"),o(l,"INFO","agents updated to workspace",{userId:e,cId:i,workspaceRoot:c,targetAgentsDir:j,agentTypes:A})):o(l,"INFO","agents directory not found in uploaded zip, skipping",{userId:e,cId:i,extractRoot:g})}for(let g=0;g<d.length;g+=1){const P=d[g],$=a.join(y,`skill_url_${Date.now()}_${g}_${Math.round(Math.random()*1e6)}.zip`);p.push($);const v=a.join(y,`skill_url_extract_${Date.now()}_${g}_${Math.round(Math.random()*1e6)}`);m.push(v),await t.promises.mkdir(v,{recursive:!0}),o(l,"INFO","Start download skill zip from url",{userId:e,cId:i,skillUrl:P}),await I(P,$,l),o(l,"INFO","Skill zip downloaded, start extracting",{userId:e,cId:i,skillUrl:P,downloadedZipPath:$}),await B($,v);const G=(await t.promises.readdir(v,{withFileTypes:!0})).filter(R=>R.isDirectory()&&!R.name.startsWith(".")),q=G.find(R=>R.name==="skills")?(await t.promises.readdir(a.join(v,"skills"),{withFileTypes:!0})).filter(R=>R.isDirectory()&&!R.name.startsWith(".")).map(R=>({name:R.name,sourcePath:a.join(v,"skills",R.name)})):G.map(R=>({name:R.name,sourcePath:a.join(v,R.name)}));if(q.length===0){o(l,"WARN","No skill directory found after extracting skill url zip",{userId:e,cId:i,skillUrl:P,extractRoot:v});continue}await t.promises.mkdir(f,{recursive:!0});for(const R of q){const J=a.join(f,R.name);t.existsSync(J)&&await T(J),await W(R.sourcePath,J),S.add(R.name),o(l,"INFO","Skill from url updated to workspace",{userId:e,cId:i,skillUrl:P,skillName:R.name,destSkillPath:J})}r.includes("skills")||r.push("skills")}r.length===0&&o(l,"WARN","No valid skills or agents directories found",{userId:e,cId:i,hasUploadedZip:!!s,skillUrlsCount:d.length});const F=Array.from(S),O=r.length>0?`Workspace created successfully, ${r.join(" and ")} updated`:"Workspace created successfully (skills and agents directories not found)";return o(l,"INFO",O,{userId:e,cId:i,updatedDirs:r,updatedSkills:F,agentTypes:A,elapsedMs:Date.now()-h}),await V(z),{message:O,workspaceRoot:c,updatedSkills:F}}catch(r){throw o(l,"ERROR","Failed to process workspace resources",{userId:e,cId:i,error:r.message,elapsedMs:Date.now()-h}),r instanceof k||r instanceof Z||r instanceof U?r:new U(`Failed to create workspace: ${r.message}`,{userId:e,cId:i})}finally{try{for(const r of m)t.existsSync(r)&&await t.promises.rm(r,{recursive:!0,force:!0})}catch(r){o(l,"WARN","Failed to clean up temporary extracted zip",{error:r.message})}for(const r of p)try{t.existsSync(r)&&await t.promises.unlink(r)}catch(F){o(l,"WARN","Failed to clean up downloaded skill zip file",{downloadedZipPath:r,error:F.message})}try{s&&s.path&&t.existsSync(s.path)&&await t.promises.unlink(s.path)}catch(r){o(l,"WARN","Failed to clean up uploaded zip file",{tempZipPath:s?.path,error:r.message})}}}async function ot(e,i,s,w){const u=Date.now(),n=`computer:${e}:${i}`,x=Q(w),N=[],h=[];if(!e)throw new k("userId cannot be empty",{field:"userId"});if(!i)throw new k("cId cannot be empty",{field:"cId"});if(!s&&x.length===0)throw new k("file or skillUrls cannot both be empty",{field:"file|skillUrls"});if(s){if(!s.path)throw new k("Uploaded file has no valid path",{field:"file.path"});if(a.extname(s.originalname||s.filename||"").toLowerCase()!==".zip")throw new k("Only zip files are supported",{field:"file",originalName:s?.originalname})}const l=await K(n),d=a.join(l,String(e),String(i),".tmp"),p=a.join(l,String(e),String(i)),{skillsDir:m,agentTypes:S}=await Y(p);try{t.existsSync(p)||await t.promises.mkdir(p,{recursive:!0}),t.existsSync(m)||await t.promises.mkdir(m,{recursive:!0}),t.existsSync(d)||await t.promises.mkdir(d,{recursive:!0});const c=[],y=new Set;if(s){const f=a.join(d,`skill_push_${Date.now()}_${Math.round(Math.random()*1e6)}`);h.push(f),await t.promises.mkdir(f,{recursive:!0}),o(n,"DEBUG","Start extracting skill zip file",{extractRoot:f}),await B(s.path,f),o(n,"DEBUG","Skill zip file extracted successfully",{extractRoot:f});const j=await H(f,"skills");if(!j)o(n,"WARN","skills directory not found in uploaded zip",{userId:e,cId:i,extractRoot:f});else{const D=(await t.promises.readdir(j,{withFileTypes:!0})).filter(_=>_.isDirectory()&&!_.name.startsWith("."));if(D.length===0)o(n,"WARN","skills directory in uploaded zip has no skill subdirectories",{userId:e,cId:i,skillsDir:j});else for(const _ of D){const M=a.join(j,_.name),E=a.join(m,_.name);t.existsSync(E)&&await T(E),await W(M,E),y.add(_.name),o(n,"INFO","skill pushed to workspace from uploaded zip",{userId:e,cId:i,skillName:_.name,destSkillPath:E})}}}for(let f=0;f<x.length;f+=1){const j=x[f],A=a.join(d,`skill_push_url_${Date.now()}_${f}_${Math.round(Math.random()*1e6)}.zip`);N.push(A);const D=a.join(d,`skill_push_url_extract_${Date.now()}_${f}_${Math.round(Math.random()*1e6)}`);h.push(D),await t.promises.mkdir(D,{recursive:!0}),o(n,"INFO","Start download skill zip for push from url",{userId:e,cId:i,skillUrl:j}),await I(j,A,n),await B(A,D);const M=(await t.promises.readdir(D,{withFileTypes:!0})).filter(r=>r.isDirectory()&&!r.name.startsWith(".")),b=M.find(r=>r.name==="skills")?(await t.promises.readdir(a.join(D,"skills"),{withFileTypes:!0})).filter(r=>r.isDirectory()&&!r.name.startsWith(".")).map(r=>({name:r.name,sourcePath:a.join(D,"skills",r.name)})):M.map(r=>({name:r.name,sourcePath:a.join(D,r.name)}));if(b.length===0){o(n,"WARN","No skill directory found after extracting push skill url zip",{userId:e,cId:i,skillUrl:j,extractRoot:D});continue}for(const r of b){const F=a.join(m,r.name);t.existsSync(F)&&await T(F),await W(r.sourcePath,F),y.add(r.name),o(n,"INFO","skill pushed to workspace from url zip",{userId:e,cId:i,skillUrl:j,skillName:r.name,destSkillPath:F})}}for(const f of y)c.push(f);const z=c.length>0?`Pushed ${c.length} skills: ${c.join(", ")}`:"No valid skill directories found in file or skillUrls";return o(n,"INFO",z,{userId:e,cId:i,updatedSkills:c,agentTypes:S,elapsedMs:Date.now()-u}),await V(p),{message:z,workspaceRoot:l,updatedSkills:c}}catch(c){throw o(n,"ERROR","Failed to push skill to workspace",{userId:e,cId:i,error:c.message,elapsedMs:Date.now()-u}),c instanceof k||c instanceof Z||c instanceof U?c:new U(`Failed to push skill to workspace: ${c.message}`,{userId:e,cId:i})}finally{try{for(const c of h)t.existsSync(c)&&await t.promises.rm(c,{recursive:!0,force:!0})}catch(c){o(n,"WARN","Failed to clean up temporary extracted zip",{error:c.message})}for(const c of N)try{t.existsSync(c)&&await t.promises.unlink(c)}catch(y){o(n,"WARN","Failed to clean up downloaded skill zip file",{downloadedZipPath:c,error:y.message})}try{s&&s.path&&t.existsSync(s.path)&&await t.promises.unlink(s.path)}catch(c){o(n,"WARN","Failed to clean up uploaded zip file",{tempZipPath:s?.path,error:c.message})}}}async function rt(e,i,s,w){const u=Date.now(),n=`computer:${e}:${i}`;if(!e)throw new k("userId cannot be empty",{field:"userId"});if(!i)throw new k("cId cannot be empty",{field:"cId"});if(!s||!s.path)throw new k("file is required",{field:"file"});if(a.extname(s.originalname||s.filename||"").toLowerCase()!==".zip")throw new k("Only zip files are supported",{field:"file",originalName:s.originalname});const N=await K(n),h=a.join(N,String(e),String(i)),l=a.join(h,".tmp"),d=a.join(l,`template_extract_${Date.now()}_${Math.round(Math.random()*1e6)}`);try{t.existsSync(h)||await t.promises.mkdir(h,{recursive:!0}),t.existsSync(l)||await t.promises.mkdir(l,{recursive:!0}),await t.promises.mkdir(d,{recursive:!0}),o(n,"DEBUG","Start extracting template zip file",{extractRoot:d}),await B(s.path,d),o(n,"DEBUG","Template zip file extracted successfully",{extractRoot:d});const p=await t.promises.readdir(d,{withFileTypes:!0});for(const m of p){const S=a.join(d,m.name),c=a.join(h,m.name);if(m.isDirectory())await W(S,c);else{const y=a.dirname(c);t.existsSync(y)||await t.promises.mkdir(y,{recursive:!0}),await t.promises.copyFile(S,c)}}if(o(n,"INFO","Template files extracted to workspace",{userId:e,cId:i,targetDir:h,fileCount:p.length}),X.GIT_ENABLED&&(w==="true"||w===!0)){const m=await import("../../service/gitService.js");await m.default.init({workspaceType:"taskAgent",userId:e,cId:i}),await m.default.commit({workspaceType:"taskAgent",userId:e,cId:i,message:"Initial commit"}),o(n,"INFO","Git init and initial commit done",{userId:e,cId:i})}return o(n,"INFO","Init project template completed",{userId:e,cId:i,targetDir:h,elapsedMs:Date.now()-u}),{message:"Project template initialized successfully",workspaceRoot:h}}catch(p){throw o(n,"ERROR","Failed to init project template",{userId:e,cId:i,error:p.message,elapsedMs:Date.now()-u}),p instanceof k||p instanceof Z||p instanceof U?p:new U(`Failed to init project template: ${p.message}`,{userId:e,cId:i})}finally{try{t.existsSync(d)&&await t.promises.rm(d,{recursive:!0,force:!0})}catch(p){o(n,"WARN","Failed to clean up temporary extracted zip",{error:p.message})}try{s&&s.path&&t.existsSync(s.path)&&await t.promises.unlink(s.path)}catch(p){o(n,"WARN","Failed to clean up uploaded zip file",{tempZipPath:s?.path,error:p.message})}}}async function nt(e,i,s){if(!e)throw new k("userId cannot be empty",{field:"userId"});if(!i)throw new k("cId cannot be empty",{field:"cId"});if(!s||typeof s!="string"||!s.trim())throw new k("command cannot be empty",{field:"command"});const w=await K("computer"),u=a.join(w,String(e),String(i));if(!t.existsSync(u))throw new k("workspace directory does not exist",{field:"workDir",workDir:u});const n=`computer:${e}:${i}`;o(n,"INFO","Execute command in workspace",{userId:e,cId:i,workDir:u,command:s});const x=5*60*1e3;return new Promise((N,h)=>{tt(s,{cwd:u,timeout:x,maxBuffer:50*1024*1024},(l,d,p)=>{const m=l?l.code||1:0;o(n,"INFO","Execute command completed",{userId:e,cId:i,exitCode:m,stdoutLength:d?d.length:0,stderrLength:p?p.length:0}),N({stdout:d||"",stderr:p||"",exitCode:m})})})}export{at as createWorkspace,ot as pushSkillsToWorkspace,rt as initProjectTemplate,nt as executeCommand};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import f from"path";import o from"fs";import{simpleGit as c}from"simple-git";import r from"../../appConfig/index.js";function m(n){return c(n)}function l(n){return o.existsSync(f.join(n,".git"))}async function d(n){if(!l(n)){const i=c(n);await i.init(["-b","main"]),await i.addConfig("user.name",r.GIT_DEFAULT_AUTHOR_NAME,!1,"local"),await i.addConfig("user.email",r.GIT_DEFAULT_AUTHOR_EMAIL,!1,"local"),await i.addConfig("init.defaultBranch","main",!1,"local"),await i.commit("Initial commit",["--allow-empty"])}g(n)}export{m as getGitInstance,l as isGitRepo,d as ensureGitRepo,g as ensureGitignore,G as autoGitAdd};async function G(n,i){if(l(n))try{const t=m(n);Array.isArray(i)&&i.length>0?await t.add(i):await t.add("--all")}catch{}}function g(n){if(!r.GIT_AUTO_GITIGNORE)return;const i=f.join(n,".gitignore"),t=r.GIT_GITIGNORE_ENTRIES||[];let e=[];o.existsSync(i)&&(e=o.readFileSync(i,"utf8").split(/\r?\n/));const u=new Set(e.map(a=>a.trim()).filter(Boolean)),s=t.filter(a=>!u.has(a));if(s.length>0){const a=e.length>0&&e[e.length-1]!==""?`
|
|
2
|
+
`+s.join(`
|
|
3
|
+
`)+`
|
|
4
|
+
`:s.join(`
|
|
5
|
+
`)+`
|
|
6
|
+
`;o.appendFileSync(i,a,"utf8")}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import r from"fs";import c from"path";import{log as a}from"../log/logUtils.js";import{copyDirectoryFiltered as $}from"./backupUtils.js";import{ValidationError as y,BusinessError as w,SystemError as x}from"../error/errorHandler.js";import{createPnpmNpmrc as E}from"../common/npmrcUtils.js";import{copyNodeModulesFromCache as C}from"../common/templateCacheUtils.js";import{resolveProjectPath as u}from"../common/projectPathUtils.js";import F from"../../appConfig/index.js";async function S(o,i,f={}){if(!o)throw new y("Source project ID cannot be empty",{field:"sourceProjectId"});if(!i)throw new y("Target project ID cannot be empty",{field:"targetProjectId"});const h=f.sourceIsolationContext||{},m=f.targetIsolationContext||{},n=u(o,h),s=u(i,m);if(!r.existsSync(n))throw new w(`Source project ${o} does not exist`,{sourceProjectId:o,sourceProjectPath:n});if(r.existsSync(s))throw new w(`Target project ${i} already exists`,{targetProjectId:i,targetProjectPath:s});try{a(i,"INFO",`Start copying project from ${o} to ${i}`,{sourceProjectId:o,targetProjectId:i}),r.mkdirSync(s,{recursive:!0}),a(i,"INFO",`Target project directory created successfully: ${s}`,{targetProjectId:i});const p=await r.promises.readdir(n,{withFileTypes:!0});for(const e of p){const l=c.join(n,e.name),t=c.join(s,e.name);e.isDirectory()?(await r.promises.mkdir(t,{recursive:!0}),await $(l,t)):e.isFile()&&(await r.promises.mkdir(c.dirname(t),{recursive:!0}),await r.promises.copyFile(l,t))}if(a(i,"INFO",`Project copied successfully: ${i}`,{sourceProjectId:o,targetProjectId:i}),await C(s,i),await E(s,i),F.GIT_ENABLED)try{const e=await import("../../service/gitService.js");await e.init({workspaceType:"pageApp",projectId:i,isolationContext:m}),await e.commit({workspaceType:"pageApp",projectId:i,isolationContext:m,message:`copy project: ${o} -> ${i}`})}catch(e){a(i,"WARN","Git init/commit failed, skipping",{error:e.message})}return{success:!0,message:`Project ${o} successfully copied to ${i}`,sourceProjectId:o,targetProjectId:i,targetProjectPath:s}}catch(p){if(a(i,"ERROR",`Copy project failed: ${p.message}`,{sourceProjectId:o,targetProjectId:i}),r.existsSync(s))try{await r.promises.rm(s,{recursive:!0,force:!0}),a(i,"INFO","Copy failed, target project directory cleaned",{targetProjectId:i})}catch(e){a(i,"ERROR",`Clean target project directory failed: ${e.message}`,{targetProjectId:i,originalError:e.message})}throw p.isOperational?p:new x(`Copy project failed: ${p.message}`,{sourceProjectId:o,targetProjectId:i,sourceProjectPath:n,targetProjectPath:s,originalError:p.message})}}export{S as copyProject};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import l from"fs";import m from"path";import
|
|
1
|
+
import l from"fs";import m from"path";import E from"../../appConfig/index.js";import{log as o}from"../log/logUtils.js";import{ValidationError as F,SystemError as C,ResourceError as R}from"../error/errorHandler.js";import{extractZip as b}from"../common/zipUtils.js";import{getFrameworkInfo as x}from"./frameworkDetectorUtils.js";import{resolveProjectPath as p,shouldUseIsolationPath as $}from"../common/projectPathUtils.js";async function _(e){try{const s=l.readFileSync(e);if(s.includes(0))return!0;const c=s.toString("utf-8");for(let f=0;f<c.length;f++){const i=c.charCodeAt(f);if(i<32&&i!==9&&i!==10&&i!==13)return!0}return!1}catch{return!1}}function h(e){const s=m.extname(e).toLowerCase();return[".jpg",".jpeg",".png",".gif",".bmp",".webp",".svg"].includes(s)}async function D(e,s,c,f){const i=[],a=await l.promises.readdir(e,{withFileTypes:!0});a.sort((r,n)=>r.isDirectory()&&!n.isDirectory()?-1:!r.isDirectory()&&n.isDirectory()?1:r.name.toLowerCase().localeCompare(n.name.toLowerCase()));for(const r of a){const n=m.join(e,r.name);if(!(r.name.startsWith(".")&&r.name!==".gitignore"||(E.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(r.name))&&!(r.isDirectory()&&E.TRAVERSE_EXCLUDE_DIRS.includes(r.name)))if(r.isDirectory()){const t=await D(n,s,c,f);if(t.length===0){const u=s||p(c),y=m.relative(u,n).replace(/\\/g,"/");i.push({name:y,isDir:!0})}else i.push(...t)}else try{const t=await l.promises.stat(n),u=s||p(c),y=m.relative(u,n),w=await _(n),S=`${f}/${y}`,d={name:y,isDir:!1,binary:w,sizeExceeded:t.size>E.MAX_INLINE_FILE_SIZE_BYTES,contents:"",fileProxyUrl:S};if(!d.sizeExceeded)if(d.binary){if(h(n)){const v=l.readFileSync(n);d.contents=v.toString("base64")}}else d.contents=l.readFileSync(n,"utf-8");i.push(d)}catch(t){o(c,"WARN",`Process file failed: ${n} - ${t.message}`,{filePath:n,error:t.message})}}return i}async function N(e,s,c){const f=Date.now(),i=m.basename(e);try{o(i,"INFO","Start getting project content",{projectPath:e,command:s}),o(i,"DEBUG","Start traversing project directory",{projectPath:e});const a=await D(e,e,i,c);o(i,"DEBUG","Project directory traversal completed",{projectPath:e,fileCount:a.length});let r=a;s!=="cpage_config"&&(r=a.filter(t=>t.name!=="cpage_config.json")),o(i,"DEBUG","Start detecting framework information",{projectPath:e});const n=x(e),g={files:r,...n};return o(i,"INFO",`Project content obtained, total ${r.length} files`,{projectPath:e,fileCount:r.length,command:s,elapsedMs:Date.now()-f}),g}catch(a){throw o(i,"ERROR",`Get project content failed: ${a.message}`,{projectPath:e,originalError:a.message,elapsedMs:Date.now()-f}),new C(`Get project content failed: ${a.message}`,{projectPath:e,originalError:a.message})}}async function U(e,s,c,f,i={}){const a=Date.now(),r=Number(s);if(!Number.isFinite(r))throw new F("Code version must be a number",{field:"codeVersion"});const n=m.join(E.UPLOAD_PROJECT_DIR,e),g=m.join(n,`${e}-v${r}.zip`);if(!l.existsSync(g))throw new R(`Backup file for version ${r} does not exist`,{projectId:e,codeVersion:r,backupZipPath:g});const t=$(i)?m.join(E.PROJECT_SOURCE_DIR,String(i.tenantId),String(i.spaceId),"_his",e):m.join(E.PROJECT_SOURCE_DIR,"_his",e);try{l.existsSync(t)&&await l.promises.rm(t,{recursive:!0,force:!0}),await l.promises.mkdir(t,{recursive:!0}),o(e,"INFO",`Start extracting backup file for version ${r}`,{projectId:e,codeVersion:r,backupZipPath:g,tempExtractDir:t}),o(e,"DEBUG","Start extracting version backup file",{projectId:e,backupZipPath:g,tempExtractDir:t}),await b(g,t),o(e,"DEBUG",`Version ${r} backup file extraction completed`,{projectId:e,codeVersion:r,tempExtractDir:t}),o(e,"DEBUG","Start traversing version directory",{projectId:e,tempExtractDir:t});const u=await D(t,t,e,f);let y=u;c!=="cpage_config"&&(y=u.filter(S=>S.name!=="cpage_config.json"));const w={files:y};return o(e,"INFO",`Version ${r} project content obtained`,{projectId:e,codeVersion:r,fileCount:w.files?w.files.length:0,command:c,elapsedMs:Date.now()-a}),w}finally{try{l.existsSync(t)&&(await l.promises.rm(t,{recursive:!0,force:!0}),o(e,"INFO",`Temporary directory cleaned: ${t}`,{projectId:e,codeVersion:r}))}catch(u){o(e,"WARN",`Clean temporary directory failed: ${u.message}`,{projectId:e,codeVersion:r,tempExtractDir:t,error:u.message})}}}export{N as getProjectContent,U as getProjectContentByVersion,D as traverseDirectory,_ as isBinaryFile,h as isImageFile};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuwax-file-server",
|
|
3
3
|
"displayName": "nuwax-file-server",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0-beta.11",
|
|
5
5
|
"description": "Cross-platform file service deployment tool with start/stop/restart CLI commands",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "index.js",
|
|
@@ -77,6 +77,7 @@
|
|
|
77
77
|
"multer": "^2.0.2",
|
|
78
78
|
"node-cron": "^3.0.3",
|
|
79
79
|
"pm2": "^6.0.11",
|
|
80
|
+
"simple-git": "^3.36.0",
|
|
80
81
|
"swagger-jsdoc": "^6.2.8",
|
|
81
82
|
"swagger-ui-express": "^5.0.0",
|
|
82
83
|
"tree-kill": "^1.2.2",
|