nuwax-file-server 1.2.7 → 1.2.8
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/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.7",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.2.8",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();
|
|
@@ -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 P(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=[],A=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 P(g),await P(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 P(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 P(O),await T($,O),A.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 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 P(W),await T(w.sourcePath,W),A.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(A),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 A=await V(s),x=o.join(A,String(e),String(i),".tmp"),D=o.join(A,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 P(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 P(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:A,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 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};
|
package/package.json
CHANGED