nuwax-file-server 1.0.6 → 1.2.0
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 +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as
|
|
2
|
+
import{Command as N}from"commander";import g from"path";import y from"os";import u from"fs-extra";import{spawn as D}from"cross-spawn";import{fileURLToPath as T}from"url";import{createRequire as b}from"module";var L=g.dirname(T(import.meta.url)),K=b(import.meta.url),E="1.2.0",l=new N,$={name:"nuwax-file-server",pidDir:g.join(y.tmpdir(),"nuwax-file-server"),logDir:g.join(y.tmpdir(),"nuwax-file-server","logs"),pidFile:g.join(y.tmpdir(),"nuwax-file-server","server.pid")},d={reset:"\x1B[0m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"};function a(e,o="reset"){console.log(`${d[o]}${e}${d.reset}`)}function f(e){console.error(`${d.red}ERROR: ${e}${d.reset}`)}function r(e){console.log(`${d.green}${e}${d.reset}`)}function s(e){console.log(`${d.blue}${e}${d.reset}`)}function h(){return $.pidFile}function R(){try{let e=h();if(u.existsSync(e)){let o=u.readFileSync(e,"utf8");return JSON.parse(o)}}catch{}return null}function j(e){let o=g.dirname(h());u.ensureDirSync(o),u.writeFileSync(h(),JSON.stringify(e,null,2))}function x(){let e=h();u.existsSync(e)&&u.removeSync(e)}function v(e){try{return process.kill(e,0),!0}catch(o){return o.code==="ESRCH"||o.code==="EPERM",!1}}async function k(e,o=!1){return new Promise(t=>{if(process.platform==="win32"){let i=o?["/F","/PID",String(e)]:["/PID",String(e)],n=D("taskkill",i,{stdio:"pipe"});n.on("error",c=>{f(`\u505C\u6B62\u8FDB\u7A0B\u5931\u8D25: ${c.message}`),t(!1)}),n.on("close",c=>{c===0?(r(`\u8FDB\u7A0B ${e} \u5DF2\u505C\u6B62`),t(!0)):o?D("taskkill",["/F","/PID",String(e)],{stdio:"pipe"}).on("close",m=>{t(m===0)}):t(!1)})}else try{process.kill(-e,o?"SIGKILL":"SIGTERM"),r(`\u8FDB\u7A0B\u7EC4 ${e} \u5DF2\u505C\u6B62`),t(!0)}catch(i){if(i.code==="ESRCH")r(`\u8FDB\u7A0B ${e} \u4E0D\u5B58\u5728\uFF0C\u89C6\u4E3A\u5DF2\u505C\u6B62`),t(!0);else try{process.kill(e,o?"SIGKILL":"SIGTERM"),r(`\u8FDB\u7A0B ${e} \u5DF2\u505C\u6B62`),t(!0)}catch(n){n.code==="ESRCH"?(r(`\u8FDB\u7A0B ${e} \u4E0D\u5B58\u5728\uFF0C\u89C6\u4E3A\u5DF2\u505C\u6B62`),t(!0)):(f(`\u505C\u6B62\u8FDB\u7A0B\u5931\u8D25: ${n.message}`),t(!1))}}})}async function F(e){let{env:o,port:t,config:w}=e,i=new Set(["env","port","config","force","help","version"]);process.argv.slice(2).forEach(p=>{if(p.startsWith("--")&&p.includes("=")){let O=p.indexOf("="),P=p.slice(2,O);if(!i.has(P)){let _=p.slice(O+1);process.env[P]=_,s(`CLI \u53C2\u6570\u8986\u76D6\u73AF\u5883\u53D8\u91CF: ${P}=${_}`)}}}),s(`\u542F\u52A8 ${$.name} \u670D\u52A1...`);let n=R();n&&v(n.pid)&&(f(`\u670D\u52A1\u5DF2\u5728\u8FD0\u884C\u4E2D (PID: ${n.pid})`),s("\u8BF7\u4F7F\u7528 'nuwax-file-server stop' \u505C\u6B62\u73B0\u6709\u670D\u52A1\u540E\u518D\u8BD5"),process.exit(1));let c=o||"production";process.env.NODE_ENV=c,s(`\u4F7F\u7528\u73AF\u5883: ${c}`),t&&(process.env.PORT=t,s(`\u4F7F\u7528\u7AEF\u53E3: ${t}`)),w&&(process.env.CONFIG_FILE=w,s(`\u4F7F\u7528\u914D\u7F6E\u6587\u4EF6: ${w}`)),u.ensureDirSync($.logDir);let I=g.join(L,"server.js"),m=D("node",[I],{env:{...process.env,NODE_ENV:c},stdio:"inherit",detached:!0,cwd:process.cwd(),windowsHide:!0});m.on("error",p=>{f(`\u542F\u52A8\u670D\u52A1\u5931\u8D25: ${p.message}`),process.exit(1)}),await new Promise(p=>setTimeout(p,2e3)),v(m.pid)||(f("\u670D\u52A1\u542F\u52A8\u5931\u8D25"),process.exit(1));let S={pid:m.pid,startedAt:new Date().toISOString(),env:o||"production",port:t||process.env.PORT||"60000",version:E,platform:process.platform};j(S),m.unref(),r(`\u670D\u52A1\u5DF2\u542F\u52A8 (PID: ${m.pid})`),a(`\u670D\u52A1\u8FD0\u884C\u5728: http://localhost:${S.port}`,"cyan"),a(`\u73AF\u5883: ${S.env}`,"cyan"),a(`\u5E73\u53F0: ${S.platform}`,"cyan"),a(`PID \u6587\u4EF6: ${h()}`,"cyan")}async function C(e){let{force:o}=e;s(`\u505C\u6B62 ${$.name} \u670D\u52A1...`);let t=R();t||(f("\u672A\u627E\u5230\u8FD0\u884C\u4E2D\u7684\u670D\u52A1"),s("\u670D\u52A1\u53EF\u80FD\u672A\u542F\u52A8\u6216 PID \u6587\u4EF6\u5DF2\u4E22\u5931"),process.exit(0)),v(t.pid)||(s("\u670D\u52A1\u8FDB\u7A0B\u5DF2\u505C\u6B62\uFF0C\u6E05\u7406 PID \u6587\u4EF6..."),x(),r("\u670D\u52A1\u5DF2\u505C\u6B62"),process.exit(0)),await k(t.pid,o)&&(await new Promise(n=>setTimeout(n,1e3)),v(t.pid)||(x(),r("\u670D\u52A1\u5DF2\u505C\u6B62"),process.exit(0))),o&&(f("\u5F3A\u5236\u505C\u6B62\u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u505C\u6B62\u8FDB\u7A0B"),process.exit(1)),s("\u5C1D\u8BD5\u5F3A\u5236\u505C\u6B62..."),await k(t.pid,!0)&&(await new Promise(n=>setTimeout(n,1e3)),v(t.pid)||(x(),r("\u670D\u52A1\u5DF2\u5F3A\u5236\u505C\u6B62"),process.exit(0))),f("\u505C\u6B62\u670D\u52A1\u5931\u8D25"),process.exit(1)}async function A(e){a(`\u91CD\u542F ${$.name} \u670D\u52A1...`,"yellow"),s("\u505C\u6B62\u73B0\u6709\u670D\u52A1...");try{await C({force:!1})}catch{s("\u670D\u52A1\u672A\u8FD0\u884C\u6216\u5DF2\u505C\u6B62")}await new Promise(o=>setTimeout(o,2e3)),s("\u542F\u52A8\u670D\u52A1..."),await F(e),r("\u670D\u52A1\u5DF2\u91CD\u542F")}function G(){s(`${$.name} \u670D\u52A1\u72B6\u6001:`);let e=R();e||(a("\u670D\u52A1\u672A\u8FD0\u884C","yellow"),process.exit(0));let o=v(e.pid);if(console.log(""),console.log(` \u670D\u52A1\u540D\u79F0: ${$.name}`),console.log(` \u8FD0\u884C\u72B6\u6001: ${o?"\u8FD0\u884C\u4E2D":"\u5DF2\u505C\u6B62"}`),console.log(` \u8FDB\u7A0B ID: ${e.pid}`),console.log(` \u73AF\u5883: ${e.env||"production"}`),console.log(` \u7AEF\u53E3: ${e.port||"60000"}`),console.log(` \u7248\u672C: ${e.version||E}`),console.log(` \u5E73\u53F0: ${e.platform||process.platform}`),console.log(` \u542F\u52A8\u65F6\u95F4: ${e.startedAt||"\u672A\u77E5"}`),e.startedAt){let t=new Date(e.startedAt),i=Math.floor((new Date-t)/1e3),n=Math.floor(i/3600),c=Math.floor(i%3600/60),I=i%60;console.log(` \u8FD0\u884C\u65F6\u95F4: ${n}\u5C0F\u65F6 ${c}\u5206 ${I}\u79D2`)}console.log(` PID \u6587\u4EF6: ${h()}`),console.log(""),o?a("\u670D\u52A1\u8FD0\u884C\u6B63\u5E38","green"):(a("\u8B66\u544A: \u670D\u52A1\u8FDB\u7A0B\u4E0D\u5B58\u5728\uFF0C\u4F46 PID \u6587\u4EF6\u4ECD\u5B58\u5728","yellow"),s("\u5EFA\u8BAE\u6267\u884C stop \u547D\u4EE4\u6E05\u7406"))}function H(){l.name("nuwax-file-server").description("\u8DE8\u5E73\u53F0\u6587\u4EF6\u670D\u52A1\u90E8\u7F72\u5DE5\u5177\uFF0C\u652F\u6301 start/stop/restart/status").version(E,"-v, --version","\u663E\u793A\u7248\u672C\u53F7").helpOption("-h, --help","\u663E\u793A\u5E2E\u52A9\u4FE1\u606F"),l.command("start").allowUnknownOption().description("\u542F\u52A8\u670D\u52A1").option("--env <environment>","\u73AF\u5883: development|production|test","production").option("--port <port>","\u670D\u52A1\u7AEF\u53E3\u53F7").option("--config <path>","\u81EA\u5B9A\u4E49\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84").action(F),l.command("stop").description("\u505C\u6B62\u670D\u52A1").option("--force","\u5F3A\u5236\u505C\u6B62").action(C),l.command("restart").allowUnknownOption().description("\u91CD\u542F\u670D\u52A1").option("--env <environment>","\u73AF\u5883: development|production|test","production").option("--port <port>","\u670D\u52A1\u7AEF\u53E3\u53F7").option("--config <path>","\u81EA\u5B9A\u4E49\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84").action(A),l.command("status").description("\u67E5\u770B\u670D\u52A1\u72B6\u6001").action(G),l.command("help").description("\u663E\u793A\u5E2E\u52A9\u4FE1\u606F").action(()=>{l.outputHelp()}),l.parse(process.argv),process.argv.slice(2).length||(l.outputHelp(),process.exit(0))}H();
|