nuwax-file-server 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
  ```bash
18
18
  # 克隆项目
19
19
  git clone <repository-url>
20
- cd subapp-deployer
20
+ cd nuwax-file-server
21
21
 
22
22
  # 安装依赖
23
23
  npm install
package/dist/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
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();
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.2",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();
@@ -3,7 +3,7 @@ NODE_ENV=development
3
3
  PORT=60000
4
4
 
5
5
  # 日志配置
6
- LOG_BASE_DIR=/Users/leilinzhou/Work/custom_page/project_logs
6
+ LOG_BASE_DIR=/app/logs/project_logs
7
7
  LOG_LEVEL=debug
8
8
  LOG_CONSOLE_ENABLED=true
9
9
  LOG_PREFIX_API=api
@@ -11,16 +11,16 @@ LOG_PREFIX_BUILD=build
11
11
 
12
12
  # 系统内置初始化工程
13
13
  INIT_PROJECT_NAME=react-vite-template
14
- INIT_PROJECT_DIR=/Users/leilinzhou/Work/custom_page/project_init
14
+ INIT_PROJECT_DIR=/app/project_init
15
15
 
16
16
  #上传的项目压缩包所在路径
17
- UPLOAD_PROJECT_DIR=/Users/leilinzhou/Work/custom_page/project_zips
17
+ UPLOAD_PROJECT_DIR=/app/project_zips
18
18
 
19
19
  #项目源文件所在路径
20
- PROJECT_SOURCE_DIR=/Users/leilinzhou/Work/custom_page/project_workspace
20
+ PROJECT_SOURCE_DIR=/app/project_workspace
21
21
 
22
22
  #dist拷贝到的目标目录,nginx加载
23
- DIST_TARGET_DIR=/Users/leilinzhou/Work/custom_page/project_nginx
23
+ DIST_TARGET_DIR=/app/project_nginx
24
24
 
25
25
  #build最大并发数
26
26
  MAX_BUILD_CONCURRENCY=20
@@ -54,13 +54,9 @@ INLINE_IMAGE_EXTENSIONS=.png,.jpg,.jpeg,.gif,.bmp,.svg,.ico,.webp,.avif
54
54
  TOP_LEVEL_NOISE_PATTERNS=__MACOSX,Thumbs.db,node_modules,.pnpm-store,.attachments
55
55
 
56
56
  # 开发服务器配置
57
- # 从日志中解析实际端口,最多等待5秒
58
57
  DEV_SERVER_PORT_TIMEOUT=5000
59
- # 等待进程真正停止,最多等待5秒
60
58
  DEV_SERVER_STOP_TIMEOUT=5000
61
- # 等待进程真正停止,每100ms检查一次
62
59
  DEV_SERVER_STOP_CHECK_INTERVAL=100
63
- # 等待进程真正停止,最大重试次数
64
60
  DEV_SERVER_STOP_MAX_ATTEMPTS=50
65
61
 
66
62
  # pnpm清理配置
@@ -80,6 +76,6 @@ LOG_CACHE_MAX_ENTRIES=100
80
76
  LOG_CACHE_MAX_FILE_SIZE=2097152
81
77
 
82
78
  # computer 工作目录
83
- COMPUTER_WORKSPACE_DIR=/Users/leilinzhou/Work/computer-project-workspace
79
+ COMPUTER_WORKSPACE_DIR=/app/computer-project-workspace
84
80
  # computer 日志目录
85
- COMPUTER_LOG_DIR=/Users/leilinzhou/Work/custom_page/computer_logs
81
+ COMPUTER_LOG_DIR=/app/logs/computer_logs
@@ -1 +1 @@
1
- import $ from"express";import g from"multer";import d from"fs";import y from"path";import{ValidationError as E,asyncHandler as f}from"../utils/error/errorHandler.js";import{log as m}from"../utils/log/logUtils.js";import w from"../appConfig/index.js";import{createWorkspace as S}from"../utils/computer/computerUtils.js";import{getFileList as A,updateFiles as z,uploadFile as N,uploadFiles as P,downloadAllFiles as R}from"../utils/computer/computerFileUtils.js";const h=$.Router(),I=g({storage:g.diskStorage({destination:(e,a,s)=>{try{const t=w.COMPUTER_WORKSPACE_DIR;if(!t)return s(new Error("COMPUTER_WORKSPACE_DIR \u672A\u914D\u7F6E\uFF0C\u65E0\u6CD5\u786E\u5B9A\u4E0A\u4F20\u4E34\u65F6\u76EE\u5F55"));const o=e.body?.userId||"unknown",n=e.body?.cId||"unknown",i=y.join(t,String(o),String(n),".tmp");d.existsSync(i)||d.mkdirSync(i,{recursive:!0}),s(null,i)}catch(t){s(t)}},filename:(e,a,s)=>{const t=y.extname(a.originalname)||".zip",o=y.basename(a.originalname,t),n=`${Date.now()}_${Math.round(Math.random()*1e6)}`;s(null,`${o}_${n}${t}`)}}),limits:{fileSize:w.UPLOAD_MAX_FILE_SIZE_BYTES}});function b(e,a,s,t){if(e&&e.name==="MulterError"||e&&(e.name==="ValidationError"||e instanceof E))return t(e);t(e)}const C=[{path:"/create-workspace",method:"post",middleware:I.single("file"),handler:f(async(e,a)=>{const{userId:s,cId:t}=e.body||{},o=e.file||null,n=`computer:${s}:${t}`;m(n,"INFO","\u521B\u5EFA\u5DE5\u4F5C\u7A7A\u95F4\u8BF7\u6C42",{userId:s,cId:t,hasFile:!!o,fileName:o?.originalname,fileSize:o?.size});const i=await S(s,t,o);a.status(200).json({success:!0,...i})})},{path:"/get-file-list",method:"get",handler:f(async(e,a)=>{const{userId:s,cId:t,proxyPath:o}=e.query,n=await A(s,t,o);a.status(200).json({success:!0,...n})})},{path:"/files-update",method:"post",handler:f(async(e,a)=>{const{userId:s,cId:t,files:o}=e.body||{},n=`computer:${s}:${t}`;m(n,"INFO","\u6587\u4EF6\u66F4\u65B0",{userId:s,cId:t,filesCount:o?o.length:0}),o&&Array.isArray(o)&&o.forEach(r=>{if(r&&typeof r.contents=="string"&&r.contents)try{r.contents=decodeURIComponent(r.contents)}catch(l){m(n,"WARN","\u89E3\u7801\u6587\u4EF6\u5185\u5BB9\u5931\u8D25",{fileName:r.name,error:l.message})}});const i=await z(s,t,o);a.status(200).json(i)})},{path:"/upload-file",method:"post",middleware:I.single("file"),handler:f(async(e,a)=>{const{userId:s,cId:t,filePath:o}=e.body||{},n=e.file,i=`computer:${s}:${t}`;m(i,"INFO","\u4E0A\u4F20\u5355\u4E2A\u6587\u4EF6",{userId:s,cId:t,filePath:o});const l={buffer:await d.promises.readFile(n.path),originalname:n.originalname,mimetype:n.mimetype,size:n.size};try{const u=await N(s,t,l,o);a.status(200).json(u)}finally{d.existsSync(n.path)&&await d.promises.unlink(n.path)}})},{path:"/upload-files",method:"post",middleware:I.array("files"),handler:f(async(e,a)=>{const{userId:s,cId:t,filePaths:o}=e.body||{},n=e.files||[],i=`computer:${s}:${t}`,r=Array.isArray(o)?o:typeof o=="string"?[o]:o;m(i,"INFO","\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u8BF7\u6C42",{userId:s,cId:t,filesCount:n.length,filePathsCount:Array.isArray(r)?r.length:0});const l=[],u=[];try{for(const c of n){u.push(c.path);const F=await d.promises.readFile(c.path);l.push({buffer:F,originalname:c.originalname,mimetype:c.mimetype,size:c.size})}const p=await P(s,t,l,r);a.status(200).json(p)}finally{for(const p of u)if(d.existsSync(p))try{await d.promises.unlink(p)}catch(c){m(i,"WARN","\u6E05\u7406\u4E34\u65F6\u6587\u4EF6\u5931\u8D25",{tempPath:p,error:c.message})}}})},{path:"/download-all-files",method:"get",handler:f(async(e,a)=>{const{userId:s,cId:t}=e.query||{},o=`computer:${s}:${t}`;m(o,"INFO","\u4E0B\u8F7D\u5168\u90E8\u6587\u4EF6\u8BF7\u6C42",{userId:s,cId:t});const{archive:n,zipFileName:i}=await R(s,t);a.setHeader("Content-Type","application/zip");const r=encodeURIComponent(i);a.setHeader("Content-Disposition",`attachment; filename="${r}"; filename*=UTF-8''${r}`),n.on("error",l=>{a.destroy(l)}),n.pipe(a),n.finalize()})}];C.forEach(e=>{if(e.middleware)h[e.method](e.path,e.middleware,e.handler);else if(e.customHandler){const a=[];e.handler&&a.push(e.handler),e.decodeMiddleware&&a.push(e.decodeMiddleware),a.push(b),a.push(e.customHandler),h[e.method](e.path,...a)}else h[e.method](e.path,e.handler)});export default h;
1
+ import $ from"express";import g from"multer";import c from"fs";import I from"path";import{ValidationError as S,asyncHandler as f}from"../utils/error/errorHandler.js";import{log as d}from"../utils/log/logUtils.js";import w from"../appConfig/index.js";import{createWorkspace as z,pushSkillsToWorkspace as E}from"../utils/computer/computerUtils.js";import{getFileList as N,updateFiles as A,uploadFile as b,uploadFiles as k,downloadAllFiles as P}from"../utils/computer/computerFileUtils.js";const h=$.Router(),y=g({storage:g.diskStorage({destination:(e,a,o)=>{try{const t=w.COMPUTER_WORKSPACE_DIR;if(!t)return o(new Error("COMPUTER_WORKSPACE_DIR \u672A\u914D\u7F6E\uFF0C\u65E0\u6CD5\u786E\u5B9A\u4E0A\u4F20\u4E34\u65F6\u76EE\u5F55"));const s=e.body?.userId||"unknown",i=e.body?.cId||"unknown",n=I.join(t,String(s),String(i),".tmp");c.existsSync(n)||c.mkdirSync(n,{recursive:!0}),o(null,n)}catch(t){o(t)}},filename:(e,a,o)=>{const t=I.extname(a.originalname)||".zip",s=I.basename(a.originalname,t),i=`${Date.now()}_${Math.round(Math.random()*1e6)}`;o(null,`${s}_${i}${t}`)}}),limits:{fileSize:w.UPLOAD_MAX_FILE_SIZE_BYTES}});function R(e,a,o,t){if(e&&e.name==="MulterError"||e&&(e.name==="ValidationError"||e instanceof S))return t(e);t(e)}const C=[{path:"/create-workspace",method:"post",middleware:y.single("file"),handler:f(async(e,a)=>{const{userId:o,cId:t}=e.body||{},s=e.file||null,i=`computer:${o}:${t}`;d(i,"INFO","\u521B\u5EFA\u5DE5\u4F5C\u7A7A\u95F4\u8BF7\u6C42",{userId:o,cId:t,hasFile:!!s,fileName:s?.originalname,fileSize:s?.size});const n=await z(o,t,s);a.status(200).json({success:!0,...n})})},{path:"/push-skills-to-workspace",method:"post",middleware:y.single("file"),handler:f(async(e,a)=>{const{userId:o,cId:t}=e.body||{},s=e.file||null,i=`computer:${o}:${t}`;d(i,"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 E(o,t,s);a.status(200).json({success:!0,...n})})},{path:"/get-file-list",method:"get",handler:f(async(e,a)=>{const{userId:o,cId:t,proxyPath:s}=e.query,i=await N(o,t,s);a.status(200).json({success:!0,...i})})},{path:"/files-update",method:"post",handler:f(async(e,a)=>{const{userId:o,cId:t,files:s}=e.body||{},i=`computer:${o}:${t}`;d(i,"INFO","\u6587\u4EF6\u66F4\u65B0",{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){d(i,"WARN","\u89E3\u7801\u6587\u4EF6\u5185\u5BB9\u5931\u8D25",{fileName:r.name,error:l.message})}});const n=await A(o,t,s);a.status(200).json(n)})},{path:"/upload-file",method:"post",middleware:y.single("file"),handler:f(async(e,a)=>{const{userId:o,cId:t,filePath:s}=e.body||{},i=e.file,n=`computer:${o}:${t}`;d(n,"INFO","\u4E0A\u4F20\u5355\u4E2A\u6587\u4EF6",{userId:o,cId:t,filePath:s});const l={buffer:await c.promises.readFile(i.path),originalname:i.originalname,mimetype:i.mimetype,size:i.size};try{const u=await b(o,t,l,s);a.status(200).json(u)}finally{c.existsSync(i.path)&&await c.promises.unlink(i.path)}})},{path:"/upload-files",method:"post",middleware:y.array("files"),handler:f(async(e,a)=>{const{userId:o,cId:t,filePaths:s}=e.body||{},i=e.files||[],n=`computer:${o}:${t}`,r=Array.isArray(s)?s:typeof s=="string"?[s]:s;d(n,"INFO","\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u8BF7\u6C42",{userId:o,cId:t,filesCount:i.length,filePathsCount:Array.isArray(r)?r.length:0});const l=[],u=[];try{for(const m of i){u.push(m.path);const F=await c.promises.readFile(m.path);l.push({buffer:F,originalname:m.originalname,mimetype:m.mimetype,size:m.size})}const p=await k(o,t,l,r);a.status(200).json(p)}finally{for(const p of u)if(c.existsSync(p))try{await c.promises.unlink(p)}catch(m){d(n,"WARN","\u6E05\u7406\u4E34\u65F6\u6587\u4EF6\u5931\u8D25",{tempPath:p,error:m.message})}}})},{path:"/download-all-files",method:"get",handler:f(async(e,a)=>{const{userId:o,cId:t}=e.query||{},s=`computer:${o}:${t}`;d(s,"INFO","\u4E0B\u8F7D\u5168\u90E8\u6587\u4EF6\u8BF7\u6C42",{userId:o,cId:t});const{archive:i,zipFileName:n}=await P(o,t);a.setHeader("Content-Type","application/zip");const r=encodeURIComponent(n);a.setHeader("Content-Disposition",`attachment; filename="${r}"; filename*=UTF-8''${r}`),i.on("error",l=>{a.destroy(l)}),i.pipe(a),i.finalize()})}];C.forEach(e=>{if(e.middleware)h[e.method](e.path,e.middleware,e.handler);else if(e.customHandler){const a=[];e.handler&&a.push(e.handler),e.decodeMiddleware&&a.push(e.decodeMiddleware),a.push(R),a.push(e.customHandler),h[e.method](e.path,...a)}else h[e.method](e.path,e.handler)});export default h;
package/dist/server.js CHANGED
@@ -1 +1 @@
1
- import O from"express";import"json-bigint";import m from"swagger-ui-express";import E from"./config/swagger.js";import s from"./appConfig/index.js";import{log as c,logger as I}from"./utils/log/logUtils.js";import S from"./utils/log/logCacheManager.js";import{errorHandler as P,notFoundHandler as R}from"./utils/error/errorHandler.js";import w from"./routes/router.js";import{cleanupInitProjectOnStartup as y}from"./utils/project/initProjectCleanupUtils.js";import{startScheduler as N,stopScheduler as D}from"./scheduler/pnpmPruneScheduler.js";import T from"path";const o=O();o.use(O.json({limit:s.REQUEST_BODY_LIMIT,reviver:(t,e)=>typeof e=="number"&&!Number.isSafeInteger(e)?e.toString():e})),o.use(O.urlencoded({extended:!0,limit:s.REQUEST_BODY_LIMIT})),o.use(I);const C=t=>{let e=t;try{for(;;){const n=decodeURIComponent(e);if(n===e)break;e=n}}catch{}return e};o.use("/api/page/static/:projectId",(t,e,n)=>{const{projectId:l}=t.params;let i=t.path||"/";if(!l||i==="/")return e.status(404).send("Not Found");const r=t.headers.origin,a=r||"*";if(e.header("Access-Control-Allow-Origin",a),e.header("Access-Control-Allow-Methods","HEAD,GET,POST,PUT,DELETE,OPTIONS"),e.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control, Fragment"),e.header("Access-Control-Expose-Headers","Content-Type"),r&&(e.header("Access-Control-Allow-Credentials","true"),e.header("Vary","Origin")),t.method==="OPTIONS")return e.sendStatus(200);i=i.replace(/^\/+/,"");const p=C(i),A=T.join(s.PROJECT_SOURCE_DIR,l,p),d={"Access-Control-Allow-Origin":a,"Access-Control-Allow-Methods":"HEAD,GET,POST,PUT,DELETE,OPTIONS","Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control, Fragment"};return r&&(d["Access-Control-Allow-Credentials"]="true",d.Vary="Origin"),e.sendFile(A,{dotfiles:"ignore",headers:d},u=>{if(u)return n()})}),o.use("/api/computer/static/:userId/:cId",(t,e,n)=>{const{userId:l,cId:i}=t.params;let r=t.path||"/";if(!l||!i||r==="/")return e.status(404).send("Not Found");const a=t.headers.origin,p=a||"*";if(e.header("Access-Control-Allow-Origin",p),e.header("Access-Control-Allow-Methods","HEAD,GET,POST,PUT,DELETE,OPTIONS"),e.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"),e.header("Access-Control-Expose-Headers","Content-Type"),a&&(e.header("Access-Control-Allow-Credentials","true"),e.header("Vary","Origin")),t.method==="OPTIONS")return e.sendStatus(200);r=r.replace(/^\/+/,"");const A=C(r),d=T.join(s.COMPUTER_WORKSPACE_DIR,l,i,A),u={"Access-Control-Allow-Origin":p,"Access-Control-Allow-Methods":"HEAD,GET,POST,PUT,DELETE,OPTIONS","Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"};return a&&(u["Access-Control-Allow-Credentials"]="true",u.Vary="Origin"),e.sendFile(d,{dotfiles:"ignore",headers:u},g=>{if(g)return n()})}),o.use("/api-docs",m.serve,m.setup(E,{customCss:".swagger-ui .topbar { display: none }",customSiteTitle:"Subapp Deployer API \u6587\u6863"})),o.use(w),o.use(R),o.use(P);const h=o.listen(s.PORT,async()=>{c("default","INFO",`Server is running on port ${s.PORT} (${s.NODE_ENV} mode)`),await y(s);try{N()}catch(t){c("default","ERROR",`pnpm prune \u5B9A\u65F6\u4EFB\u52A1\u542F\u52A8\u5931\u8D25: ${t.message}`)}});h.timeout=6e5,h.keepAliveTimeout=61e4,h.headersTimeout=62e4;const f=t=>{c("default","INFO",`\u6536\u5230 ${t} \u4FE1\u53F7\uFF0C\u51C6\u5907\u4F18\u96C5\u9000\u51FA...`),D();try{S.destroy(),c("default","INFO","\u65E5\u5FD7\u7F13\u5B58\u7BA1\u7406\u5668\u5DF2\u6E05\u7406")}catch(e){c("default","ERROR",`\u6E05\u7406\u65E5\u5FD7\u7F13\u5B58\u7BA1\u7406\u5668\u5931\u8D25: ${e.message}`)}h.close(()=>{c("default","INFO","\u670D\u52A1\u5668\u5DF2\u5173\u95ED"),process.exit(0)}),setTimeout(()=>{c("default","ERROR","\u5F3A\u5236\u9000\u51FA\uFF08\u8D85\u65F6\uFF09"),process.exit(1)},3e4)};process.on("SIGTERM",()=>f("SIGTERM")),process.on("SIGINT",()=>f("SIGINT"));
1
+ import O from"express";import"json-bigint";import m from"swagger-ui-express";import E from"./config/swagger.js";import s from"./appConfig/index.js";import{log as c,logger as I}from"./utils/log/logUtils.js";import S from"./utils/log/logCacheManager.js";import{errorHandler as P,notFoundHandler as R}from"./utils/error/errorHandler.js";import w from"./routes/router.js";import{cleanupInitProjectOnStartup as y}from"./utils/project/initProjectCleanupUtils.js";import{startScheduler as N,stopScheduler as D}from"./scheduler/pnpmPruneScheduler.js";import T from"path";const o=O();o.use(O.json({limit:s.REQUEST_BODY_LIMIT,reviver:(t,e)=>typeof e=="number"&&!Number.isSafeInteger(e)?e.toString():e})),o.use(O.urlencoded({extended:!0,limit:s.REQUEST_BODY_LIMIT})),o.use(I);const f=t=>{let e=t;try{for(;;){const n=decodeURIComponent(e);if(n===e)break;e=n}}catch{}return e};o.use("/api/page/static/:projectId",(t,e,n)=>{const{projectId:l}=t.params;let i=t.path||"/";if(!l||i==="/")return e.status(404).send("Not Found");const r=t.headers.origin,a=r||"*";if(e.header("Access-Control-Allow-Origin",a),e.header("Access-Control-Allow-Methods","HEAD,GET,POST,PUT,DELETE,OPTIONS"),e.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control, Fragment"),e.header("Access-Control-Expose-Headers","Content-Type"),r&&(e.header("Access-Control-Allow-Credentials","true"),e.header("Vary","Origin")),t.method==="OPTIONS")return e.sendStatus(200);i=i.replace(/^\/+/,"");const p=f(i),A=T.join(s.PROJECT_SOURCE_DIR,l,p),d={"Access-Control-Allow-Origin":a,"Access-Control-Allow-Methods":"HEAD,GET,POST,PUT,DELETE,OPTIONS","Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control, Fragment"};return r&&(d["Access-Control-Allow-Credentials"]="true",d.Vary="Origin"),e.sendFile(A,{dotfiles:"ignore",headers:d},u=>{if(u)return n()})}),o.use("/api/computer/static/:userId/:cId",(t,e,n)=>{const{userId:l,cId:i}=t.params;let r=t.path||"/";if(!l||!i||r==="/")return e.status(404).send("Not Found");const a=t.headers.origin,p=a||"*";if(e.header("Access-Control-Allow-Origin",p),e.header("Access-Control-Allow-Methods","HEAD,GET,POST,PUT,DELETE,OPTIONS"),e.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"),e.header("Access-Control-Expose-Headers","Content-Type"),a&&(e.header("Access-Control-Allow-Credentials","true"),e.header("Vary","Origin")),t.method==="OPTIONS")return e.sendStatus(200);r=r.replace(/^\/+/,"");const A=f(r),d=T.join(s.COMPUTER_WORKSPACE_DIR,l,i,A),u={"Access-Control-Allow-Origin":p,"Access-Control-Allow-Methods":"HEAD,GET,POST,PUT,DELETE,OPTIONS","Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"};return a&&(u["Access-Control-Allow-Credentials"]="true",u.Vary="Origin"),e.sendFile(d,{dotfiles:"ignore",headers:u},g=>{if(g)return n()})}),o.use("/api-docs",m.serve,m.setup(E,{customCss:".swagger-ui .topbar { display: none }",customSiteTitle:"nuwax-file-server API \u6587\u6863"})),o.use(w),o.use(R),o.use(P);const h=o.listen(s.PORT,async()=>{c("default","INFO",`Server is running on port ${s.PORT} (${s.NODE_ENV} mode)`),await y(s);try{N()}catch(t){c("default","ERROR",`pnpm prune \u5B9A\u65F6\u4EFB\u52A1\u542F\u52A8\u5931\u8D25: ${t.message}`)}});h.timeout=6e5,h.keepAliveTimeout=61e4,h.headersTimeout=62e4;const C=t=>{c("default","INFO",`\u6536\u5230 ${t} \u4FE1\u53F7\uFF0C\u51C6\u5907\u4F18\u96C5\u9000\u51FA...`),D();try{S.destroy(),c("default","INFO","\u65E5\u5FD7\u7F13\u5B58\u7BA1\u7406\u5668\u5DF2\u6E05\u7406")}catch(e){c("default","ERROR",`\u6E05\u7406\u65E5\u5FD7\u7F13\u5B58\u7BA1\u7406\u5668\u5931\u8D25: ${e.message}`)}h.close(()=>{c("default","INFO","\u670D\u52A1\u5668\u5DF2\u5173\u95ED"),process.exit(0)}),setTimeout(()=>{c("default","ERROR","\u5F3A\u5236\u9000\u51FA\uFF08\u8D85\u65F6\uFF09"),process.exit(1)},3e4)};process.on("SIGTERM",()=>C("SIGTERM")),process.on("SIGINT",()=>C("SIGINT"));
@@ -1,4 +1,4 @@
1
- import a from"path";import o from"fs";import F from"../appConfig/index.js";import{restartDevServer as $}from"../utils/build/restartDevUtils.js";import{log as t}from"../utils/log/logUtils.js";import{ValidationError as m,SystemError as v,ResourceError as S}from"../utils/error/errorHandler.js";import"../utils/common/sensitiveUtils.js";import{backupProjectToZip as C,restoreProjectFromZip as D,pruneMissingFiles as A,removeEmptyDirectories as z}from"../utils/project/backupUtils.js";import"../utils/common/zipUtils.js";import"../utils/buildJudge/restartJudgeUtils.js";function V(e,b){const f=e.split(/\r?\n/),g=b.split(/\r?\n/),u=f.length,s=g.length,h=Math.min(u,s);let r=0;for(let n=0;n<h;n++)f[n]!==g[n]&&(f[n]=g[n],r++);if(u>s)for(let n=u-1;n>=s;n--)f.splice(n,1),r++;if(s>u)for(let n=u;n<s;n++)f.push(g[n]),r++;const c=e.includes(`\r
1
+ import r from"path";import a from"fs";import F from"../appConfig/index.js";import{restartDevServer as A}from"../utils/build/restartDevUtils.js";import{log as n}from"../utils/log/logUtils.js";import{ValidationError as h,SystemError as v,ResourceError as S}from"../utils/error/errorHandler.js";import"../utils/common/sensitiveUtils.js";import{backupProjectToZip as C,restoreProjectFromZip as T,pruneMissingFiles as V,removeEmptyDirectories as z}from"../utils/project/backupUtils.js";import"../utils/common/zipUtils.js";import"../utils/buildJudge/restartJudgeUtils.js";function p(e,g){const s=e.split(/\r?\n/),E=g.split(/\r?\n/),R=s.length,u=E.length,o=Math.min(R,u);let f=0;for(let i=0;i<o;i++)s[i]!==E[i]&&(s[i]=E[i],f++);if(R>u)for(let i=R-1;i>=u;i--)s.splice(i,1),f++;if(u>R)for(let i=R;i<u;i++)s.push(E[i]),f++;const t=e.includes(`\r
2
2
  `)?`\r
3
3
  `:`
4
- `;return{finalContent:f.join(c),changesCount:r}}function I(e,b){return e===b?{finalContent:e,changesCount:0}:{finalContent:b,changesCount:-1}}async function T(e,b,f,g){if(!e)throw new m("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(b==null)throw new m("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});const u=Number(b);if(!Number.isFinite(u))throw new m("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});if(!Array.isArray(f))throw new m("files\u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});for(let r=0;r<f.length;r++){const c=f[r];if(!c||typeof c.operation!="string")throw new m(`files[${r}].operation \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${r}].operation`});if(!c.name||typeof c.name!="string")throw new m(`files[${r}].name \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${r}].name`});const i=c.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(i))throw new m(`files[${r}].operation \u5FC5\u987B\u662F create\u3001delete\u3001rename \u6216 modify \u4E4B\u4E00`,{field:`files[${r}].operation`});if(i==="rename"&&!c.renameFrom)throw new m(`files[${r}].renameFrom \u4E0D\u80FD\u4E3A\u7A7A\uFF08\u91CD\u547D\u540D\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${r}].renameFrom`});if(i==="modify"&&typeof c.contents!="string")throw new m(`files[${r}].contents \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\uFF08\u4FEE\u6539\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${r}].contents`})}const s=a.join(F.PROJECT_SOURCE_DIR,e);if(!o.existsSync(s))throw t(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:s}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});let h="";try{const r=a.join(F.UPLOAD_PROJECT_DIR,e);o.existsSync(r)||o.mkdirSync(r,{recursive:!0});const c=`${e}-v${u}.zip`;h=a.join(r,c),await C(e,s,h),t(e,"INFO","\u9879\u76EE\u5DF2\u5907\u4EFD",{projectId:e,zipPath:h});try{for(const i of f){const n=i.operation.toLowerCase(),w=i.name,l=a.normalize(w).replace(/^[\/\\]+/,""),R=a.join(s,l),O=a.resolve(R),y=a.resolve(s);if(!O.startsWith(y+a.sep)&&O!==y){t(e,"WARN","\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7",{filePath:l,resolvedPath:O});continue}switch(n){case"create":{await o.promises.mkdir(a.dirname(R),{recursive:!0});const P=i.contents||"";await o.promises.writeFile(R,P,"utf8"),t(e,"INFO","\u6587\u4EF6\u521B\u5EFA\u6210\u529F",{filePath:l});break}case"delete":{o.existsSync(R)?(await o.promises.unlink(R),t(e,"INFO","\u6587\u4EF6\u5220\u9664\u6210\u529F",{filePath:l})):t(e,"WARN","\u8981\u5220\u9664\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{filePath:l});break}case"rename":{const P=i.renameFrom;if(!P||typeof P!="string"){t(e,"WARN","\u91CD\u547D\u540D\u64CD\u4F5C\u7F3A\u5C11 renameFrom",{filePath:l});break}const E=a.normalize(P).replace(/^[\/\\]+/,""),k=a.join(s,E),N=a.resolve(k);if(!N.startsWith(y+a.sep)&&N!==y){t(e,"WARN","\u91CD\u547D\u540D\u6E90\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7",{renameFrom:E,resolvedPath:N});break}o.existsSync(k)?(await o.promises.mkdir(a.dirname(R),{recursive:!0}),await o.promises.rename(k,R),t(e,"INFO","\u6587\u4EF6\u91CD\u547D\u540D\u6210\u529F",{oldPath:E,newPath:l})):t(e,"WARN","\u8981\u91CD\u547D\u540D\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{renameFrom:E});break}case"modify":{if(!o.existsSync(R)){t(e,"WARN","\u8981\u4FEE\u6539\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{filePath:l});break}const P=await o.promises.readFile(R,"utf8"),E=typeof i.contents=="string"?i.contents:"",{finalContent:k,changesCount:N}=V(P,E);if(N===0){t(e,"INFO","\u6587\u4EF6\u5185\u5BB9\u65E0\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5199\u5165",{filePath:l});break}await o.promises.writeFile(R,k,"utf8"),t(e,"INFO","\u6587\u4EF6\u4FEE\u6539\u6210\u529F",{filePath:l,changesCount:N});break}default:{t(e,"WARN","\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7C7B\u578B",{operation:n,filePath:l});break}}}try{await z(s,F.TRAVERSE_EXCLUDE_DIRS||[])}catch(i){t(e,"WARN","\u6E05\u7406\u7A7A\u76EE\u5F55\u5931\u8D25",{projectId:e,error:i&&i.message})}return t(e,"INFO","\u90E8\u5206\u6587\u4EF6\u66F4\u65B0\u6210\u529F",{projectId:e,filesCount:f.length}),{success:!0,message:"\u90E8\u5206\u6587\u4EF6\u66F4\u65B0\u6210\u529F",projectId:e,filesCount:f.length}}catch(i){throw t(e,"ERROR","\u5904\u7406\u6587\u4EF6\u64CD\u4F5C\u5931\u8D25",{projectId:e,error:i&&i.message}),i}}catch(r){throw r.isOperational?r:new v("\u5907\u4EFD\u9879\u76EE\u5931\u8D25",{projectId:e,originalError:r&&r.message})}}async function p(e,b,f,g){if(!e)throw new m("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const u=Number(b);if(!Number.isFinite(u))throw new m("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});if(!Array.isArray(f))throw new m("files\u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});const s=a.join(F.PROJECT_SOURCE_DIR,e);if(!o.existsSync(s))throw t(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:s}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});let h="";try{const r=a.join(F.UPLOAD_PROJECT_DIR,e);o.existsSync(r)||o.mkdirSync(r,{recursive:!0});const c=`${e}-v${u}.zip`;h=a.join(r,c),await C(e,s,h);try{for(const i of f){if(!i||typeof i.name!="string")continue;const n=a.join(s,i.name);if(i.renameFrom&&typeof i.renameFrom=="string"){const P=a.join(s,i.renameFrom);if(o.existsSync(P)){await o.promises.mkdir(a.dirname(n),{recursive:!0}),await o.promises.rename(P,n),t(e,"INFO","\u6587\u4EF6\u91CD\u547D\u540D\u6210\u529F",{projectId:e,oldPath:i.renameFrom,newPath:i.name});continue}}const w=i.binary===!0,l=i.binary===!1,R=!!i.sizeExceeded,O=typeof i.contents=="string"&&i.contents.length>0;if(w){if(o.existsSync(n)){t(e,"INFO","\u4E8C\u8FDB\u5236\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5199\u5165",{filePath:i.name});continue}if(O)try{await o.promises.mkdir(a.dirname(n),{recursive:!0});const P=Buffer.from(i.contents,"base64");await o.promises.writeFile(n,P),t(e,"INFO","\u4E8C\u8FDB\u5236\u6587\u4EF6\u5199\u5165\u6210\u529F",{filePath:i.name})}catch(P){t(e,"ERROR","\u4E8C\u8FDB\u5236\u6587\u4EF6\u5199\u5165\u5931\u8D25",{filePath:i.name,error:P&&P.message})}else t(e,"WARN","\u4E8C\u8FDB\u5236\u6587\u4EF6\u4E0D\u5B58\u5728\u4E14\u65E0\u5185\u5BB9\uFF0C\u8DF3\u8FC7",{filePath:i.name});continue}l&&(!R||R&&O)&&(await o.promises.mkdir(a.dirname(n),{recursive:!0}),await o.promises.writeFile(n,i.contents||"","utf8"))}}catch(i){throw t(e,"ERROR","\u5199\u5165\u6587\u4EF6\u5931\u8D25",{projectId:e,error:i&&i.message}),i}try{const i=new Set(f.filter(n=>n&&typeof n.name=="string").map(n=>a.normalize(n.name)));await A(s,i,F.TRAVERSE_EXCLUDE_DIRS||[]),await z(s,F.TRAVERSE_EXCLUDE_DIRS||[])}catch(i){throw t(e,"ERROR","\u6E05\u7406\u7F3A\u5931\u6587\u4EF6\u5931\u8D25\uFF0C\u5F00\u59CB\u56DE\u6EDA",{projectId:e,error:i&&i.message}),i}return t(e,"INFO","\u6587\u4EF6\u63D0\u4EA4\u6210\u529F",{projectId:e,filesCount:f.length}),{success:!0,message:"\u6587\u4EF6\u63D0\u4EA4\u6210\u529F",projectId:e,restarted:!1}}catch(r){throw r.isOperational?r:new v("\u5907\u4EFD\u65E7\u7248\u672C\u5931\u8D25",{projectId:e,originalError:r&&r.message})}}async function x(e,b,f,g,u){if(!e)throw new m("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const s=Number(b);if(!Number.isFinite(s))throw new m("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});if(!f)throw new m("\u6587\u4EF6\u4E0D\u80FD\u4E3A\u7A7A",{field:"file"});if(!g||typeof g!="string")throw new m("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A",{field:"filePath"});const h=a.join(F.PROJECT_SOURCE_DIR,e);if(!o.existsSync(h))throw t(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:h}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const r=a.normalize(g).replace(/^[\/\\]+/,""),c=a.join(h,r),i=a.resolve(c),n=a.resolve(h);if(!i.startsWith(n))throw new m("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u4E0D\u80FD\u8D85\u51FA\u9879\u76EE\u76EE\u5F55",{field:"filePath",providedPath:g,resolvedPath:i});let w="";try{const l=a.join(F.UPLOAD_PROJECT_DIR,e);o.existsSync(l)||o.mkdirSync(l,{recursive:!0});const R=`${e}-v${s}.zip`;w=a.join(l,R),await C(e,h,w),t(e,"INFO",`\u9879\u76EE\u5DF2\u5907\u4EFD: ${w}`,{projectId:e,zipPath:w});try{if(await o.promises.mkdir(a.dirname(c),{recursive:!0}),!f.buffer)throw new m("\u6587\u4EF6\u5185\u5BB9\u683C\u5F0F\u4E0D\u6B63\u786E\uFF0C\u7F3A\u5C11buffer",{field:"file"});if(t(e,"INFO","\u51C6\u5907\u5199\u5165\u6587\u4EF6",{targetPath:c,bufferLength:f.buffer.length,expectedSize:f.size,bufferIsBuffer:Buffer.isBuffer(f.buffer),sizeMatch:f.buffer.length===f.size}),await o.promises.writeFile(c,f.buffer),t(e,"INFO","\u6587\u4EF6\u4E0A\u4F20\u6210\u529F",{projectId:e,filePath:r,targetPath:i,fileSize:f.buffer?f.buffer.length:0}),!1)try{const y=await $(u,e);return t(e,"INFO","\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668\u6210\u529F",{projectId:e,pid:y.pid,port:y.port}),{success:!0,message:"\u6587\u4EF6\u4E0A\u4F20\u5E76\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668\u6210\u529F",projectId:e,filePath:r,targetPath:i,fileSize:f.buffer?f.buffer.length:0,pid:y.pid,port:y.port,restarted:!0}}catch(y){t(e,"ERROR","\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668\u5931\u8D25",{projectId:e,filePath:r,error:y&&y.message})}else return t(e,"INFO","\u6587\u4EF6\u4FEE\u6539\u4E0D\u9700\u8981\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668",{projectId:e,filePath:r}),{success:!0,message:"\u6587\u4EF6\u4E0A\u4F20\u6210\u529F\uFF0C\u65E0\u9700\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668",projectId:e,restarted:!1}}catch(O){throw t(e,"ERROR","\u5199\u5165\u6587\u4EF6\u5931\u8D25",{projectId:e,filePath:r,error:O&&O.message}),O}}catch(l){throw l.isOperational?l:new v("\u5907\u4EFD\u9879\u76EE\u5931\u8D25",{projectId:e,filePath:r,originalError:l&&l.message})}}async function _(e,b,f,g){if(!e)throw new m("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const u=Number(b);if(!Number.isFinite(u))throw new m("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const s=Number(f);if(!Number.isFinite(s))throw new m("rollbackTo\u5FC5\u987B\u662F\u6570\u5B57",{field:"rollbackTo"});if(s<0)throw new m("rollbackTo\u4E0D\u80FD\u5C0F\u4E8E0",{field:"rollbackTo"});if(s>=u)throw new m("rollbackTo\u5FC5\u987B\u5C0F\u4E8E\u5F53\u524DcodeVersion",{field:"rollbackTo"});const h=a.join(F.PROJECT_SOURCE_DIR,e);if(!o.existsSync(h))throw t(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:h}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const r=a.join(F.UPLOAD_PROJECT_DIR,e),c=`${e}-v${s}.zip`,i=a.join(r,c);if(!o.existsSync(i))throw t(e,"ERROR","\u56DE\u6EDA\u7248\u672C\u5907\u4EFD\u6587\u4EF6\u4E0D\u5B58\u5728",{projectId:e,rollbackTo:s,zipPath:i}),new S("\u56DE\u6EDA\u7248\u672C\u5907\u4EFD\u6587\u4EF6\u4E0D\u5B58\u5728",{projectId:e,rollbackTo:s});let n="";try{o.existsSync(r)||o.mkdirSync(r,{recursive:!0});const w=`${e}-v${u}.zip`;return n=a.join(r,w),o.existsSync(n)?t(e,"INFO","\u5F53\u524D\u7248\u672C\u5907\u4EFD\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5907\u4EFD",{projectId:e,zipPath:n}):(await C(e,h,n),t(e,"INFO","\u5F53\u524D\u7248\u672C\u5DF2\u5907\u4EFD",{projectId:e,zipPath:n})),await D(e,h,i),t(e,"INFO","\u9879\u76EE\u56DE\u6EDA\u6210\u529F",{projectId:e,newVersion:u,toVersion:s,rollbackZipPath:i}),{success:!0,message:"\u9879\u76EE\u56DE\u6EDA\u6210\u529F",newVersion:u,rollbackTo:s}}catch(w){if(t(e,"ERROR","\u56DE\u6EDA\u9879\u76EE\u5931\u8D25",{projectId:e,rollbackTo:s,error:w&&w.message}),n&&o.existsSync(n))try{t(e,"INFO","\u56DE\u6EDA\u5931\u8D25\uFF0C\u5C1D\u8BD5\u6062\u590D\u5F53\u524D\u7248\u672C",{projectId:e,backupPath:n}),await D(e,h,n),t(e,"INFO","\u5DF2\u6062\u590D\u5F53\u524D\u7248\u672C",{projectId:e})}catch(l){t(e,"ERROR","\u6062\u590D\u5F53\u524D\u7248\u672C\u5931\u8D25",{projectId:e,error:l&&l.message})}throw w.isOperational?w:new v("\u56DE\u6EDA\u9879\u76EE\u5931\u8D25",{projectId:e,rollbackTo:s,originalError:w&&w.message})}}export{T as specifiedFilesUpdate,p as allFilesUpdate,x as uploadSingleFile,_ as rollbackVersion};export default{specifiedFilesUpdate:T,allFilesUpdate:p,uploadSingleFile:x,rollbackVersion:_};
4
+ `;return{finalContent:s.join(t),changesCount:f}}function j(e,g){return e===g?{finalContent:e,changesCount:0}:{finalContent:g,changesCount:-1}}async function x(e,g,s,E){const R=Date.now();if(!e)throw new h("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(g==null)throw new h("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});const u=Number(g);if(!Number.isFinite(u))throw new h("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});if(!Array.isArray(s))throw new h("files\u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});for(let t=0;t<s.length;t++){const w=s[t];if(!w||typeof w.operation!="string")throw new h(`files[${t}].operation \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${t}].operation`});if(!w.name||typeof w.name!="string")throw new h(`files[${t}].name \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${t}].name`});const i=w.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(i))throw new h(`files[${t}].operation \u5FC5\u987B\u662F create\u3001delete\u3001rename \u6216 modify \u4E4B\u4E00`,{field:`files[${t}].operation`});if(i==="rename"&&!w.renameFrom)throw new h(`files[${t}].renameFrom \u4E0D\u80FD\u4E3A\u7A7A\uFF08\u91CD\u547D\u540D\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${t}].renameFrom`});if(i==="modify"&&typeof w.contents!="string")throw new h(`files[${t}].contents \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\uFF08\u4FEE\u6539\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${t}].contents`})}const o=r.join(F.PROJECT_SOURCE_DIR,e);if(!a.existsSync(o))throw n(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:o}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});let f="";try{const t=r.join(F.UPLOAD_PROJECT_DIR,e);a.existsSync(t)||a.mkdirSync(t,{recursive:!0});const w=`${e}-v${u}.zip`;f=r.join(t,w),n(e,"DEBUG","\u5F00\u59CB\u5907\u4EFD\u9879\u76EE",{projectId:e,backupZipPath:f}),await C(e,o,f),n(e,"INFO","\u9879\u76EE\u5DF2\u5907\u4EFD",{projectId:e,zipPath:f});try{n(e,"DEBUG","\u5F00\u59CB\u5904\u7406\u6587\u4EF6\u64CD\u4F5C",{projectId:e,filesCount:s.length});for(const i of s){const l=i.operation.toLowerCase(),c=i.name,m=r.normalize(c).replace(/^[\/\\]+/,""),P=r.join(o,m),O=r.resolve(P),y=r.resolve(o);if(!O.startsWith(y+r.sep)&&O!==y){n(e,"WARN","\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7",{filePath:m,resolvedPath:O});continue}switch(l){case"create":{await a.promises.mkdir(r.dirname(P),{recursive:!0});const b=i.contents||"";await a.promises.writeFile(P,b,"utf8"),n(e,"INFO","\u6587\u4EF6\u521B\u5EFA\u6210\u529F",{filePath:m});break}case"delete":{a.existsSync(P)?(await a.promises.unlink(P),n(e,"INFO","\u6587\u4EF6\u5220\u9664\u6210\u529F",{filePath:m})):n(e,"WARN","\u8981\u5220\u9664\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{filePath:m});break}case"rename":{const b=i.renameFrom;if(!b||typeof b!="string"){n(e,"WARN","\u91CD\u547D\u540D\u64CD\u4F5C\u7F3A\u5C11 renameFrom",{filePath:m});break}const D=r.normalize(b).replace(/^[\/\\]+/,""),k=r.join(o,D),N=r.resolve(k);if(!N.startsWith(y+r.sep)&&N!==y){n(e,"WARN","\u91CD\u547D\u540D\u6E90\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7",{renameFrom:D,resolvedPath:N});break}a.existsSync(k)?(await a.promises.mkdir(r.dirname(P),{recursive:!0}),await a.promises.rename(k,P),n(e,"INFO","\u6587\u4EF6\u91CD\u547D\u540D\u6210\u529F",{oldPath:D,newPath:m})):n(e,"WARN","\u8981\u91CD\u547D\u540D\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{renameFrom:D});break}case"modify":{if(!a.existsSync(P)){n(e,"WARN","\u8981\u4FEE\u6539\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{filePath:m});break}const b=await a.promises.readFile(P,"utf8"),D=typeof i.contents=="string"?i.contents:"",{finalContent:k,changesCount:N}=p(b,D);if(N===0){n(e,"INFO","\u6587\u4EF6\u5185\u5BB9\u65E0\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5199\u5165",{filePath:m});break}await a.promises.writeFile(P,k,"utf8"),n(e,"INFO","\u6587\u4EF6\u4FEE\u6539\u6210\u529F",{filePath:m,changesCount:N});break}default:{n(e,"WARN","\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7C7B\u578B",{operation:l,filePath:m});break}}}n(e,"DEBUG","\u5F00\u59CB\u6E05\u7406\u7A7A\u76EE\u5F55",{projectId:e});try{await z(o,F.TRAVERSE_EXCLUDE_DIRS||[])}catch(i){n(e,"WARN","\u6E05\u7406\u7A7A\u76EE\u5F55\u5931\u8D25",{projectId:e,error:i&&i.message})}return n(e,"INFO","\u90E8\u5206\u6587\u4EF6\u66F4\u65B0\u6210\u529F",{projectId:e,filesCount:s.length,elapsedMs:Date.now()-R}),{success:!0,message:"\u90E8\u5206\u6587\u4EF6\u66F4\u65B0\u6210\u529F",projectId:e,filesCount:s.length}}catch(i){throw n(e,"ERROR","\u5904\u7406\u6587\u4EF6\u64CD\u4F5C\u5931\u8D25",{projectId:e,error:i&&i.message,elapsedMs:Date.now()-R}),i}}catch(t){throw t.isOperational?t:new v("\u5907\u4EFD\u9879\u76EE\u5931\u8D25",{projectId:e,originalError:t&&t.message})}}async function U(e,g,s,E){const R=Date.now();if(!e)throw new h("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const u=Number(g);if(!Number.isFinite(u))throw new h("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});if(!Array.isArray(s))throw new h("files\u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});const o=r.join(F.PROJECT_SOURCE_DIR,e);if(!a.existsSync(o))throw n(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:o}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});let f="";try{const t=r.join(F.UPLOAD_PROJECT_DIR,e);a.existsSync(t)||a.mkdirSync(t,{recursive:!0});const w=`${e}-v${u}.zip`;f=r.join(t,w),n(e,"DEBUG","\u5F00\u59CB\u5907\u4EFD\u9879\u76EE",{projectId:e,backupZipPath:f}),await C(e,o,f);try{n(e,"DEBUG","\u5F00\u59CB\u5199\u5165\u6587\u4EF6",{projectId:e,filesCount:s.length});for(const i of s){if(!i||typeof i.name!="string")continue;const l=r.join(o,i.name);if(i.renameFrom&&typeof i.renameFrom=="string"){const b=r.join(o,i.renameFrom);if(a.existsSync(b)){await a.promises.mkdir(r.dirname(l),{recursive:!0}),await a.promises.rename(b,l),n(e,"INFO","\u6587\u4EF6\u91CD\u547D\u540D\u6210\u529F",{projectId:e,oldPath:i.renameFrom,newPath:i.name});continue}}const c=i.binary===!0,m=i.binary===!1,P=!!i.sizeExceeded,O=typeof i.contents=="string"&&i.contents.length>0;if(c){if(a.existsSync(l)){n(e,"INFO","\u4E8C\u8FDB\u5236\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5199\u5165",{filePath:i.name});continue}if(O)try{await a.promises.mkdir(r.dirname(l),{recursive:!0});const b=Buffer.from(i.contents,"base64");await a.promises.writeFile(l,b),n(e,"INFO","\u4E8C\u8FDB\u5236\u6587\u4EF6\u5199\u5165\u6210\u529F",{filePath:i.name})}catch(b){n(e,"ERROR","\u4E8C\u8FDB\u5236\u6587\u4EF6\u5199\u5165\u5931\u8D25",{filePath:i.name,error:b&&b.message})}else n(e,"WARN","\u4E8C\u8FDB\u5236\u6587\u4EF6\u4E0D\u5B58\u5728\u4E14\u65E0\u5185\u5BB9\uFF0C\u8DF3\u8FC7",{filePath:i.name});continue}m&&(!P||P&&O)&&(await a.promises.mkdir(r.dirname(l),{recursive:!0}),await a.promises.writeFile(l,i.contents||"","utf8"))}}catch(i){throw n(e,"ERROR","\u5199\u5165\u6587\u4EF6\u5931\u8D25",{projectId:e,error:i&&i.message,elapsedMs:Date.now()-R}),i}try{n(e,"DEBUG","\u5F00\u59CB\u6E05\u7406\u7F3A\u5931\u6587\u4EF6\u4E0E\u7A7A\u76EE\u5F55",{projectId:e});const i=new Set(s.filter(l=>l&&typeof l.name=="string").map(l=>r.normalize(l.name)));await V(o,i,F.TRAVERSE_EXCLUDE_DIRS||[]),await z(o,F.TRAVERSE_EXCLUDE_DIRS||[])}catch(i){throw n(e,"ERROR","\u6E05\u7406\u7F3A\u5931\u6587\u4EF6\u5931\u8D25\uFF0C\u5F00\u59CB\u56DE\u6EDA",{projectId:e,error:i&&i.message,elapsedMs:Date.now()-R}),i}return n(e,"INFO","\u6587\u4EF6\u63D0\u4EA4\u6210\u529F",{projectId:e,filesCount:s.length,elapsedMs:Date.now()-R}),{success:!0,message:"\u6587\u4EF6\u63D0\u4EA4\u6210\u529F",projectId:e,restarted:!1}}catch(t){throw t.isOperational?t:new v("\u5907\u4EFD\u65E7\u7248\u672C\u5931\u8D25",{projectId:e,originalError:t&&t.message})}}async function _(e,g,s,E,R){const u=Date.now();if(!e)throw new h("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const o=Number(g);if(!Number.isFinite(o))throw new h("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});if(!s)throw new h("\u6587\u4EF6\u4E0D\u80FD\u4E3A\u7A7A",{field:"file"});if(!E||typeof E!="string")throw new h("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A",{field:"filePath"});const f=r.join(F.PROJECT_SOURCE_DIR,e);if(!a.existsSync(f))throw n(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:f}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const t=r.normalize(E).replace(/^[\/\\]+/,""),w=r.join(f,t),i=r.resolve(w),l=r.resolve(f);if(!i.startsWith(l))throw new h("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u4E0D\u80FD\u8D85\u51FA\u9879\u76EE\u76EE\u5F55",{field:"filePath",providedPath:E,resolvedPath:i});let c="";try{const m=r.join(F.UPLOAD_PROJECT_DIR,e);a.existsSync(m)||a.mkdirSync(m,{recursive:!0});const P=`${e}-v${o}.zip`;c=r.join(m,P),n(e,"DEBUG","\u5F00\u59CB\u5907\u4EFD\u9879\u76EE",{projectId:e,backupZipPath:c}),await C(e,f,c),n(e,"INFO",`\u9879\u76EE\u5DF2\u5907\u4EFD: ${c}`,{projectId:e,zipPath:c});try{if(n(e,"DEBUG","\u5F00\u59CB\u5199\u5165\u4E0A\u4F20\u6587\u4EF6",{projectId:e,filePath:t}),await a.promises.mkdir(r.dirname(w),{recursive:!0}),!s.buffer)throw new h("\u6587\u4EF6\u5185\u5BB9\u683C\u5F0F\u4E0D\u6B63\u786E\uFF0C\u7F3A\u5C11buffer",{field:"file"});if(n(e,"INFO","\u51C6\u5907\u5199\u5165\u6587\u4EF6",{targetPath:w,bufferLength:s.buffer.length,expectedSize:s.size,bufferIsBuffer:Buffer.isBuffer(s.buffer),sizeMatch:s.buffer.length===s.size}),await a.promises.writeFile(w,s.buffer),n(e,"INFO","\u6587\u4EF6\u4E0A\u4F20\u6210\u529F",{projectId:e,filePath:t,targetPath:i,fileSize:s.buffer?s.buffer.length:0,elapsedMs:Date.now()-u}),!1)try{const y=await A(R,e);return n(e,"INFO","\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668\u6210\u529F",{projectId:e,pid:y.pid,port:y.port}),{success:!0,message:"\u6587\u4EF6\u4E0A\u4F20\u5E76\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668\u6210\u529F",projectId:e,filePath:t,targetPath:i,fileSize:s.buffer?s.buffer.length:0,pid:y.pid,port:y.port,restarted:!0}}catch(y){n(e,"ERROR","\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668\u5931\u8D25",{projectId:e,filePath:t,error:y&&y.message})}else return n(e,"INFO","\u6587\u4EF6\u4FEE\u6539\u4E0D\u9700\u8981\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668",{projectId:e,filePath:t}),{success:!0,message:"\u6587\u4EF6\u4E0A\u4F20\u6210\u529F\uFF0C\u65E0\u9700\u91CD\u542F\u5F00\u53D1\u670D\u52A1\u5668",projectId:e,restarted:!1}}catch(O){throw n(e,"ERROR","\u5199\u5165\u6587\u4EF6\u5931\u8D25",{projectId:e,filePath:t,error:O&&O.message,elapsedMs:Date.now()-u}),O}}catch(m){throw m.isOperational?m:new v("\u5907\u4EFD\u9879\u76EE\u5931\u8D25",{projectId:e,filePath:t,originalError:m&&m.message})}}async function $(e,g,s,E){const R=Date.now();if(!e)throw new h("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const u=Number(g);if(!Number.isFinite(u))throw new h("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const o=Number(s);if(!Number.isFinite(o))throw new h("rollbackTo\u5FC5\u987B\u662F\u6570\u5B57",{field:"rollbackTo"});if(o<0)throw new h("rollbackTo\u4E0D\u80FD\u5C0F\u4E8E0",{field:"rollbackTo"});if(o>=u)throw new h("rollbackTo\u5FC5\u987B\u5C0F\u4E8E\u5F53\u524DcodeVersion",{field:"rollbackTo"});const f=r.join(F.PROJECT_SOURCE_DIR,e);if(!a.existsSync(f))throw n(e,"ERROR","\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e,projectPath:f}),new S("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const t=r.join(F.UPLOAD_PROJECT_DIR,e),w=`${e}-v${o}.zip`,i=r.join(t,w);if(!a.existsSync(i))throw n(e,"ERROR","\u56DE\u6EDA\u7248\u672C\u5907\u4EFD\u6587\u4EF6\u4E0D\u5B58\u5728",{projectId:e,rollbackTo:o,zipPath:i}),new S("\u56DE\u6EDA\u7248\u672C\u5907\u4EFD\u6587\u4EF6\u4E0D\u5B58\u5728",{projectId:e,rollbackTo:o});let l="";try{a.existsSync(t)||a.mkdirSync(t,{recursive:!0});const c=`${e}-v${u}.zip`;return l=r.join(t,c),a.existsSync(l)?n(e,"INFO","\u5F53\u524D\u7248\u672C\u5907\u4EFD\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5907\u4EFD",{projectId:e,zipPath:l}):(await C(e,f,l),n(e,"INFO","\u5F53\u524D\u7248\u672C\u5DF2\u5907\u4EFD",{projectId:e,zipPath:l})),n(e,"DEBUG","\u5F00\u59CB\u4ECE\u5907\u4EFD\u6062\u590D\u9879\u76EE",{projectId:e,rollbackToNum:o,rollbackZipPath:i}),await T(e,f,i),n(e,"INFO","\u9879\u76EE\u56DE\u6EDA\u6210\u529F",{projectId:e,newVersion:u,toVersion:o,rollbackZipPath:i,elapsedMs:Date.now()-R}),{success:!0,message:"\u9879\u76EE\u56DE\u6EDA\u6210\u529F",newVersion:u,rollbackTo:o}}catch(c){if(n(e,"ERROR","\u56DE\u6EDA\u9879\u76EE\u5931\u8D25",{projectId:e,rollbackTo:o,error:c&&c.message,elapsedMs:Date.now()-R}),l&&a.existsSync(l))try{n(e,"INFO","\u56DE\u6EDA\u5931\u8D25\uFF0C\u5C1D\u8BD5\u6062\u590D\u5F53\u524D\u7248\u672C",{projectId:e,backupPath:l}),await T(e,f,l),n(e,"INFO","\u5DF2\u6062\u590D\u5F53\u524D\u7248\u672C",{projectId:e})}catch(m){n(e,"ERROR","\u6062\u590D\u5F53\u524D\u7248\u672C\u5931\u8D25",{projectId:e,error:m&&m.message})}throw c.isOperational?c:new v("\u56DE\u6EDA\u9879\u76EE\u5931\u8D25",{projectId:e,rollbackTo:o,originalError:c&&c.message})}}export{x as specifiedFilesUpdate,U as allFilesUpdate,_ as uploadSingleFile,$ as rollbackVersion};export default{specifiedFilesUpdate:x,allFilesUpdate:U,uploadSingleFile:_,rollbackVersion:$};
@@ -1 +1 @@
1
- import{log as i}from"../utils/log/logUtils.js";import h from"../appConfig/index.js";import m from"path";import n from"fs";import{extractZip as D}from"../utils/common/zipUtils.js";import"../utils/build/startDevUtils.js";import"../utils/build/restartDevUtils.js";import{stopDevServer as P}from"../utils/build/stopDevUtils.js";import{ValidationError as O,BusinessError as J,SystemError as E,FileError as A,ResourceError as N}from"../utils/error/errorHandler.js";import{sanitizeSensitivePaths as F}from"../utils/common/sensitiveUtils.js";import{removeNodeModules as z}from"../utils/buildDependency/dependencyManager.js";import{backupProjectToZip as U,copyDirectoryFiltered as L}from"../utils/project/backupUtils.js";import{createPnpmNpmrc as S}from"../utils/common/npmrcUtils.js";async function _(e){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const r=h.PROJECT_SOURCE_DIR,s=m.join(r,e);if(n.existsSync(s))throw new J(`\u9879\u76EE\u76EE\u5F55 ${e} \u5DF2\u5B58\u5728`,{projectId:e,projectPath:s});try{n.mkdirSync(s,{recursive:!0}),i(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u521B\u5EFA\u6210\u529F: ${s}`,{projectId:e});const t=h.INIT_PROJECT_DIR,a=m.join(t,`${h.INIT_PROJECT_NAME}.zip`),u=m.join(t,h.INIT_PROJECT_NAME);if(!n.existsSync(u)){if(!n.existsSync(a))throw i(e,"ERROR",`\u521D\u59CB\u5316\u6A21\u677F\u4E0D\u5B58\u5728: ${a}`,{projectId:e,templateZipPath:a}),new N("\u521D\u59CB\u5316\u6A21\u677F\u4E0D\u5B58\u5728",{});if(i(e,"INFO",`\u6A21\u677F\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u5F00\u59CB\u89E3\u538B\u6A21\u677F: ${a}`,{projectId:e,templateZipPath:a}),await D(a,u),i(e,"INFO","\u6A21\u677F\u89E3\u538B\u5B8C\u6210",{projectId:e}),!n.existsSync(u))throw new E("\u6A21\u677F\u89E3\u538B\u540E\u76EE\u5F55\u4ECD\u4E0D\u5B58\u5728",{})}const w=await n.promises.readdir(u,{withFileTypes:!0});for(const f of w){const o=m.join(u,f.name),l=m.join(s,f.name);f.isDirectory()?(await n.promises.mkdir(l,{recursive:!0}),await L(o,l)):f.isFile()&&(await n.promises.mkdir(m.dirname(l),{recursive:!0}),await n.promises.copyFile(o,l))}return i(e,"INFO",`\u9879\u76EE ${e} \u521D\u59CB\u5316\u6210\u529F`,{projectId:e}),await S(s,e),{success:!0,message:`\u9879\u76EE ${e} \u521B\u5EFA\u6210\u529F`,projectPath:s}}catch(t){throw i(e,"ERROR",`\u9879\u76EE ${e} \u521D\u59CB\u5316\u5931\u8D25: ${t.message}`,{projectId:e}),new E(`\u9879\u76EE ${e} \u521D\u59CB\u5316\u5931\u8D25: ${t.message}`,{projectId:e,projectPath:s,originalError:t.message})}}async function W(e){const r=await n.promises.readdir(e,{withFileTypes:!0}),s=h.TOP_LEVEL_NOISE_PATTERNS,t=r.filter(a=>{const u=a.name;return u.startsWith(".")?!1:!s.some(w=>w.endsWith("*")?u.startsWith(w.slice(0,-1)):u===w)});if(t.length===1&&t[0].isDirectory()){const a=m.join(e,t[0].name),u=m.join(e,"..",`temp_${Date.now()}`);await n.promises.rename(a,u);const w=await n.promises.readdir(u);for(const f of w){const o=m.join(u,f),l=m.join(e,f);await n.promises.rename(o,l)}await n.promises.rmdir(u)}}async function $(e){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const r=m.join(h.PROJECT_SOURCE_DIR,e);if(n.existsSync(r))try{i(e,"INFO",`\u5F00\u59CB\u6E05\u7406\u9879\u76EE\u76EE\u5F55: ${r}`,{projectId:e}),await n.promises.rm(r,{recursive:!0,force:!0}),i(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u6E05\u7406\u5B8C\u6210: ${r}`,{projectId:e})}catch(s){throw i(e,"ERROR",`\u6E05\u7406\u9879\u76EE\u76EE\u5F55\u5931\u8D25: ${s.message}`,{projectId:e,projectPath:r,originalError:s.message}),new E(`\u6E05\u7406\u9879\u76EE\u76EE\u5F55\u5931\u8D25: ${s.message}`,{projectId:e,projectPath:r,originalError:s.message})}else i(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u65E0\u9700\u6E05\u7406: ${r}`,{projectId:e})}function Z(e){if(!n.existsSync(e))return!0;try{return n.readdirSync(e).filter(t=>!t.startsWith(".")&&t!=="node_modules").length===0}catch(r){const s=m.basename(e);return i(s,"ERROR",`\u68C0\u67E5\u76EE\u5F55\u662F\u5426\u4E3A\u7A7A\u5931\u8D25: ${r.message}`,{dirPath:e}),!0}}async function v(e,r,s,t,a,u){const w=h.PROJECT_SOURCE_DIR,f=m.join(w,e);try{if(Z(f))i(e,"INFO","\u9879\u76EE\u76EE\u5F55\u4E3A\u7A7A\uFF0C\u76F4\u63A5\u90E8\u7F72\u65B0\u9879\u76EE",{projectId:e});else{i(e,"INFO","\u9879\u76EE\u76EE\u5F55\u975E\u7A7A\uFF0C\u5F00\u59CB\u5907\u4EFD\u5F53\u524D\u7248\u672C",{projectId:e});const l=parseInt(t)-1,y=m.join(h.UPLOAD_PROJECT_DIR,e),c=m.join(y,`${e}-v${l}.zip`);if(n.existsSync(c))i(e,"INFO",`\u5907\u4EFD\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5907\u4EFD: ${c}`,{projectId:e});else try{await g(e,l),i(e,"INFO",`\u5F53\u524D\u7248\u672C\u5DF2\u5907\u4EFD: ${c}`,{projectId:e})}catch(R){throw i(e,"ERROR",`\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25: ${R.message}`,{projectId:e}),new E(`\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25: ${R.message}`,{projectId:e,originalError:R.message})}if(a&&!isNaN(Number(a))){const R=Number(a);i(e,"INFO",`\u6B63\u5728\u505C\u6B62\u65E7\u7248\u672Cdev\u670D\u52A1\u5668\uFF0CPID: ${R}`,{projectId:e});try{await P(s,e,R,{strict:!0}),i(e,"INFO","\u65E7\u7248\u672Cdev\u670D\u52A1\u5668\u5DF2\u505C\u6B62",{projectId:e})}catch(k){i(e,"WARN",`\u505C\u6B62\u65E7\u7248\u672Cdev\u670D\u52A1\u5668\u5931\u8D25: ${k.message}`,{projectId:e,pid:R})}}n.existsSync(f)&&(i(e,"INFO",`\u6B63\u5728\u6E05\u7A7A\u9879\u76EE\u76EE\u5F55: ${f}`,{projectId:e}),await n.promises.rm(f,{recursive:!0,force:!0}))}return n.mkdirSync(f,{recursive:!0}),i(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u521B\u5EFA\u6210\u529F: ${f}`,{projectId:e}),i(e,"INFO","\u5F00\u59CB\u89E3\u538B\u538B\u7F29\u5305",{projectId:e}),await D(r,f),i(e,"INFO","\u538B\u7F29\u5305\u89E3\u538B\u5B8C\u6210",{projectId:e}),i(e,"INFO","\u68C0\u67E5\u5E76\u5904\u7406\u9876\u5C42\u6587\u4EF6\u5939",{projectId:e}),await W(f),i(e,"INFO","\u68C0\u67E5\u5E76\u5220\u9664 node_modules \u6587\u4EF6\u5939",{projectId:e}),await z(f),await S(f,e),{success:!0,message:`\u9879\u76EE ${e} \u4E0A\u4F20\u6210\u529F`,projectId:e,codeVersion:t}}catch(o){i(e,"ERROR",`\u4E0A\u4F20\u9879\u76EE\u5931\u8D25: ${o.message}`,{projectId:e});try{await $(e),i(e,"INFO","\u4E0A\u4F20\u5931\u8D25\uFF0C\u9879\u76EE\u76EE\u5F55\u5DF2\u6E05\u7406",{projectId:e})}catch(l){i(e,"ERROR",`\u6E05\u7406\u9879\u76EE\u76EE\u5F55\u5931\u8D25: ${l.message}`,{projectId:e,originalError:l.message})}throw o.isOperational?o:new E(`\u4E0A\u4F20\u9879\u76EE\u5931\u8D25: ${o.message}`,{projectId:e,projectPath:f,zipFilePath:r,originalError:o.message})}}async function g(e,r){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(r==null)throw new O("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});const s=Number(r);if(!Number.isFinite(s))throw new O("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const t=m.join(h.PROJECT_SOURCE_DIR,e);if(!n.existsSync(t))throw new N("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const a=m.join(h.UPLOAD_PROJECT_DIR,e);n.existsSync(a)||n.mkdirSync(a,{recursive:!0});const u=`${e}-v${s}.zip`,w=m.join(a,u);return await U(e,t,w)}async function T(e,r,s){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(!r)throw new O("\u4EE3\u7801\u7248\u672C\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});if(!s)throw new O("\u8BF7\u4E0A\u4F20\u538B\u7F29\u5305\u6587\u4EF6",{field:"zipFile"});const t=m.join(h.UPLOAD_PROJECT_DIR,e);n.existsSync(t)||n.mkdirSync(t,{recursive:!0});const a=s.path,u=m.join(t,`${e}-v${r}.zip`);try{return n.renameSync(a,u),i(e,"INFO","\u6587\u4EF6\u4FDD\u5B58\u6210\u529F",{projectId:e,codeVersion:r,filePath:u}),{success:!0,filePath:u}}catch(w){if(i(e,"ERROR","\u79FB\u52A8\u6587\u4EF6\u5931\u8D25",{projectId:e,codeVersion:r,error:w.message}),n.existsSync(a))try{n.unlinkSync(a)}catch(f){i(e,"ERROR","\u6E05\u7406\u4E34\u65F6\u6587\u4EF6\u5931\u8D25",{projectId:e,error:f.message})}throw new E("\u6587\u4EF6\u4FDD\u5B58\u5931\u8D25",{projectId:e,codeVersion:r,originalError:w.message})}}async function C(e,r,s){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const t=null;try{if(r&&!isNaN(Number(r))){const o=Number(r);i(t,"INFO",`[delete-project] \u6B63\u5728\u505C\u6B62\u5F00\u53D1\u670D\u52A1\u5668\uFF0CPID: ${o}`,{projectId:e,pid:o});try{await P(s,e,o,{strict:!0}),i(t,"INFO","[delete-project] \u5F00\u53D1\u670D\u52A1\u5668\u5DF2\u505C\u6B62",{projectId:e})}catch(l){i(t,"WARN",`[delete-project] \u505C\u6B62\u5F00\u53D1\u670D\u52A1\u5668\u5931\u8D25: ${l.message}`,{projectId:e,pid:o})}}const a=[m.join(h.UPLOAD_PROJECT_DIR,e),m.join(h.PROJECT_SOURCE_DIR,e),m.join(h.DIST_TARGET_DIR,e),m.join(h.LOG_BASE_DIR,e)],u=[],w=[];for(const o of a)if(n.existsSync(o))try{await n.promises.rm(o,{recursive:!0,force:!0}),u.push(o),i(t,"INFO",`[delete-project] \u76EE\u5F55\u5220\u9664\u6210\u529F: ${o}`,{projectId:e})}catch(l){w.push({path:o,error:l.message}),i(t,"ERROR",`[delete-project] \u76EE\u5F55\u5220\u9664\u5931\u8D25: ${o}`,{projectId:e,error:l.message})}else i(t,"INFO",`[delete-project] \u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5220\u9664: ${o}`,{projectId:e});const f={success:!0,message:`\u9879\u76EE ${e} \u5220\u9664\u5B8C\u6210`,projectId:e,deletedDirectories:u,failedDirectories:w};return w.length>0&&(f.message+=`\uFF0C\u4F46\u6709 ${w.length} \u4E2A\u76EE\u5F55\u5220\u9664\u5931\u8D25`,i(t,"WARN","[delete-project] \u90E8\u5206\u76EE\u5F55\u5220\u9664\u5931\u8D25",{projectId:e,failedDirs:w})),i(t,"INFO",`[delete-project] \u9879\u76EE\u5220\u9664\u5B8C\u6210: ${e}`,{projectId:e}),f}catch(a){throw i(t,"ERROR",`[delete-project] \u5220\u9664\u9879\u76EE\u5931\u8D25: ${a.message}`,{projectId:e,originalError:a.message}),a.isOperational?a:new E(`\u5220\u9664\u9879\u76EE\u5931\u8D25: ${a.message}`,{projectId:e,originalError:a.message})}}async function x(e,r,s,t){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(r==null)throw new O("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});const a=Number(r);if(!Number.isFinite(a))throw new O("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const u=m.join(h.PROJECT_SOURCE_DIR,e);if(!n.existsSync(u))throw new N("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const w=m.join(h.UPLOAD_PROJECT_DIR,e),f=`${e}-v${a}.zip`,o=m.join(w,f);if(s!=="LATEST"){if(n.existsSync(o))return i(e,"INFO",`\u4F7F\u7528\u5DF2\u5B58\u5728\u7684\u5BFC\u51FA\u6587\u4EF6: ${o}`,{projectId:e,zipPath:o}),{success:!0,projectId:e,zipPath:o};throw new N(`\u6307\u5B9A\u7248\u672C\u7684zip\u5305\u4E0D\u5B58\u5728: ${o}`,{projectId:e,zipPath:o})}const l=m.join(u,"cpage_config.json");let y=!1;try{if(t)try{const R=JSON.stringify(t,null,2);await n.promises.writeFile(l,R,"utf8"),y=!0,i(e,"INFO",`\u5DF2\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6: ${l}`,{projectId:e,configFilePath:l})}catch(R){throw i(e,"ERROR",`\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6\u5931\u8D25: ${R.message}`,{projectId:e,error:R.message}),new A("\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6\u5931\u8D25",{projectId:e,configFilePath:l,originalError:R.message})}const c=await g(e,r);return i(e,"INFO",`\u9879\u76EE\u5DF2\u5BFC\u51FA: ${c}`,{projectId:e,zipPath:c}),{success:!0,projectId:e,zipPath:c}}catch(c){throw c.isOperational?c:new E("\u5BFC\u51FA\u9879\u76EE\u5931\u8D25",{projectId:e,originalError:c&&c.message?F(c.message):c&&c.message})}finally{if(y&&n.existsSync(l))try{await n.promises.unlink(l),i(e,"INFO",`\u5DF2\u5220\u9664\u4E34\u65F6\u914D\u7F6E\u6587\u4EF6: ${l}`,{projectId:e,configFilePath:l})}catch(c){i(e,"WARN",`\u5220\u9664\u4E34\u65F6\u914D\u7F6E\u6587\u4EF6\u5931\u8D25: ${c.message}`,{projectId:e,error:c.message})}}}async function b(e,r){if(!e)throw new O("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(r==null)throw new O("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});try{const s=await g(e,r);return i(e,"INFO",`\u5F53\u524D\u7248\u672C\u5DF2\u5907\u4EFD: ${s}`,{projectId:e,zipPath:s}),{success:!0,projectId:e,zipPath:s}}catch(s){throw s.isOperational?s:new E("\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25",{projectId:e,originalError:s&&s.message?F(s.message):s&&s.message})}}export{_ as createProject,v as uploadProject,b as backupCurrentVersion,x as exportProject,g as backupProjectOfVersion,$ as cleanupProjectDirectory,T as handleFileUpload,C as deleteProject};export default{createProject:_,uploadProject:v,backupCurrentVersion:b,exportProject:x,backupProjectOfVersion:g,cleanupProjectDirectory:$,handleFileUpload:T,deleteProject:C};
1
+ import{log as s}from"../utils/log/logUtils.js";import h from"../appConfig/index.js";import l from"path";import n from"fs";import{extractZip as P}from"../utils/common/zipUtils.js";import"../utils/build/startDevUtils.js";import"../utils/build/restartDevUtils.js";import{stopDevServer as F}from"../utils/build/stopDevUtils.js";import{ValidationError as c,BusinessError as J,SystemError as O,FileError as A,ResourceError as N}from"../utils/error/errorHandler.js";import{sanitizeSensitivePaths as S}from"../utils/common/sensitiveUtils.js";import{removeNodeModules as z}from"../utils/buildDependency/dependencyManager.js";import{backupProjectToZip as M,copyDirectoryFiltered as B}from"../utils/project/backupUtils.js";import{createPnpmNpmrc as _}from"../utils/common/npmrcUtils.js";async function T(e){const r=Date.now();if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const u=h.PROJECT_SOURCE_DIR,i=l.join(u,e);if(n.existsSync(i))throw new J(`\u9879\u76EE\u76EE\u5F55 ${e} \u5DF2\u5B58\u5728`,{projectId:e,projectPath:i});try{n.mkdirSync(i,{recursive:!0}),s(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u521B\u5EFA\u6210\u529F: ${i}`,{projectId:e});const t=h.INIT_PROJECT_DIR,a=l.join(t,`${h.INIT_PROJECT_NAME}.zip`),m=l.join(t,h.INIT_PROJECT_NAME);if(s(e,"DEBUG","\u5F00\u59CB\u68C0\u67E5\u6A21\u677F\u76EE\u5F55",{templateDir:m,templateZipPath:a}),!n.existsSync(m)){if(!n.existsSync(a))throw s(e,"ERROR",`\u521D\u59CB\u5316\u6A21\u677F\u4E0D\u5B58\u5728: ${a}`,{projectId:e,templateZipPath:a}),new N("\u521D\u59CB\u5316\u6A21\u677F\u4E0D\u5B58\u5728",{});if(s(e,"INFO",`\u6A21\u677F\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u5F00\u59CB\u89E3\u538B\u6A21\u677F: ${a}`,{projectId:e,templateZipPath:a}),await P(a,m),s(e,"INFO","\u6A21\u677F\u89E3\u538B\u5B8C\u6210",{projectId:e}),!n.existsSync(m))throw new O("\u6A21\u677F\u89E3\u538B\u540E\u76EE\u5F55\u4ECD\u4E0D\u5B58\u5728",{})}s(e,"DEBUG","\u5F00\u59CB\u590D\u5236\u6A21\u677F\u5185\u5BB9\u5230\u9879\u76EE\u76EE\u5F55",{templateDir:m,projectPath:i});const R=await n.promises.readdir(m,{withFileTypes:!0});for(const w of R){const o=l.join(m,w.name),f=l.join(i,w.name);w.isDirectory()?(await n.promises.mkdir(f,{recursive:!0}),await B(o,f)):w.isFile()&&(await n.promises.mkdir(l.dirname(f),{recursive:!0}),await n.promises.copyFile(o,f))}return s(e,"DEBUG","\u5F00\u59CB\u521B\u5EFA .npmrc \u914D\u7F6E\u6587\u4EF6",{projectPath:i}),await _(i,e),s(e,"INFO",`\u9879\u76EE ${e} \u521D\u59CB\u5316\u6210\u529F`,{projectId:e,elapsedMs:Date.now()-r}),{success:!0,message:`\u9879\u76EE ${e} \u521B\u5EFA\u6210\u529F`,projectPath:i}}catch(t){throw s(e,"ERROR",`\u9879\u76EE ${e} \u521D\u59CB\u5316\u5931\u8D25: ${t.message}`,{projectId:e,elapsedMs:Date.now()-r}),new O(`\u9879\u76EE ${e} \u521D\u59CB\u5316\u5931\u8D25: ${t.message}`,{projectId:e,projectPath:i,originalError:t.message})}}async function G(e){const r=await n.promises.readdir(e,{withFileTypes:!0}),u=h.TOP_LEVEL_NOISE_PATTERNS,i=r.filter(t=>{const a=t.name;return a.startsWith(".")?!1:!u.some(m=>m.endsWith("*")?a.startsWith(m.slice(0,-1)):a===m)});if(i.length===1&&i[0].isDirectory()){const t=l.join(e,i[0].name),a=l.join(e,"..",`temp_${Date.now()}`);await n.promises.rename(t,a);const m=await n.promises.readdir(a);for(const R of m){const w=l.join(a,R),o=l.join(e,R);await n.promises.rename(w,o)}await n.promises.rmdir(a)}}async function $(e){if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const r=l.join(h.PROJECT_SOURCE_DIR,e);if(n.existsSync(r))try{s(e,"INFO",`\u5F00\u59CB\u6E05\u7406\u9879\u76EE\u76EE\u5F55: ${r}`,{projectId:e}),await n.promises.rm(r,{recursive:!0,force:!0}),s(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u6E05\u7406\u5B8C\u6210: ${r}`,{projectId:e})}catch(u){throw s(e,"ERROR",`\u6E05\u7406\u9879\u76EE\u76EE\u5F55\u5931\u8D25: ${u.message}`,{projectId:e,projectPath:r,originalError:u.message}),new O(`\u6E05\u7406\u9879\u76EE\u76EE\u5F55\u5931\u8D25: ${u.message}`,{projectId:e,projectPath:r,originalError:u.message})}else s(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u65E0\u9700\u6E05\u7406: ${r}`,{projectId:e})}function L(e){if(!n.existsSync(e))return!0;try{return n.readdirSync(e).filter(i=>!i.startsWith(".")&&i!=="node_modules").length===0}catch(r){const u=l.basename(e);return s(u,"ERROR",`\u68C0\u67E5\u76EE\u5F55\u662F\u5426\u4E3A\u7A7A\u5931\u8D25: ${r.message}`,{dirPath:e}),!0}}async function v(e,r,u,i,t,a){const m=Date.now(),R=h.PROJECT_SOURCE_DIR,w=l.join(R,e);try{if(L(w))s(e,"INFO","\u9879\u76EE\u76EE\u5F55\u4E3A\u7A7A\uFF0C\u76F4\u63A5\u90E8\u7F72\u65B0\u9879\u76EE",{projectId:e});else{s(e,"INFO","\u9879\u76EE\u76EE\u5F55\u975E\u7A7A\uFF0C\u5F00\u59CB\u5907\u4EFD\u5F53\u524D\u7248\u672C",{projectId:e});const f=parseInt(i)-1,y=l.join(h.UPLOAD_PROJECT_DIR,e),E=l.join(y,`${e}-v${f}.zip`);if(n.existsSync(E))s(e,"INFO",`\u5907\u4EFD\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5907\u4EFD: ${E}`,{projectId:e});else try{await g(e,f),s(e,"INFO",`\u5F53\u524D\u7248\u672C\u5DF2\u5907\u4EFD: ${E}`,{projectId:e})}catch(D){throw s(e,"ERROR",`\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25: ${D.message}`,{projectId:e}),new O(`\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25: ${D.message}`,{projectId:e,originalError:D.message})}if(t&&!isNaN(Number(t))){const D=Number(t);s(e,"INFO",`\u6B63\u5728\u505C\u6B62\u65E7\u7248\u672Cdev\u670D\u52A1\u5668\uFF0CPID: ${D}`,{projectId:e});try{await F(u,e,D,{strict:!0}),s(e,"INFO","\u65E7\u7248\u672Cdev\u670D\u52A1\u5668\u5DF2\u505C\u6B62",{projectId:e})}catch(k){s(e,"WARN",`\u505C\u6B62\u65E7\u7248\u672Cdev\u670D\u52A1\u5668\u5931\u8D25: ${k.message}`,{projectId:e,pid:D})}}n.existsSync(w)&&(s(e,"INFO",`\u6B63\u5728\u6E05\u7A7A\u9879\u76EE\u76EE\u5F55: ${w}`,{projectId:e}),await n.promises.rm(w,{recursive:!0,force:!0}))}return n.mkdirSync(w,{recursive:!0}),s(e,"INFO",`\u9879\u76EE\u76EE\u5F55\u521B\u5EFA\u6210\u529F: ${w}`,{projectId:e}),s(e,"DEBUG","\u5F00\u59CB\u89E3\u538B\u538B\u7F29\u5305",{projectId:e,zipFilePath:r}),await P(r,w),s(e,"DEBUG","\u538B\u7F29\u5305\u89E3\u538B\u5B8C\u6210",{projectId:e}),s(e,"DEBUG","\u68C0\u67E5\u5E76\u5904\u7406\u9876\u5C42\u6587\u4EF6\u5939",{projectId:e}),await G(w),s(e,"DEBUG","\u68C0\u67E5\u5E76\u5220\u9664 node_modules \u6587\u4EF6\u5939",{projectId:e}),await z(w),s(e,"DEBUG","\u5F00\u59CB\u521B\u5EFA .npmrc \u914D\u7F6E\u6587\u4EF6",{projectId:e}),await _(w,e),s(e,"INFO",`\u9879\u76EE ${e} \u4E0A\u4F20\u6210\u529F`,{projectId:e,codeVersion:i,elapsedMs:Date.now()-m}),{success:!0,message:`\u9879\u76EE ${e} \u4E0A\u4F20\u6210\u529F`,projectId:e,codeVersion:i}}catch(o){s(e,"ERROR",`\u4E0A\u4F20\u9879\u76EE\u5931\u8D25: ${o.message}`,{projectId:e,elapsedMs:Date.now()-m});try{await $(e),s(e,"INFO","\u4E0A\u4F20\u5931\u8D25\uFF0C\u9879\u76EE\u76EE\u5F55\u5DF2\u6E05\u7406",{projectId:e})}catch(f){s(e,"ERROR",`\u6E05\u7406\u9879\u76EE\u76EE\u5F55\u5931\u8D25: ${f.message}`,{projectId:e,originalError:f.message})}throw o.isOperational?o:new O(`\u4E0A\u4F20\u9879\u76EE\u5931\u8D25: ${o.message}`,{projectId:e,projectPath:w,zipFilePath:r,originalError:o.message})}}async function g(e,r){if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(r==null)throw new c("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});const u=Number(r);if(!Number.isFinite(u))throw new c("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const i=l.join(h.PROJECT_SOURCE_DIR,e);if(!n.existsSync(i))throw new N("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const t=l.join(h.UPLOAD_PROJECT_DIR,e);n.existsSync(t)||n.mkdirSync(t,{recursive:!0});const a=`${e}-v${u}.zip`,m=l.join(t,a);return s(e,"DEBUG","\u5F00\u59CB\u5907\u4EFD\u9879\u76EE\u4E3Azip",{projectId:e,versionNum:u,outZipPath:m}),await M(e,i,m)}async function C(e,r,u){if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(!r)throw new c("\u4EE3\u7801\u7248\u672C\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});if(!u)throw new c("\u8BF7\u4E0A\u4F20\u538B\u7F29\u5305\u6587\u4EF6",{field:"zipFile"});const i=l.join(h.UPLOAD_PROJECT_DIR,e);n.existsSync(i)||n.mkdirSync(i,{recursive:!0});const t=u.path,a=l.join(i,`${e}-v${r}.zip`);try{return n.renameSync(t,a),s(e,"INFO","\u6587\u4EF6\u4FDD\u5B58\u6210\u529F",{projectId:e,codeVersion:r,filePath:a}),{success:!0,filePath:a}}catch(m){if(s(e,"ERROR","\u79FB\u52A8\u6587\u4EF6\u5931\u8D25",{projectId:e,codeVersion:r,error:m.message}),n.existsSync(t))try{n.unlinkSync(t)}catch(R){s(e,"ERROR","\u6E05\u7406\u4E34\u65F6\u6587\u4EF6\u5931\u8D25",{projectId:e,error:R.message})}throw new O("\u6587\u4EF6\u4FDD\u5B58\u5931\u8D25",{projectId:e,codeVersion:r,originalError:m.message})}}async function U(e,r,u){const i=Date.now();if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});const t=null;try{if(r&&!isNaN(Number(r))){const o=Number(r);s(t,"INFO",`[delete-project] \u6B63\u5728\u505C\u6B62\u5F00\u53D1\u670D\u52A1\u5668\uFF0CPID: ${o}`,{projectId:e,pid:o});try{await F(u,e,o,{strict:!0}),s(t,"INFO","[delete-project] \u5F00\u53D1\u670D\u52A1\u5668\u5DF2\u505C\u6B62",{projectId:e})}catch(f){s(t,"WARN",`[delete-project] \u505C\u6B62\u5F00\u53D1\u670D\u52A1\u5668\u5931\u8D25: ${f.message}`,{projectId:e,pid:o})}}const a=[l.join(h.UPLOAD_PROJECT_DIR,e),l.join(h.PROJECT_SOURCE_DIR,e),l.join(h.DIST_TARGET_DIR,e),l.join(h.LOG_BASE_DIR,e)],m=[],R=[];for(const o of a)if(n.existsSync(o))try{await n.promises.rm(o,{recursive:!0,force:!0}),m.push(o),s(t,"INFO",`[delete-project] \u76EE\u5F55\u5220\u9664\u6210\u529F: ${o}`,{projectId:e})}catch(f){R.push({path:o,error:f.message}),s(t,"ERROR",`[delete-project] \u76EE\u5F55\u5220\u9664\u5931\u8D25: ${o}`,{projectId:e,error:f.message})}else s(t,"INFO",`[delete-project] \u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5220\u9664: ${o}`,{projectId:e});const w={success:!0,message:`\u9879\u76EE ${e} \u5220\u9664\u5B8C\u6210`,projectId:e,deletedDirectories:m,failedDirectories:R};return R.length>0&&(w.message+=`\uFF0C\u4F46\u6709 ${R.length} \u4E2A\u76EE\u5F55\u5220\u9664\u5931\u8D25`,s(t,"WARN","[delete-project] \u90E8\u5206\u76EE\u5F55\u5220\u9664\u5931\u8D25",{projectId:e,failedDirs:R})),s(t,"INFO",`[delete-project] \u9879\u76EE\u5220\u9664\u5B8C\u6210: ${e}`,{projectId:e,elapsedMs:Date.now()-i}),w}catch(a){throw s(t,"ERROR",`[delete-project] \u5220\u9664\u9879\u76EE\u5931\u8D25: ${a.message}`,{projectId:e,originalError:a.message,elapsedMs:Date.now()-i}),a.isOperational?a:new O(`\u5220\u9664\u9879\u76EE\u5931\u8D25: ${a.message}`,{projectId:e,originalError:a.message})}}async function x(e,r,u,i){const t=Date.now();if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(r==null)throw new c("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});const a=Number(r);if(!Number.isFinite(a))throw new c("codeVersion\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const m=l.join(h.PROJECT_SOURCE_DIR,e);if(!n.existsSync(m))throw new N("\u9879\u76EE\u4E0D\u5B58\u5728",{projectId:e});const R=l.join(h.UPLOAD_PROJECT_DIR,e),w=`${e}-v${a}.zip`,o=l.join(R,w);if(u!=="LATEST"){if(n.existsSync(o))return s(e,"INFO",`\u4F7F\u7528\u5DF2\u5B58\u5728\u7684\u5BFC\u51FA\u6587\u4EF6: ${o}`,{projectId:e,zipPath:o}),{success:!0,projectId:e,zipPath:o};throw new N(`\u6307\u5B9A\u7248\u672C\u7684zip\u5305\u4E0D\u5B58\u5728: ${o}`,{projectId:e,zipPath:o})}const f=l.join(m,"cpage_config.json");let y=!1;try{if(i)try{const D=JSON.stringify(i,null,2);await n.promises.writeFile(f,D,"utf8"),y=!0,s(e,"INFO",`\u5DF2\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6: ${f}`,{projectId:e,configFilePath:f})}catch(D){throw s(e,"ERROR",`\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6\u5931\u8D25: ${D.message}`,{projectId:e,error:D.message}),new A("\u521B\u5EFA\u914D\u7F6E\u6587\u4EF6\u5931\u8D25",{projectId:e,configFilePath:f,originalError:D.message})}s(e,"DEBUG","\u5F00\u59CB\u6267\u884C\u5BFC\u51FA\u6253\u5305",{projectId:e,codeVersion:r});const E=await g(e,r);return s(e,"INFO",`\u9879\u76EE\u5DF2\u5BFC\u51FA: ${E}`,{projectId:e,zipPath:E,elapsedMs:Date.now()-t}),{success:!0,projectId:e,zipPath:E}}catch(E){throw s(e,"ERROR",`\u5BFC\u51FA\u9879\u76EE\u5931\u8D25: ${E?.message}`,{projectId:e,elapsedMs:Date.now()-t}),E.isOperational?E:new O("\u5BFC\u51FA\u9879\u76EE\u5931\u8D25",{projectId:e,originalError:E&&E.message?S(E.message):E&&E.message})}finally{if(y&&n.existsSync(f))try{await n.promises.unlink(f),s(e,"INFO",`\u5DF2\u5220\u9664\u4E34\u65F6\u914D\u7F6E\u6587\u4EF6: ${f}`,{projectId:e,configFilePath:f})}catch(E){s(e,"WARN",`\u5220\u9664\u4E34\u65F6\u914D\u7F6E\u6587\u4EF6\u5931\u8D25: ${E.message}`,{projectId:e,error:E.message})}}}async function b(e,r){const u=Date.now();if(!e)throw new c("\u9879\u76EEID\u4E0D\u80FD\u4E3A\u7A7A",{field:"projectId"});if(r==null)throw new c("codeVersion\u4E0D\u80FD\u4E3A\u7A7A",{field:"codeVersion"});try{const i=await g(e,r);return s(e,"INFO",`\u5F53\u524D\u7248\u672C\u5DF2\u5907\u4EFD: ${i}`,{projectId:e,zipPath:i,elapsedMs:Date.now()-u}),{success:!0,projectId:e,zipPath:i}}catch(i){throw s(e,"ERROR",`\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25: ${i?.message}`,{projectId:e,elapsedMs:Date.now()-u}),i.isOperational?i:new O("\u5907\u4EFD\u5F53\u524D\u7248\u672C\u5931\u8D25",{projectId:e,originalError:i&&i.message?S(i.message):i&&i.message})}}export{T as createProject,v as uploadProject,b as backupCurrentVersion,x as exportProject,g as backupProjectOfVersion,$ as cleanupProjectDirectory,C as handleFileUpload,U as deleteProject};export default{createProject:T,uploadProject:v,backupCurrentVersion:b,exportProject:x,backupProjectOfVersion:g,cleanupProjectDirectory:$,handleFileUpload:C,deleteProject:U};
@@ -1 +1 @@
1
- import u from"fs";import g from"path";import O from"archiver";import P from"../../appConfig/index.js";import{log as i}from"../log/logUtils.js";import{ValidationError as w,SystemError as F}from"../error/errorHandler.js";function v(c){try{const a=u.readFileSync(c);if(a.includes(0))return!0;const t=a.toString("utf-8");for(let s=0;s<t.length;s++){const d=t.charCodeAt(s);if(d<32&&d!==9&&d!==10&&d!==13)return!0}return!1}catch(a){return i("system","WARN",`\u68C0\u6D4B\u4E8C\u8FDB\u5236\u6587\u4EF6\u5931\u8D25: ${c}`,{error:a.message}),!1}}function j(c){const a=g.extname(c).toLowerCase();return[".jpg",".jpeg",".png",".gif",".bmp",".webp",".svg",".ico",".avif"].includes(a)}async function S(c,a,t,s){const d=[],l=await u.promises.readdir(c,{withFileTypes:!0});l.sort((o,n)=>o.isDirectory()&&!n.isDirectory()?-1:!o.isDirectory()&&n.isDirectory()?1:o.name.toLowerCase().localeCompare(n.name.toLowerCase()));for(const o of l){const n=g.join(c,o.name);if(!(o.name.startsWith(".")||(P.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(o.name))&&!(o.isDirectory()&&P.TRAVERSE_EXCLUDE_DIRS.includes(o.name)))if(o.isDirectory()){const f=await S(n,a,t,s);if(f.length===0){const h=a||c,r=g.relative(h,n);d.push({name:r,isDir:!0})}else d.push(...f)}else try{const f=await u.promises.stat(n),h=a||c,r=g.relative(h,n),m=v(n),p=o.isSymbolicLink();let I=null;s&&(I=`${s}/${r}`);const R={name:r,isDir:!1,binary:m,fileProxyUrl:I,isLink:p};d.push(R)}catch(f){i(t,"WARN",`\u5904\u7406\u6587\u4EF6\u5931\u8D25: ${n}`,{error:f.message})}}return d}async function A(c,a,t){const s=`computer:${c}:${a}`,d=P.COMPUTER_WORKSPACE_DIR;if(!c)throw new w("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new w("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});const l=String(c),o=String(a),n=g.join(d,l,o);if(!u.existsSync(n))return i(s,"INFO","\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8FD4\u56DE\u7A7A\u5217\u8868",{targetDir:n,userId:l,cId:o}),{files:[]};i(s,"INFO","\u5F00\u59CB\u83B7\u53D6\u7528\u6237\u6587\u4EF6\u5217\u8868",{targetDir:n,userId:l,cId:o});try{const e=await S(n,n,s,t);return i(s,"INFO","\u7528\u6237\u6587\u4EF6\u5217\u8868\u83B7\u53D6\u5B8C\u6210",{fileCount:e.length,targetDir:n,userId:l,cId:o}),{files:e}}catch(e){throw i(s,"ERROR","\u83B7\u53D6\u7528\u6237\u6587\u4EF6\u5217\u8868\u5931\u8D25",{targetDir:n,userId:l,cId:o,error:e.message}),new F(`\u83B7\u53D6\u6587\u4EF6\u5217\u8868\u5931\u8D25: ${e.message}`,{targetDir:n,originalError:e.message})}}async function D(c,a,t){const s=`computer:${c}:${a}`,d=P.COMPUTER_WORKSPACE_DIR;if(!c)throw new w("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new w("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!Array.isArray(t))throw new w("files\u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});const l=String(c),o=String(a),n=g.join(d,l,o);u.existsSync(n)||u.mkdirSync(n,{recursive:!0});for(let e=0;e<t.length;e++){const f=t[e];if(!f||typeof f.operation!="string")throw new w(`files[${e}].operation \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${e}].operation`});if(!f.name||typeof f.name!="string")throw new w(`files[${e}].name \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${e}].name`});const h=f.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(h))throw new w(`files[${e}].operation \u5FC5\u987B\u662F create\u3001delete\u3001rename \u6216 modify \u4E4B\u4E00`,{field:`files[${e}].operation`});if(h==="rename"&&!f.renameFrom)throw new w(`files[${e}].renameFrom \u4E0D\u80FD\u4E3A\u7A7A\uFF08\u91CD\u547D\u540D\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${e}].renameFrom`});if(h==="modify"&&f.isDir!==!0&&typeof f.contents!="string")throw new w(`files[${e}].contents \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\uFF08\u4FEE\u6539\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${e}].contents`})}i(s,"INFO","\u5F00\u59CB\u66F4\u65B0\u7528\u6237\u6587\u4EF6",{userId:l,cId:o,filesCount:t.length});try{for(const e of t){const f=e.operation.toLowerCase(),h=e.name,r=g.normalize(h).replace(/^[\/\\]+/,""),m=g.join(n,r),p=g.resolve(m),I=g.resolve(n);if(!p.startsWith(I+g.sep)&&p!==I){i(s,"WARN","\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7",{filePath:r,resolvedPath:p});continue}switch(f){case"create":{if(e.isDir===!0){if(u.existsSync(m)){if((await u.promises.stat(m)).isFile())throw new w("\u65E0\u6CD5\u521B\u5EFA\u76EE\u5F55\uFF0C\u5DF2\u5B58\u5728\u540C\u540D\u6587\u4EF6",{filePath:r});i(s,"INFO","\u76EE\u5F55\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u521B\u5EFA",{filePath:r});break}await u.promises.mkdir(m,{recursive:!0}),i(s,"INFO","\u76EE\u5F55\u521B\u5EFA\u6210\u529F",{filePath:r});break}if(u.existsSync(m)){if((await u.promises.stat(m)).isDirectory())throw new w("\u65E0\u6CD5\u521B\u5EFA\u6587\u4EF6\uFF0C\u5DF2\u5B58\u5728\u540C\u540D\u76EE\u5F55",{filePath:r});i(s,"INFO","\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u521B\u5EFA",{filePath:r});break}await u.promises.mkdir(g.dirname(m),{recursive:!0});const R=e.contents||"";await u.promises.writeFile(m,R,"utf8"),i(s,"INFO","\u6587\u4EF6\u521B\u5EFA\u6210\u529F",{filePath:r});break}case"delete":{u.existsSync(m)?(await u.promises.stat(m)).isDirectory()?(await u.promises.rm(m,{recursive:!0,force:!0}),i(s,"INFO","\u76EE\u5F55\u5220\u9664\u6210\u529F",{filePath:r})):(await u.promises.unlink(m),i(s,"INFO","\u6587\u4EF6\u5220\u9664\u6210\u529F",{filePath:r})):i(s,"WARN","\u8981\u5220\u9664\u7684\u6587\u4EF6\u6216\u76EE\u5F55\u4E0D\u5B58\u5728",{filePath:r});break}case"rename":{const R=e.renameFrom;if(!R||typeof R!="string"){i(s,"WARN","\u91CD\u547D\u540D\u64CD\u4F5C\u7F3A\u5C11 renameFrom",{filePath:r});break}const y=g.normalize(R).replace(/^[\/\\]+/,""),E=g.join(n,y),N=g.resolve(E);if(!N.startsWith(I+g.sep)&&N!==I){i(s,"WARN","\u6E90\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7\u91CD\u547D\u540D",{sourcePath:y,targetPath:r});break}if(u.existsSync(E)){const $=(await u.promises.stat(E)).isDirectory();await u.promises.mkdir(g.dirname(m),{recursive:!0}),await u.promises.rename(E,m),i(s,"INFO",$?"\u76EE\u5F55\u91CD\u547D\u540D\u6210\u529F":"\u6587\u4EF6\u91CD\u547D\u540D\u6210\u529F",{sourcePath:y,targetPath:r})}else i(s,"WARN","\u8981\u91CD\u547D\u540D\u7684\u6587\u4EF6\u6216\u76EE\u5F55\u4E0D\u5B58\u5728",{sourcePath:y});break}case"modify":{if(!u.existsSync(m)){i(s,"WARN","\u8981\u4FEE\u6539\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{filePath:r});break}if((await u.promises.stat(m)).isDirectory()){i(s,"INFO","\u76EE\u6807\u662F\u76EE\u5F55\uFF0C\u8DF3\u8FC7\u4FEE\u6539",{filePath:r});break}const y=typeof e.contents=="string"?e.contents:"";if(await u.promises.readFile(m,"utf8")===y){i(s,"INFO","\u6587\u4EF6\u5185\u5BB9\u65E0\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5199\u5165",{filePath:r});break}await u.promises.writeFile(m,y,"utf8"),i(s,"INFO","\u6587\u4EF6\u4FEE\u6539\u6210\u529F",{filePath:r});break}default:{i(s,"WARN","\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7C7B\u578B",{operation:f,filePath:r});break}}}return i(s,"INFO","\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u6210\u529F",{userId:l,cId:o,filesCount:t.length}),{success:!0,message:"\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u6210\u529F",userId:l,cId:o,filesCount:t.length}}catch(e){throw i(s,"ERROR","\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u5931\u8D25",{userId:l,cId:o,error:e.message}),new F(`\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u5931\u8D25: ${e.message}`,{userId:l,cId:o,originalError:e.message})}}async function C(c,a,t,s){const d=`computer:${c}:${a}`,l=P.COMPUTER_WORKSPACE_DIR;if(!c)throw new w("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new w("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!t)throw new w("\u6587\u4EF6\u4E0D\u80FD\u4E3A\u7A7A",{field:"file"});if(!s||typeof s!="string")throw new w("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A",{field:"filePath"});const o=String(c),n=String(a),e=g.join(l,o,n);u.existsSync(e)||u.mkdirSync(e,{recursive:!0});const f=g.normalize(s).replace(/^[\/\\]+/,""),h=g.join(e,f),r=g.resolve(h),m=g.resolve(e);if(!r.startsWith(m+g.sep)&&r!==m)throw new w("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u4E0D\u80FD\u8D85\u51FA\u7528\u6237\u76EE\u5F55",{field:"filePath",providedPath:s,resolvedPath:r});try{if(await u.promises.mkdir(g.dirname(h),{recursive:!0}),t.buffer)await u.promises.writeFile(h,t.buffer);else if(typeof t.contents=="string")await u.promises.writeFile(h,t.contents,"utf8");else throw new w("\u6587\u4EF6\u5185\u5BB9\u683C\u5F0F\u4E0D\u6B63\u786E",{field:"file",hasBuffer:!!t.buffer,hasContents:typeof t.contents});return i(d,"INFO","\u6587\u4EF6\u4E0A\u4F20\u6210\u529F",{userId:o,cId:n,filePath:f,targetPath:r,fileSize:t.buffer?t.buffer.length:t.contents?t.contents.length:0}),{success:!0,message:"\u6587\u4EF6\u4E0A\u4F20\u6210\u529F",fileSize:t.buffer?t.buffer.length:t.contents?t.contents.length:0}}catch(p){throw i(d,"ERROR","\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25",{userId:o,cId:n,filePath:f,error:p.message}),new F(`\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25: ${p.message}`,{userId:o,cId:n,filePath:f,originalError:p.message})}}async function k(c,a,t,s){const d=`computer:${c}:${a}`;if(!c)throw new w("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new w("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!Array.isArray(t))throw new w("files \u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});if(!Array.isArray(s))throw new w("filePaths \u5FC5\u987B\u662F\u6570\u7EC4",{field:"filePaths"});if(t.length!==s.length)throw new w(`\u6587\u4EF6\u6570\u91CF (${t.length}) \u4E0E\u8DEF\u5F84\u6570\u91CF (${s.length}) \u4E0D\u5339\u914D`,{field:"filePaths"});i(d,"INFO","\u5F00\u59CB\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6",{userId:c,cId:a,filesCount:t.length});const l=[];try{for(let e=0;e<t.length;e++){const f=t[e],h=s[e];if(!f){i(d,"WARN","\u6279\u91CF\u4E0A\u4F20\u4E2D\u9047\u5230\u7A7A\u6587\u4EF6\u5BF9\u8C61\uFF0C\u8DF3\u8FC7",{index:e,filePath:h}),l.push({success:!1,filePath:h,error:"\u6587\u4EF6\u5BF9\u8C61\u4E3A\u7A7A"});continue}if(!h||typeof h!="string"){i(d,"WARN","\u6279\u91CF\u4E0A\u4F20\u4E2D\u6587\u4EF6\u8DEF\u5F84\u65E0\u6548\uFF0C\u8DF3\u8FC7",{index:e,originalname:f.originalname}),l.push({success:!1,filePath:h||"",originalname:f.originalname,error:"\u6587\u4EF6\u8DEF\u5F84\u65E0\u6548"});continue}try{const r=await C(c,a,f,h);l.push({success:!0,filePath:h,originalname:f.originalname,...r})}catch(r){i(d,"ERROR","\u6279\u91CF\u4E0A\u4F20\u4E2D\u5355\u4E2A\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25",{filePath:h,originalname:f.originalname,error:r.message}),l.push({success:!1,filePath:h,originalname:f.originalname,error:r.message})}}const o=l.filter(e=>e.success).length,n=l.filter(e=>!e.success).length;return i(d,"INFO","\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u5B8C\u6210",{userId:c,cId:a,totalCount:t.length,successCount:o,failCount:n}),{success:!0,message:"\u6279\u91CF\u4E0A\u4F20\u5B8C\u6210",totalCount:t.length,successCount:o,failCount:n,results:l}}catch(o){throw i(d,"ERROR","\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25",{userId:c,cId:a,error:o.message}),new F(`\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25: ${o.message}`,{userId:c,cId:a,originalError:o.message})}}async function b(c,a){const t=`computer:${c}:${a}`,s=P.COMPUTER_WORKSPACE_DIR;if(!c)throw new w("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new w("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!s)throw new F("COMPUTER_WORKSPACE_DIR \u672A\u914D\u7F6E\uFF0C\u65E0\u6CD5\u521B\u5EFA\u538B\u7F29\u5305");const d=String(c),l=String(a),o=g.join(s,d,l);if(!u.existsSync(o)){const r=`${d}_${l}.zip`;i(t,"WARN","\u5DE5\u4F5C\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8FD4\u56DE\u7A7A\u538B\u7F29\u5305",{targetDir:o,userId:d,cId:l,zipFileName:r});const m=O("zip",{zlib:{level:9}});return m.append(null,{name:`${d}_${l}/`,type:"directory"}),m.on("warning",p=>{if(p.code==="ENOENT")i(t,"WARN","\u521B\u5EFA\u7A7A\u538B\u7F29\u5305\u65F6\u9047\u5230\u6587\u4EF6\u95EE\u9898",{message:p.message,code:p.code});else throw i(t,"ERROR","\u521B\u5EFA\u7A7A\u538B\u7F29\u5305\u65F6\u53D1\u751F\u8B66\u544A",{message:p.message,code:p.code}),p}),m.on("error",p=>{i(t,"ERROR","\u521B\u5EFA\u7A7A\u538B\u7F29\u5305\u5931\u8D25",{message:p.message})}),{archive:m,zipFileName:r}}const n=`${d}_${l}.zip`;i(t,"INFO","\u5F00\u59CB\u521B\u5EFA\u5DE5\u4F5C\u76EE\u5F55\u538B\u7F29\u5305",{targetDir:o,zipFileName:n});const e=O("zip",{zlib:{level:9}}),f=P.CONTENT_TRAVERSE_EXCLUDE_FILES||[],h=P.TRAVERSE_EXCLUDE_DIRS||[];return e.directory(o,`${d}_${l}`,r=>{const m=r.name||"",p=m.split(/[\/\\]/).filter(Boolean);if(p.some(R=>R.startsWith(".")))return!1;const I=p[p.length-1];if(f.includes(I)||p.some(R=>h.includes(R)))return!1;try{const R=g.join(o,m),y=u.lstatSync(R);if(y.isSymbolicLink()||y.nlink>1)return!1}catch(R){return i(t,"WARN","\u68C0\u6D4B\u94FE\u63A5\u6587\u4EF6\u65F6\u51FA\u9519\uFF0C\u8DF3\u8FC7\u8BE5\u6587\u4EF6",{filePath:m,error:R.message}),!1}return r}),e.on("warning",r=>{if(r.code==="ENOENT")i(t,"WARN","\u521B\u5EFA\u538B\u7F29\u5305\u65F6\u9047\u5230\u6587\u4EF6\u95EE\u9898",{message:r.message,code:r.code});else throw i(t,"ERROR","\u521B\u5EFA\u538B\u7F29\u5305\u65F6\u53D1\u751F\u8B66\u544A",{message:r.message,code:r.code}),r}),e.on("error",r=>{i(t,"ERROR","\u521B\u5EFA\u538B\u7F29\u5305\u5931\u8D25",{message:r.message})}),{archive:e,zipFileName:n}}export{A as getFileList,D as updateFiles,C as uploadFile,k as uploadFiles,b as downloadAllFiles};
1
+ import m from"fs";import w from"path";import N from"archiver";import P from"../../appConfig/index.js";import{log as s}from"../log/logUtils.js";import{ValidationError as g,SystemError as F}from"../error/errorHandler.js";function v(l){try{const a=m.readFileSync(l);if(a.includes(0))return!0;const i=a.toString("utf-8");for(let c=0;c<i.length;c++){const r=i.charCodeAt(c);if(r<32&&r!==9&&r!==10&&r!==13)return!0}return!1}catch(a){return s("system","WARN",`\u68C0\u6D4B\u4E8C\u8FDB\u5236\u6587\u4EF6\u5931\u8D25: ${l}`,{error:a.message}),!1}}function j(l){const a=w.extname(l).toLowerCase();return[".jpg",".jpeg",".png",".gif",".bmp",".webp",".svg",".ico",".avif"].includes(a)}async function C(l,a,i,c){const r=[],R=await m.promises.readdir(l,{withFileTypes:!0});R.sort((n,o)=>n.isDirectory()&&!o.isDirectory()?-1:!n.isDirectory()&&o.isDirectory()?1:n.name.toLowerCase().localeCompare(o.name.toLowerCase()));for(const n of R){const o=w.join(l,n.name);if(!(n.name.startsWith(".")||(P.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(n.name))&&!(n.isDirectory()&&P.TRAVERSE_EXCLUDE_DIRS.includes(n.name)))if(n.isDirectory()){const e=await C(o,a,i,c);if(e.length===0){const d=a||l,h=w.relative(d,o).replace(/\\/g,"/");r.push({name:h,isDir:!0})}else r.push(...e)}else try{const e=await m.promises.stat(o),d=a||l,h=w.relative(d,o).replace(/\\/g,"/"),t=v(o),f=n.isSymbolicLink();let p=null;if(c){const y=h.split("/").map(I=>encodeURIComponent(I)).join("/");p=`${c}/${y}`}const E={name:h,isDir:!1,binary:t,fileProxyUrl:p,isLink:f};r.push(E)}catch(e){s(i,"WARN",`\u5904\u7406\u6587\u4EF6\u5931\u8D25: ${o}`,{error:e.message})}}return r}async function A(l,a,i){const c=Date.now(),r=`computer:${l}:${a}`,R=P.COMPUTER_WORKSPACE_DIR;if(!l)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});const n=String(l),o=String(a),u=w.join(R,n,o);if(!m.existsSync(u))return s(r,"INFO","\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8FD4\u56DE\u7A7A\u5217\u8868",{targetDir:u,userId:n,cId:o}),{files:[]};s(r,"DEBUG","\u5F00\u59CB\u83B7\u53D6\u7528\u6237\u6587\u4EF6\u5217\u8868",{targetDir:u,userId:n,cId:o});try{const e=await C(u,u,r,i);return s(r,"INFO","\u7528\u6237\u6587\u4EF6\u5217\u8868\u83B7\u53D6\u5B8C\u6210",{fileCount:e.length,targetDir:u,userId:n,cId:o,elapsedMs:Date.now()-c}),{files:e}}catch(e){throw s(r,"ERROR","\u83B7\u53D6\u7528\u6237\u6587\u4EF6\u5217\u8868\u5931\u8D25",{targetDir:u,userId:n,cId:o,error:e.message,elapsedMs:Date.now()-c}),new F(`\u83B7\u53D6\u6587\u4EF6\u5217\u8868\u5931\u8D25: ${e.message}`,{targetDir:u,originalError:e.message})}}async function k(l,a,i){const c=Date.now(),r=`computer:${l}:${a}`,R=P.COMPUTER_WORKSPACE_DIR;if(!l)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!Array.isArray(i))throw new g("files\u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});const n=String(l),o=String(a),u=w.join(R,n,o);m.existsSync(u)||m.mkdirSync(u,{recursive:!0});for(let e=0;e<i.length;e++){const d=i[e];if(!d||typeof d.operation!="string")throw new g(`files[${e}].operation \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${e}].operation`});if(!d.name||typeof d.name!="string")throw new g(`files[${e}].name \u4E0D\u80FD\u4E3A\u7A7A`,{field:`files[${e}].name`});const h=d.operation.toLowerCase();if(!["create","delete","rename","modify"].includes(h))throw new g(`files[${e}].operation \u5FC5\u987B\u662F create\u3001delete\u3001rename \u6216 modify \u4E4B\u4E00`,{field:`files[${e}].operation`});if(h==="rename"&&!d.renameFrom)throw new g(`files[${e}].renameFrom \u4E0D\u80FD\u4E3A\u7A7A\uFF08\u91CD\u547D\u540D\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${e}].renameFrom`});if(h==="modify"&&d.isDir!==!0&&typeof d.contents!="string")throw new g(`files[${e}].contents \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\uFF08\u4FEE\u6539\u64CD\u4F5C\u9700\u8981\uFF09`,{field:`files[${e}].contents`})}s(r,"DEBUG","\u5F00\u59CB\u66F4\u65B0\u7528\u6237\u6587\u4EF6",{userId:n,cId:o,filesCount:i.length});try{for(const e of i){const d=e.operation.toLowerCase(),h=e.name,t=w.normalize(h).replace(/^[\/\\]+/,""),f=w.join(u,t),p=w.resolve(f),E=w.resolve(u);if(!p.startsWith(E+w.sep)&&p!==E){s(r,"WARN","\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7",{filePath:t,resolvedPath:p});continue}switch(d){case"create":{if(e.isDir===!0){if(m.existsSync(f)){if((await m.promises.stat(f)).isFile())throw new g("\u65E0\u6CD5\u521B\u5EFA\u76EE\u5F55\uFF0C\u5DF2\u5B58\u5728\u540C\u540D\u6587\u4EF6",{filePath:t});s(r,"INFO","\u76EE\u5F55\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u521B\u5EFA",{filePath:t});break}await m.promises.mkdir(f,{recursive:!0}),s(r,"INFO","\u76EE\u5F55\u521B\u5EFA\u6210\u529F",{filePath:t});break}if(m.existsSync(f)){if((await m.promises.stat(f)).isDirectory())throw new g("\u65E0\u6CD5\u521B\u5EFA\u6587\u4EF6\uFF0C\u5DF2\u5B58\u5728\u540C\u540D\u76EE\u5F55",{filePath:t});s(r,"INFO","\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u521B\u5EFA",{filePath:t});break}await m.promises.mkdir(w.dirname(f),{recursive:!0});const y=e.contents||"";await m.promises.writeFile(f,y,"utf8"),s(r,"INFO","\u6587\u4EF6\u521B\u5EFA\u6210\u529F",{filePath:t});break}case"delete":{m.existsSync(f)?(await m.promises.stat(f)).isDirectory()?(await m.promises.rm(f,{recursive:!0,force:!0}),s(r,"INFO","\u76EE\u5F55\u5220\u9664\u6210\u529F",{filePath:t})):(await m.promises.unlink(f),s(r,"INFO","\u6587\u4EF6\u5220\u9664\u6210\u529F",{filePath:t})):s(r,"WARN","\u8981\u5220\u9664\u7684\u6587\u4EF6\u6216\u76EE\u5F55\u4E0D\u5B58\u5728",{filePath:t});break}case"rename":{const y=e.renameFrom;if(!y||typeof y!="string"){s(r,"WARN","\u91CD\u547D\u540D\u64CD\u4F5C\u7F3A\u5C11 renameFrom",{filePath:t});break}const I=w.normalize(y).replace(/^[\/\\]+/,""),D=w.join(u,I),S=w.resolve(D);if(!S.startsWith(E+w.sep)&&S!==E){s(r,"WARN","\u6E90\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u8DF3\u8FC7\u91CD\u547D\u540D",{sourcePath:I,targetPath:t});break}if(m.existsSync(D)){const $=(await m.promises.stat(D)).isDirectory();await m.promises.mkdir(w.dirname(f),{recursive:!0}),await m.promises.rename(D,f),s(r,"INFO",$?"\u76EE\u5F55\u91CD\u547D\u540D\u6210\u529F":"\u6587\u4EF6\u91CD\u547D\u540D\u6210\u529F",{sourcePath:I,targetPath:t})}else s(r,"WARN","\u8981\u91CD\u547D\u540D\u7684\u6587\u4EF6\u6216\u76EE\u5F55\u4E0D\u5B58\u5728",{sourcePath:I});break}case"modify":{if(!m.existsSync(f)){s(r,"WARN","\u8981\u4FEE\u6539\u7684\u6587\u4EF6\u4E0D\u5B58\u5728",{filePath:t});break}if((await m.promises.stat(f)).isDirectory()){s(r,"INFO","\u76EE\u6807\u662F\u76EE\u5F55\uFF0C\u8DF3\u8FC7\u4FEE\u6539",{filePath:t});break}const I=typeof e.contents=="string"?e.contents:"";if(await m.promises.readFile(f,"utf8")===I){s(r,"INFO","\u6587\u4EF6\u5185\u5BB9\u65E0\u53D8\u5316\uFF0C\u8DF3\u8FC7\u5199\u5165",{filePath:t});break}await m.promises.writeFile(f,I,"utf8"),s(r,"INFO","\u6587\u4EF6\u4FEE\u6539\u6210\u529F",{filePath:t});break}default:{s(r,"WARN","\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7C7B\u578B",{operation:d,filePath:t});break}}}return s(r,"INFO","\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u6210\u529F",{userId:n,cId:o,filesCount:i.length,elapsedMs:Date.now()-c}),{success:!0,message:"\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u6210\u529F",userId:n,cId:o,filesCount:i.length}}catch(e){throw s(r,"ERROR","\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u5931\u8D25",{userId:n,cId:o,error:e.message,elapsedMs:Date.now()-c}),new F(`\u7528\u6237\u6587\u4EF6\u66F4\u65B0\u5931\u8D25: ${e.message}`,{userId:n,cId:o,originalError:e.message})}}async function O(l,a,i,c){const r=Date.now(),R=`computer:${l}:${a}`,n=P.COMPUTER_WORKSPACE_DIR;if(!l)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!i)throw new g("\u6587\u4EF6\u4E0D\u80FD\u4E3A\u7A7A",{field:"file"});if(!c||typeof c!="string")throw new g("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A",{field:"filePath"});const o=String(l),u=String(a),e=w.join(n,o,u);m.existsSync(e)||m.mkdirSync(e,{recursive:!0});const d=w.normalize(c).replace(/^[\/\\]+/,""),h=w.join(e,d),t=w.resolve(h),f=w.resolve(e);if(!t.startsWith(f+w.sep)&&t!==f)throw new g("\u6587\u4EF6\u8DEF\u5F84\u4E0D\u5B89\u5168\uFF0C\u4E0D\u80FD\u8D85\u51FA\u7528\u6237\u76EE\u5F55",{field:"filePath",providedPath:c,resolvedPath:t});try{if(await m.promises.mkdir(w.dirname(h),{recursive:!0}),i.buffer)await m.promises.writeFile(h,i.buffer);else if(typeof i.contents=="string")await m.promises.writeFile(h,i.contents,"utf8");else throw new g("\u6587\u4EF6\u5185\u5BB9\u683C\u5F0F\u4E0D\u6B63\u786E",{field:"file",hasBuffer:!!i.buffer,hasContents:typeof i.contents});return s(R,"INFO","\u6587\u4EF6\u4E0A\u4F20\u6210\u529F",{userId:o,cId:u,filePath:d,targetPath:t,fileSize:i.buffer?i.buffer.length:i.contents?i.contents.length:0,elapsedMs:Date.now()-r}),{success:!0,message:"\u6587\u4EF6\u4E0A\u4F20\u6210\u529F",fileSize:i.buffer?i.buffer.length:i.contents?i.contents.length:0}}catch(p){throw s(R,"ERROR","\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25",{userId:o,cId:u,filePath:d,error:p.message,elapsedMs:Date.now()-r}),new F(`\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25: ${p.message}`,{userId:o,cId:u,filePath:d,originalError:p.message})}}async function b(l,a,i,c){const r=Date.now(),R=`computer:${l}:${a}`;if(!l)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!Array.isArray(i))throw new g("files \u5FC5\u987B\u662F\u6570\u7EC4",{field:"files"});if(!Array.isArray(c))throw new g("filePaths \u5FC5\u987B\u662F\u6570\u7EC4",{field:"filePaths"});if(i.length!==c.length)throw new g(`\u6587\u4EF6\u6570\u91CF (${i.length}) \u4E0E\u8DEF\u5F84\u6570\u91CF (${c.length}) \u4E0D\u5339\u914D`,{field:"filePaths"});s(R,"DEBUG","\u5F00\u59CB\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6",{userId:l,cId:a,filesCount:i.length});const n=[];try{for(let e=0;e<i.length;e++){const d=i[e],h=c[e];if(!d){s(R,"WARN","\u6279\u91CF\u4E0A\u4F20\u4E2D\u9047\u5230\u7A7A\u6587\u4EF6\u5BF9\u8C61\uFF0C\u8DF3\u8FC7",{index:e,filePath:h}),n.push({success:!1,filePath:h,error:"\u6587\u4EF6\u5BF9\u8C61\u4E3A\u7A7A"});continue}if(!h||typeof h!="string"){s(R,"WARN","\u6279\u91CF\u4E0A\u4F20\u4E2D\u6587\u4EF6\u8DEF\u5F84\u65E0\u6548\uFF0C\u8DF3\u8FC7",{index:e,originalname:d.originalname}),n.push({success:!1,filePath:h||"",originalname:d.originalname,error:"\u6587\u4EF6\u8DEF\u5F84\u65E0\u6548"});continue}try{const t=await O(l,a,d,h);n.push({success:!0,filePath:h,originalname:d.originalname,...t})}catch(t){s(R,"ERROR","\u6279\u91CF\u4E0A\u4F20\u4E2D\u5355\u4E2A\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25",{filePath:h,originalname:d.originalname,error:t.message}),n.push({success:!1,filePath:h,originalname:d.originalname,error:t.message})}}const o=n.filter(e=>e.success).length,u=n.filter(e=>!e.success).length;return s(R,"INFO","\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u5B8C\u6210",{userId:l,cId:a,totalCount:i.length,successCount:o,failCount:u,elapsedMs:Date.now()-r}),{success:!0,message:"\u6279\u91CF\u4E0A\u4F20\u5B8C\u6210",totalCount:i.length,successCount:o,failCount:u,results:n}}catch(o){throw s(R,"ERROR","\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25",{userId:l,cId:a,error:o.message,elapsedMs:Date.now()-r}),new F(`\u6279\u91CF\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25: ${o.message}`,{userId:l,cId:a,originalError:o.message})}}async function T(l,a){const i=Date.now(),c=`computer:${l}:${a}`,r=P.COMPUTER_WORKSPACE_DIR;if(!l)throw new g("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!a)throw new g("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!r)throw new F("COMPUTER_WORKSPACE_DIR \u672A\u914D\u7F6E\uFF0C\u65E0\u6CD5\u521B\u5EFA\u538B\u7F29\u5305");const R=String(l),n=String(a),o=w.join(r,R,n);if(!m.existsSync(o)){const t=`${R}_${n}.zip`;s(c,"WARN","\u5DE5\u4F5C\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u8FD4\u56DE\u7A7A\u538B\u7F29\u5305",{targetDir:o,userId:R,cId:n,zipFileName:t});const f=N("zip",{zlib:{level:9}});return f.append(null,{name:`${R}_${n}/`,type:"directory"}),f.on("warning",p=>{if(p.code==="ENOENT")s(c,"WARN","\u521B\u5EFA\u7A7A\u538B\u7F29\u5305\u65F6\u9047\u5230\u6587\u4EF6\u95EE\u9898",{message:p.message,code:p.code});else throw s(c,"ERROR","\u521B\u5EFA\u7A7A\u538B\u7F29\u5305\u65F6\u53D1\u751F\u8B66\u544A",{message:p.message,code:p.code}),p}),f.on("error",p=>{s(c,"ERROR","\u521B\u5EFA\u7A7A\u538B\u7F29\u5305\u5931\u8D25",{message:p.message})}),{archive:f,zipFileName:t}}const u=`${R}_${n}.zip`;s(c,"DEBUG","\u5F00\u59CB\u521B\u5EFA\u5DE5\u4F5C\u76EE\u5F55\u538B\u7F29\u5305",{targetDir:o,zipFileName:u});const e=N("zip",{zlib:{level:9}}),d=P.CONTENT_TRAVERSE_EXCLUDE_FILES||[],h=P.TRAVERSE_EXCLUDE_DIRS||[];return e.directory(o,`${R}_${n}`,t=>{const f=t.name||"",p=f.split(/[\/\\]/).filter(Boolean);if(p.some(y=>y.startsWith(".")))return!1;const E=p[p.length-1];if(d.includes(E)||p.some(y=>h.includes(y)))return!1;try{const y=w.join(o,f),I=m.lstatSync(y);if(I.isSymbolicLink()||I.nlink>1)return!1}catch(y){return s(c,"WARN","\u68C0\u6D4B\u94FE\u63A5\u6587\u4EF6\u65F6\u51FA\u9519\uFF0C\u8DF3\u8FC7\u8BE5\u6587\u4EF6",{filePath:f,error:y.message}),!1}return t}),e.on("warning",t=>{if(t.code==="ENOENT")s(c,"WARN","\u521B\u5EFA\u538B\u7F29\u5305\u65F6\u9047\u5230\u6587\u4EF6\u95EE\u9898",{message:t.message,code:t.code});else throw s(c,"ERROR","\u521B\u5EFA\u538B\u7F29\u5305\u65F6\u53D1\u751F\u8B66\u544A",{message:t.message,code:t.code}),t}),e.on("error",t=>{s(c,"ERROR","\u521B\u5EFA\u538B\u7F29\u5305\u5931\u8D25",{message:t.message,elapsedMs:Date.now()-i})}),e.on("end",()=>{s(c,"INFO","\u5DE5\u4F5C\u76EE\u5F55\u538B\u7F29\u5305\u521B\u5EFA\u5B8C\u6210",{targetDir:o,zipFileName:u,elapsedMs:Date.now()-i})}),{archive:e,zipFileName:u}}export{A as getFileList,k as updateFiles,O as uploadFile,b as uploadFiles,T as downloadAllFiles};
@@ -1 +1 @@
1
- import i from"fs";import n from"path";import"os";import O from"../../appConfig/index.js";import{extractZip as j}from"../common/zipUtils.js";import{ValidationError as u,SystemError as x,FileError as v}from"../error/errorHandler.js";import{log as c}from"../log/logUtils.js";async function F(t="computer"){const s=O.COMPUTER_WORKSPACE_DIR;if(!s)throw new u("COMPUTER_WORKSPACE_DIR \u914D\u7F6E\u4E0D\u5B58\u5728",{field:"COMPUTER_WORKSPACE_DIR"});return i.existsSync(s)||(await i.promises.mkdir(s,{recursive:!0}),c(t,"INFO","\u521B\u5EFA\u7528\u6237\u5DE5\u4F5C\u7A7A\u95F4\u6839\u76EE\u5F55",{workspaceRoot:s})),s}async function h(t,s){const e=n.join(t,s);if(i.existsSync(e)&&(await i.promises.lstat(e)).isDirectory())return e;const r=await i.promises.readdir(t,{withFileTypes:!0});for(const a of r){if(!a.isDirectory())continue;const p=n.join(t,a.name),f=n.join(p,s);if(i.existsSync(f)&&(await i.promises.lstat(f)).isDirectory())return f}return null}async function K(t){return h(t,"skills")}async function R(t){i.existsSync(t)&&await i.promises.rm(t,{recursive:!0,force:!0})}async function S(t,s){try{await i.promises.rename(t,s)}catch(e){if(e.code==="EXDEV"){async function r(a,p){if((await i.promises.lstat(a)).isDirectory()){await i.promises.mkdir(p,{recursive:!0});const l=await i.promises.readdir(a);for(const w of l)await r(n.join(a,w),n.join(p,w))}else await i.promises.copyFile(a,p)}await r(t,s),await i.promises.rm(t,{recursive:!0,force:!0})}else throw e}}async function N(t,s,e){const r=`computer:${t}:${s}`;if(!t)throw new u("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!s)throw new u("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});const a=await F(r),p=n.join(a,String(t),String(s),".tmp"),f=n.join(a,String(t),String(s)),l=n.join(f,".claude"),w=n.join(l,"skills"),g=n.join(l,"agents");i.existsSync(f)||await i.promises.mkdir(f,{recursive:!0}),i.existsSync(l)||await i.promises.mkdir(l,{recursive:!0}),await R(w),await R(g);const D=i.existsSync(w),E=i.existsSync(g);if(c(r,"INFO","\u5220\u9664\u65E7 skills \u548C agents \u76EE\u5F55\u5B8C\u6210",{userId:t,cId:s,targetSkillsDir:w,targetAgentsDir:g,skillsExists:D,agentsExists:E}),!e)return c(r,"INFO","\u521B\u5EFA\u5DE5\u4F5C\u7A7A\u95F4\uFF08\u65E0\u4E0A\u4F20\u6587\u4EF6\uFF0C\u65E0 skills \u548C agents\uFF09",{userId:t,cId:s,workspaceRoot:a,claudeDir:l,skillsDir:null,agentsDir:null}),{message:"\u5DE5\u4F5C\u7A7A\u95F4\u5DF2\u521B\u5EFA\uFF08\u65E0\u4E0A\u4F20\u6587\u4EF6\uFF0C\u65E0 skills \u548C agents\uFF09",workspaceRoot:a};if(!e.path)throw new u("\u4E0A\u4F20\u6587\u4EF6\u65E0\u6709\u6548\u8DEF\u5F84",{field:"file.path"});if(n.extname(e.originalname||e.filename||"").toLowerCase()!==".zip")throw new u("\u4EC5\u652F\u6301 zip \u6587\u4EF6",{field:"file",originalName:e.originalname});c(r,"INFO","\u5F00\u59CB\u5904\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6",{userId:t,cId:s,workspaceRoot:a,tempZipPath:e.path});const m=n.join(p,`skill_extract_${Date.now()}_${Math.round(Math.random()*1e6)}`);try{i.existsSync(p)||await i.promises.mkdir(p,{recursive:!0}),await i.promises.mkdir(m,{recursive:!0}),await j(e.path,m);const o=await h(m,"skills"),k=await h(m,"agents"),y=[];return o?(await S(o,w),y.push("skills"),c(r,"INFO","skills \u5DF2\u66F4\u65B0\u5230\u5DE5\u4F5C\u7A7A\u95F4",{userId:t,cId:s,workspaceRoot:a,claudeDir:l,targetSkillsDir:w})):c(r,"INFO","zip \u4E2D\u672A\u627E\u5230 skills \u76EE\u5F55\uFF0C\u8DF3\u8FC7",{userId:t,cId:s,extractRoot:m}),k?(await S(k,g),y.push("agents"),c(r,"INFO","agents \u5DF2\u66F4\u65B0\u5230\u5DE5\u4F5C\u7A7A\u95F4",{userId:t,cId:s,workspaceRoot:a,claudeDir:l,targetAgentsDir:g})):c(r,"INFO","zip \u4E2D\u672A\u627E\u5230 agents \u76EE\u5F55\uFF0C\u8DF3\u8FC7",{userId:t,cId:s,extractRoot:m}),y.length===0&&c(r,"WARN","zip \u4E2D\u672A\u627E\u5230 skills \u548C agents \u76EE\u5F55",{userId:t,cId:s,extractRoot:m}),{message:y.length>0?`\u5DE5\u4F5C\u7A7A\u95F4\u521B\u5EFA\u5B8C\u6210\uFF0C${y.join(" \u548C ")} \u5DF2\u66F4\u65B0`:"\u5DE5\u4F5C\u7A7A\u95F4\u521B\u5EFA\u5B8C\u6210\uFF08zip \u4E2D\u672A\u627E\u5230 skills \u548C agents\uFF09",workspaceRoot:a}}catch(o){throw c(r,"ERROR","\u5904\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6\u5931\u8D25",{userId:t,cId:s,error:o.message}),o instanceof u||o instanceof v||o instanceof x?o:new x(`\u521B\u5EFA\u5DE5\u4F5C\u7A7A\u95F4\u5931\u8D25: ${o.message}`,{userId:t,cId:s})}finally{try{i.existsSync(m)&&await i.promises.rm(m,{recursive:!0,force:!0})}catch(o){c(r,"WARN","\u6E05\u7406\u4E34\u65F6\u89E3\u538B\u7684 zip \u5931\u8D25",{extractRoot:m,error:o.message})}try{e&&e.path&&i.existsSync(e.path)&&await i.promises.unlink(e.path)}catch(o){c(r,"WARN","\u6E05\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6\u5931\u8D25",{tempZipPath:e?.path,error:o.message})}}}export{N as createWorkspace};
1
+ import i from"fs";import a from"path";import P from"../../appConfig/index.js";import{extractZip as _}from"../common/zipUtils.js";import{ValidationError as S,SystemError as j,FileError as A}from"../error/errorHandler.js";import{log as n}from"../log/logUtils.js";async function W(t="computer"){const s=P.COMPUTER_WORKSPACE_DIR;if(!s)throw new S("COMPUTER_WORKSPACE_DIR \u914D\u7F6E\u4E0D\u5B58\u5728",{field:"COMPUTER_WORKSPACE_DIR"});return i.existsSync(s)||(await i.promises.mkdir(s,{recursive:!0}),n(t,"INFO","\u521B\u5EFA\u7528\u6237\u5DE5\u4F5C\u7A7A\u95F4\u6839\u76EE\u5F55",{workspaceRoot:s})),s}async function E(t,s){const r=a.join(t,s);if(i.existsSync(r)&&(await i.promises.lstat(r)).isDirectory())return r;const y=await i.promises.readdir(t,{withFileTypes:!0});for(const e of y){if(!e.isDirectory())continue;const k=a.join(t,e.name),u=a.join(k,s);if(i.existsSync(u)&&(await i.promises.lstat(u)).isDirectory())return u}return null}async function b(t){return E(t,"skills")}const $=".dynamic_add.lock";function T(t){const s=a.join(t,$);return i.existsSync(s)&&i.statSync(s).isFile()}async function x(t){i.existsSync(t)&&await i.promises.rm(t,{recursive:!0,force:!0})}async function R(t,s){try{await i.promises.rename(t,s)}catch(r){if(r.code==="EXDEV"){async function y(e,k){if((await i.promises.lstat(e)).isDirectory()){await i.promises.mkdir(k,{recursive:!0});const D=await i.promises.readdir(e);for(const h of D)await y(a.join(e,h),a.join(k,h))}else await i.promises.copyFile(e,k)}await y(t,s),await i.promises.rm(t,{recursive:!0,force:!0})}else throw r}}async function C(t,s,r){const y=Date.now(),e=`computer:${t}:${s}`;if(!t)throw new S("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!s)throw new S("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});const k=await W(e),u=a.join(k,String(t),String(s),".tmp"),D=a.join(k,String(t),String(s)),h=a.join(D,".claude"),w=a.join(h,"skills"),d=a.join(h,"agents");i.existsSync(D)||await i.promises.mkdir(D,{recursive:!0}),i.existsSync(h)||await i.promises.mkdir(h,{recursive:!0});const f=a.join(u,`preserved_skills_${Date.now()}_${Math.round(Math.random()*1e6)}`);if(i.existsSync(w)){const l=(await i.promises.readdir(w,{withFileTypes:!0})).filter(p=>p.isDirectory()&&T(a.join(w,p.name)));if(l.length>0){await i.promises.mkdir(f,{recursive:!0});for(const p of l){const g=a.join(w,p.name),z=a.join(f,p.name);await R(g,z)}n(e,"INFO","\u4FDD\u7559\u542B .dynamic_add.lock \u7684 skill",{preserved:l.map(p=>p.name)})}}if(await x(w),await x(d),i.existsSync(f)){await i.promises.mkdir(w,{recursive:!0});const o=await i.promises.readdir(f,{withFileTypes:!0});for(const l of o)if(l.isDirectory()){const p=a.join(f,l.name),g=a.join(w,l.name);await R(p,g)}await x(f)}const m=i.existsSync(w),F=i.existsSync(d);if(n(e,"INFO","\u5220\u9664\u65E7 skills \u548C agents \u76EE\u5F55\u5B8C\u6210",{userId:t,cId:s,targetSkillsDir:w,targetAgentsDir:d,skillsExists:m,agentsExists:F}),!r)return n(e,"INFO","\u521B\u5EFA\u5DE5\u4F5C\u7A7A\u95F4\uFF08\u65E0\u4E0A\u4F20\u6587\u4EF6\uFF0C\u65E0 skills \u548C agents\uFF09",{userId:t,cId:s,workspaceRoot:k,claudeDir:h,skillsDir:null,agentsDir:null,elapsedMs:Date.now()-y}),{message:"\u5DE5\u4F5C\u7A7A\u95F4\u5DF2\u521B\u5EFA\uFF08\u65E0\u4E0A\u4F20\u6587\u4EF6\uFF0C\u65E0 skills \u548C agents\uFF09",workspaceRoot:k};if(!r.path)throw new S("\u4E0A\u4F20\u6587\u4EF6\u65E0\u6709\u6548\u8DEF\u5F84",{field:"file.path"});if(a.extname(r.originalname||r.filename||"").toLowerCase()!==".zip")throw new S("\u4EC5\u652F\u6301 zip \u6587\u4EF6",{field:"file",originalName:r.originalname});n(e,"DEBUG","\u5F00\u59CB\u5904\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6",{userId:t,cId:s,workspaceRoot:k,tempZipPath:r.path});const c=a.join(u,`skill_extract_${Date.now()}_${Math.round(Math.random()*1e6)}`);try{i.existsSync(u)||await i.promises.mkdir(u,{recursive:!0}),await i.promises.mkdir(c,{recursive:!0}),n(e,"DEBUG","\u5F00\u59CB\u89E3\u538B zip \u6587\u4EF6",{extractRoot:c}),await _(r.path,c),n(e,"DEBUG","zip \u6587\u4EF6\u89E3\u538B\u5B8C\u6210",{extractRoot:c});const o=await E(c,"skills"),l=await E(c,"agents"),p=[];if(o){await i.promises.mkdir(w,{recursive:!0});const z=await i.promises.readdir(o,{withFileTypes:!0});for(const N of z){if(!N.isDirectory())continue;const M=a.join(o,N.name),O=a.join(w,N.name);i.existsSync(O)&&await x(O),await R(M,O)}p.push("skills"),n(e,"INFO","skills \u5DF2\u66F4\u65B0\u5230\u5DE5\u4F5C\u7A7A\u95F4",{userId:t,cId:s,workspaceRoot:k,claudeDir:h,targetSkillsDir:w})}else n(e,"INFO","zip \u4E2D\u672A\u627E\u5230 skills \u76EE\u5F55\uFF0C\u8DF3\u8FC7",{userId:t,cId:s,extractRoot:c});l?(await R(l,d),p.push("agents"),n(e,"INFO","agents \u5DF2\u66F4\u65B0\u5230\u5DE5\u4F5C\u7A7A\u95F4",{userId:t,cId:s,workspaceRoot:k,claudeDir:h,targetAgentsDir:d})):n(e,"INFO","zip \u4E2D\u672A\u627E\u5230 agents \u76EE\u5F55\uFF0C\u8DF3\u8FC7",{userId:t,cId:s,extractRoot:c}),p.length===0&&n(e,"WARN","zip \u4E2D\u672A\u627E\u5230 skills \u548C agents \u76EE\u5F55",{userId:t,cId:s,extractRoot:c});const g=p.length>0?`\u5DE5\u4F5C\u7A7A\u95F4\u521B\u5EFA\u5B8C\u6210\uFF0C${p.join(" \u548C ")} \u5DF2\u66F4\u65B0`:"\u5DE5\u4F5C\u7A7A\u95F4\u521B\u5EFA\u5B8C\u6210\uFF08zip \u4E2D\u672A\u627E\u5230 skills \u548C agents\uFF09";return n(e,"INFO",g,{userId:t,cId:s,updatedDirs:p,elapsedMs:Date.now()-y}),{message:g,workspaceRoot:k}}catch(o){throw n(e,"ERROR","\u5904\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6\u5931\u8D25",{userId:t,cId:s,error:o.message,elapsedMs:Date.now()-y}),o instanceof S||o instanceof A||o instanceof j?o:new j(`\u521B\u5EFA\u5DE5\u4F5C\u7A7A\u95F4\u5931\u8D25: ${o.message}`,{userId:t,cId:s})}finally{try{i.existsSync(c)&&await i.promises.rm(c,{recursive:!0,force:!0})}catch(o){n(e,"WARN","\u6E05\u7406\u4E34\u65F6\u89E3\u538B\u7684 zip \u5931\u8D25",{extractRoot:c,error:o.message})}try{r&&r.path&&i.existsSync(r.path)&&await i.promises.unlink(r.path)}catch(o){n(e,"WARN","\u6E05\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6\u5931\u8D25",{tempZipPath:r?.path,error:o.message})}}}async function U(t,s,r){const y=Date.now(),e=`computer:${t}:${s}`;if(!t)throw new S("userId \u4E0D\u80FD\u4E3A\u7A7A",{field:"userId"});if(!s)throw new S("cId \u4E0D\u80FD\u4E3A\u7A7A",{field:"cId"});if(!r||!r.path)throw new S("\u4E0A\u4F20\u6587\u4EF6\u65E0\u6709\u6548\u8DEF\u5F84",{field:"file.path"});if(a.extname(r.originalname||r.filename||"").toLowerCase()!==".zip")throw new S("\u4EC5\u652F\u6301 zip \u6587\u4EF6",{field:"file",originalName:r?.originalname});const u=await W(e),D=a.join(u,String(t),String(s),".tmp"),h=a.join(u,String(t),String(s)),w=a.join(h,".claude"),d=a.join(w,"skills"),f=a.join(D,`skill_push_${Date.now()}_${Math.round(Math.random()*1e6)}`);try{i.existsSync(h)||await i.promises.mkdir(h,{recursive:!0}),i.existsSync(w)||await i.promises.mkdir(w,{recursive:!0}),i.existsSync(d)||await i.promises.mkdir(d,{recursive:!0}),i.existsSync(D)||await i.promises.mkdir(D,{recursive:!0}),await i.promises.mkdir(f,{recursive:!0}),n(e,"DEBUG","\u5F00\u59CB\u89E3\u538B skill zip \u6587\u4EF6",{extractRoot:f}),await _(r.path,f),n(e,"DEBUG","skill zip \u6587\u4EF6\u89E3\u538B\u5B8C\u6210",{extractRoot:f});const m=await E(f,"skills");if(!m)return n(e,"WARN","zip \u4E2D\u672A\u627E\u5230 skills \u76EE\u5F55",{userId:t,cId:s,extractRoot:f}),{message:"zip \u4E2D\u672A\u627E\u5230 skills \u76EE\u5F55",workspaceRoot:u,updatedSkills:[]};const v=(await i.promises.readdir(m,{withFileTypes:!0})).filter(l=>l.isDirectory()&&!l.name.startsWith("."));if(v.length===0)return n(e,"WARN","zip \u7684 skills \u76EE\u5F55\u4E0B\u672A\u627E\u5230 skill \u5B50\u76EE\u5F55",{userId:t,cId:s,skillsDir:m}),{message:"zip \u7684 skills \u76EE\u5F55\u4E0B\u672A\u627E\u5230 skill \u5B50\u76EE\u5F55",workspaceRoot:u,updatedSkills:[]};const c=[];for(const l of v){const p=a.join(m,l.name),g=a.join(d,l.name);i.existsSync(g)&&await x(g),await R(p,g),c.push(l.name),n(e,"INFO","skill \u5DF2\u63A8\u9001\u5230\u5DE5\u4F5C\u7A7A\u95F4",{userId:t,cId:s,skillName:l.name,destSkillPath:g})}const o=c.length>0?`\u5DF2\u63A8\u9001 ${c.length} \u4E2A skill: ${c.join(", ")}`:"zip \u4E2D\u672A\u627E\u5230\u6709\u6548 skill \u76EE\u5F55";return n(e,"INFO",o,{userId:t,cId:s,updatedSkills:c,elapsedMs:Date.now()-y}),{message:o,workspaceRoot:u,updatedSkills:c}}catch(m){throw n(e,"ERROR","\u63A8\u9001 skill \u5230\u5DE5\u4F5C\u7A7A\u95F4\u5931\u8D25",{userId:t,cId:s,error:m.message,elapsedMs:Date.now()-y}),m instanceof S||m instanceof A||m instanceof j?m:new j(`\u63A8\u9001 skill \u5931\u8D25: ${m.message}`,{userId:t,cId:s})}finally{try{i.existsSync(f)&&await i.promises.rm(f,{recursive:!0,force:!0})}catch(m){n(e,"WARN","\u6E05\u7406\u4E34\u65F6\u89E3\u538B\u76EE\u5F55\u5931\u8D25",{extractRoot:f,error:m.message})}try{r&&r.path&&i.existsSync(r.path)&&await i.promises.unlink(r.path)}catch(m){n(e,"WARN","\u6E05\u7406\u4E0A\u4F20\u7684 zip \u6587\u4EF6\u5931\u8D25",{tempZipPath:r?.path,error:m.message})}}}export{C as createWorkspace,U as pushSkillsToWorkspace};
@@ -1 +1 @@
1
- import a from"fs";import m from"path";import A from"archiver";import C from"yauzl";import g from"../../appConfig/index.js";import{log as R}from"../log/logUtils.js";import{FileError as y}from"../error/errorHandler.js";import{sanitizeSensitivePaths as _}from"../common/sensitiveUtils.js";async function D(s,i){const r=new Set(g.TRAVERSE_EXCLUDE_DIRS||[]),l=new Set(g.BACKUP_TRAVERSE_EXCLUDE_FILES||[]),c=await a.promises.readdir(s,{withFileTypes:!0});for(const f of c){const o=m.join(s,f.name),n=m.join(i,f.name);if(f.isDirectory()){if(r.has(f.name))continue;await a.promises.mkdir(n,{recursive:!0}),await D(o,n)}else if(f.isFile()){if(l.has(f.name))continue;await a.promises.mkdir(m.dirname(n),{recursive:!0}),await a.promises.copyFile(o,n)}}}async function L(s,i,r){const l=m.join(g.UPLOAD_PROJECT_DIR,s);a.existsSync(l)||a.mkdirSync(l,{recursive:!0});const c=m.join(l,`backup_temp_${Date.now()}`);await a.promises.mkdir(c,{recursive:!0});try{return await D(i,c),await a.promises.mkdir(m.dirname(r),{recursive:!0}),await new Promise((f,o)=>{const n=a.createWriteStream(r),t=A("zip",{zlib:{level:9}});n.on("close",()=>f()),n.on("error",e=>o(new y("\u5907\u4EFDzip\u538B\u7F29\u5931\u8D25",{projectId:s,projectPath:i,outZipPath:r,originalError:e&&e.message}))),t.on("warning",e=>{e&&e.code==="ENOENT"?R(s,"WARN",`\u538B\u7F29\u544A\u8B66: ${e.message}`,{projectId:s,outZipPath:r}):e&&o(new y("\u5907\u4EFDzip\u538B\u7F29\u5931\u8D25",{projectId:s,projectPath:i,outZipPath:r,originalError:e&&e.message}))}),t.on("error",e=>o(new y("\u5907\u4EFDzip\u538B\u7F29\u5931\u8D25",{projectId:s,projectPath:i,outZipPath:r,originalError:e&&e.message}))),t.pipe(n),t.directory(c+"/",!1),t.finalize()}),R(s,"INFO",`\u9879\u76EE\u5DF2\u5907\u4EFD: ${r}`,{projectId:s,outZipPath:r}),r}finally{try{await a.promises.rm(c,{recursive:!0,force:!0})}catch{}}}async function h(s,i,r){const l=new Set(g.TRAVERSE_EXCLUDE_DIRS||[]),c=new Set(g.BACKUP_TRAVERSE_EXCLUDE_FILES||[]),f=await a.promises.readdir(i,{withFileTypes:!0});for(const o of f){const n=m.join(i,o.name);if(!(o.isDirectory()&&l.has(o.name))&&!(o.isFile()&&c.has(o.name)))try{await a.promises.rm(n,{recursive:!0,force:!0})}catch{}}await new Promise((o,n)=>{C.open(r,{lazyEntries:!0},(t,e)=>{if(t||!e)return n(new y("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:s,projectPath:i,zipPath:r,originalError:t&&t.message}));const v=m.resolve(i);e.readEntry(),e.on("entry",u=>{const T=m.normalize(u.fileName).replace(/^([/\\]+)+/,""),k=m.join(i,T),S=m.resolve(k);if(!S.startsWith(v+m.sep)&&S!==v){e.readEntry();return}if(/\\$|\/$/.test(u.fileName)||u.fileName.endsWith("/")){a.promises.mkdir(S,{recursive:!0}).then(()=>e.readEntry()).catch(w=>{e.close(),n(new y("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:s,projectPath:i,zipPath:r,originalError:w&&w.message?_(w.message):w&&w.message}))});return}e.openReadStream(u,(w,N)=>{if(w||!N)return e.close(),n(new y("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:s,projectPath:i,zipPath:r,originalError:w&&w.message}));a.promises.mkdir(m.dirname(S),{recursive:!0}).then(()=>{const E=a.createWriteStream(S);N.pipe(E),E.on("close",()=>e.readEntry()),E.on("error",d=>{e.close(),n(new y("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:s,projectPath:i,zipPath:r,originalError:d&&d.message?_(d.message):d&&d.message}))})}).catch(E=>{e.close(),n(new y("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:s,projectPath:i,zipPath:r,originalError:E&&E.message?_(E.message):E&&E.message}))})})}),e.on("end",()=>{e.close(),o()}),e.on("error",u=>{e.close(),n(new y("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:s,projectPath:i,zipPath:r,originalError:u&&u.message?_(u.message):u&&u.message}))})})}),R(s,"INFO",`\u9879\u76EE\u5DF2\u4ECE\u5907\u4EFD\u6062\u590D: ${r}`,{projectId:s,projectPath:i,zipPath:r})}async function x(s,i,r){const l=new Set(r||[]),c=new Set((g.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).map(o=>String(o).trim()));async function f(o){const n=await a.promises.readdir(o,{withFileTypes:!0});for(const t of n){const e=m.join(o,t.name),v=m.relative(s,e);if(t.isDirectory()){if(l.has(t.name))continue;await f(e)}else if(t.isFile()){if(t.name.startsWith(".")||c.has(t.name))continue;if(!i.has(v))try{await a.promises.unlink(e)}catch{}}}}await f(s)}async function U(s,i){const r=new Set(i||[]);async function l(c){const f=await a.promises.readdir(c,{withFileTypes:!0});for(const n of f){const t=m.join(c,n.name);if(n.isDirectory()){if(r.has(n.name))continue;await l(t)}}if((await a.promises.readdir(c)).length===0&&m.resolve(c)!==m.resolve(s))try{await a.promises.rmdir(c)}catch{}}await l(s)}export{D as copyDirectoryFiltered,L as backupProjectToZip,h as restoreProjectFromZip,x as pruneMissingFiles,U as removeEmptyDirectories};
1
+ import t from"fs";import m from"path";import A from"archiver";import C from"yauzl";import d from"../../appConfig/index.js";import{log as S}from"../log/logUtils.js";import{FileError as g}from"../error/errorHandler.js";import{sanitizeSensitivePaths as _}from"../common/sensitiveUtils.js";async function T(e,r){const n=new Set(d.TRAVERSE_EXCLUDE_DIRS||[]),u=new Set(d.BACKUP_TRAVERSE_EXCLUDE_FILES||[]),c=await t.promises.readdir(e,{withFileTypes:!0});for(const o of c){const f=m.join(e,o.name),i=m.join(r,o.name);if(o.isDirectory()){if(n.has(o.name))continue;await t.promises.mkdir(i,{recursive:!0}),await T(f,i)}else if(o.isFile()){if(u.has(o.name))continue;await t.promises.mkdir(m.dirname(i),{recursive:!0}),await t.promises.copyFile(f,i)}}}async function L(e,r,n){const u=Date.now(),c=m.join(d.UPLOAD_PROJECT_DIR,e);t.existsSync(c)||t.mkdirSync(c,{recursive:!0});const o=m.join(c,`backup_temp_${Date.now()}`);await t.promises.mkdir(o,{recursive:!0});try{return S(e,"DEBUG","\u5F00\u59CB\u590D\u5236\u9879\u76EE\u6587\u4EF6\u5230\u4E34\u65F6\u76EE\u5F55",{projectPath:r,tempDir:o}),await T(r,o),S(e,"DEBUG","\u9879\u76EE\u6587\u4EF6\u590D\u5236\u5B8C\u6210\uFF0C\u5F00\u59CB\u538B\u7F29",{tempDir:o,outZipPath:n}),await t.promises.mkdir(m.dirname(n),{recursive:!0}),await new Promise((f,i)=>{const a=t.createWriteStream(n),l=A("zip",{zlib:{level:9}});a.on("close",()=>f()),a.on("error",s=>i(new g("\u5907\u4EFDzip\u538B\u7F29\u5931\u8D25",{projectId:e,projectPath:r,outZipPath:n,originalError:s&&s.message}))),l.on("warning",s=>{s&&s.code==="ENOENT"?S(e,"WARN",`\u538B\u7F29\u544A\u8B66: ${s.message}`,{projectId:e,outZipPath:n}):s&&i(new g("\u5907\u4EFDzip\u538B\u7F29\u5931\u8D25",{projectId:e,projectPath:r,outZipPath:n,originalError:s&&s.message}))}),l.on("error",s=>i(new g("\u5907\u4EFDzip\u538B\u7F29\u5931\u8D25",{projectId:e,projectPath:r,outZipPath:n,originalError:s&&s.message}))),l.pipe(a),l.directory(o+"/",!1),l.finalize()}),S(e,"INFO",`\u9879\u76EE\u5DF2\u5907\u4EFD: ${n}`,{projectId:e,outZipPath:n,elapsedMs:Date.now()-u}),n}finally{try{await t.promises.rm(o,{recursive:!0,force:!0})}catch{}}}async function x(e,r,n){const u=Date.now(),c=new Set(d.TRAVERSE_EXCLUDE_DIRS||[]),o=new Set(d.BACKUP_TRAVERSE_EXCLUDE_FILES||[]);S(e,"DEBUG","\u5F00\u59CB\u6E05\u7A7A\u9879\u76EE\u76EE\u5F55\uFF08\u4FDD\u7559\u6392\u9664\u9879\uFF09",{projectPath:r});const f=await t.promises.readdir(r,{withFileTypes:!0});for(const i of f){const a=m.join(r,i.name);if(!(i.isDirectory()&&c.has(i.name))&&!(i.isFile()&&o.has(i.name)))try{await t.promises.rm(a,{recursive:!0,force:!0})}catch{}}S(e,"DEBUG","\u5F00\u59CB\u4ECE zip \u6062\u590D\u9879\u76EE\u6587\u4EF6",{zipPath:n,projectPath:r}),await new Promise((i,a)=>{C.open(n,{lazyEntries:!0},(l,s)=>{if(l||!s)return a(new g("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:e,projectPath:r,zipPath:n,originalError:l&&l.message}));const R=m.resolve(r);s.readEntry(),s.on("entry",w=>{const U=m.normalize(w.fileName).replace(/^([/\\]+)+/,""),k=m.join(r,U),v=m.resolve(k);if(!v.startsWith(R+m.sep)&&v!==R){s.readEntry();return}if(/\\$|\/$/.test(w.fileName)||w.fileName.endsWith("/")){t.promises.mkdir(v,{recursive:!0}).then(()=>s.readEntry()).catch(E=>{s.close(),a(new g("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:e,projectPath:r,zipPath:n,originalError:E&&E.message?_(E.message):E&&E.message}))});return}s.openReadStream(w,(E,N)=>{if(E||!N)return s.close(),a(new g("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:e,projectPath:r,zipPath:n,originalError:E&&E.message}));t.promises.mkdir(m.dirname(v),{recursive:!0}).then(()=>{const y=t.createWriteStream(v);N.pipe(y),y.on("close",()=>s.readEntry()),y.on("error",D=>{s.close(),a(new g("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:e,projectPath:r,zipPath:n,originalError:D&&D.message?_(D.message):D&&D.message}))})}).catch(y=>{s.close(),a(new g("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:e,projectPath:r,zipPath:n,originalError:y&&y.message?_(y.message):y&&y.message}))})})}),s.on("end",()=>{s.close(),i()}),s.on("error",w=>{s.close(),a(new g("\u56DE\u6EDA\u89E3\u538B\u5931\u8D25",{projectId:e,projectPath:r,zipPath:n,originalError:w&&w.message?_(w.message):w&&w.message}))})})}),S(e,"INFO",`\u9879\u76EE\u5DF2\u4ECE\u5907\u4EFD\u6062\u590D: ${n}`,{projectId:e,projectPath:r,zipPath:n,elapsedMs:Date.now()-u})}async function B(e,r,n){const u=new Set(n||[]),c=new Set((d.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).map(f=>String(f).trim()));async function o(f){const i=await t.promises.readdir(f,{withFileTypes:!0});for(const a of i){const l=m.join(f,a.name),s=m.relative(e,l);if(a.isDirectory()){if(u.has(a.name))continue;await o(l)}else if(a.isFile()){if(a.name.startsWith(".")||c.has(a.name))continue;if(!r.has(s))try{await t.promises.unlink(l)}catch{}}}}await o(e)}async function O(e,r){const n=new Set(r||[]);async function u(c){const o=await t.promises.readdir(c,{withFileTypes:!0});for(const i of o){const a=m.join(c,i.name);if(i.isDirectory()){if(n.has(i.name))continue;await u(a)}}if((await t.promises.readdir(c)).length===0&&m.resolve(c)!==m.resolve(e))try{await t.promises.rmdir(c)}catch{}}await u(e)}export{T as copyDirectoryFiltered,L as backupProjectToZip,x as restoreProjectFromZip,B as pruneMissingFiles,O as removeEmptyDirectories};
@@ -1 +1 @@
1
- import c from"fs";import m from"path";import g from"../../appConfig/index.js";import{log as l}from"../log/logUtils.js";import{ValidationError as D,SystemError as N,ResourceError as $}from"../error/errorHandler.js";import{extractZip as x}from"../common/zipUtils.js";import{getFrameworkInfo as O}from"./frameworkDetectorUtils.js";async function _(e){try{const t=c.readFileSync(e);if(t.includes(0))return!0;const o=t.toString("utf-8");for(let s=0;s<o.length;s++){const r=o.charCodeAt(s);if(r<32&&r!==9&&r!==10&&r!==13)return!0}return!1}catch{return!1}}function R(e){const t=m.extname(e).toLowerCase();return[".jpg",".jpeg",".png",".gif",".bmp",".webp",".svg"].includes(t)}async function F(e,t,o,s){const r=[],a=await c.promises.readdir(e,{withFileTypes:!0});a.sort((n,i)=>n.isDirectory()&&!i.isDirectory()?-1:!n.isDirectory()&&i.isDirectory()?1:n.name.toLowerCase().localeCompare(i.name.toLowerCase()));for(const n of a){const i=m.join(e,n.name);if(!(n.name.startsWith(".")||(g.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(n.name))&&!(n.isDirectory()&&g.TRAVERSE_EXCLUDE_DIRS.includes(n.name)))if(n.isDirectory()){const f=await F(i,t,o,s);r.push(...f)}else try{const f=await c.promises.stat(i),y=t||m.join(g.PROJECT_SOURCE_DIR,o),w=m.relative(y,i),C=await _(i),S=`${s}/${w}`,E={name:w,binary:C,sizeExceeded:f.size>g.MAX_INLINE_FILE_SIZE_BYTES,contents:"",fileProxyUrl:S};if(!E.sizeExceeded)if(E.binary){if(R(i)){const h=c.readFileSync(i);E.contents=h.toString("base64")}}else E.contents=c.readFileSync(i,"utf-8");r.push(E)}catch(f){l(o,"WARN",`\u5904\u7406\u6587\u4EF6\u5931\u8D25: ${i} - ${f.message}`,{filePath:i,error:f.message})}}return r}async function p(e,t,o){const s=m.basename(e);try{l(s,"INFO","\u5F00\u59CB\u83B7\u53D6\u9879\u76EE\u5185\u5BB9",{projectPath:e,command:t});const r=await F(e,null,s,o);let a=r;t!=="cpage_config"&&(a=r.filter(u=>u.name!=="cpage_config.json"));const n=O(e),i={files:a,...n};return l(s,"INFO",`\u9879\u76EE\u5185\u5BB9\u83B7\u53D6\u5B8C\u6210\uFF0C\u5171${a.length}\u4E2A\u6587\u4EF6`,{projectPath:e,fileCount:a.length,command:t}),i}catch(r){throw l(s,"ERROR",`\u83B7\u53D6\u9879\u76EE\u5185\u5BB9\u5931\u8D25: ${r.message}`,{projectPath:e,originalError:r.message}),new N(`\u83B7\u53D6\u9879\u76EE\u5185\u5BB9\u5931\u8D25: ${r.message}`,{projectPath:e,originalError:r.message})}}async function d(e,t,o,s){const r=Number(t);if(!Number.isFinite(r))throw new D("\u4EE3\u7801\u7248\u672C\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const a=m.join(g.UPLOAD_PROJECT_DIR,e),n=m.join(a,`${e}-v${r}.zip`);if(!c.existsSync(n))throw new $(`\u7248\u672C ${r} \u7684\u5907\u4EFD\u6587\u4EF6\u4E0D\u5B58\u5728`,{projectId:e,codeVersion:r,backupZipPath:n});const i=m.join(g.PROJECT_SOURCE_DIR,"_his",e);try{c.existsSync(i)&&await c.promises.rm(i,{recursive:!0,force:!0}),await c.promises.mkdir(i,{recursive:!0}),l(e,"INFO",`\u5F00\u59CB\u89E3\u538B\u7248\u672C ${r} \u7684\u5907\u4EFD\u6587\u4EF6`,{projectId:e,codeVersion:r,backupZipPath:n,tempExtractDir:i}),await x(n,i),l(e,"INFO",`\u7248\u672C ${r} \u5907\u4EFD\u6587\u4EF6\u89E3\u538B\u5B8C\u6210`,{projectId:e,codeVersion:r,tempExtractDir:i});const u=await F(i,i,e,s);let f=u;o!=="cpage_config"&&(f=u.filter(w=>w.name!=="cpage_config.json"));const y={files:f};return l(e,"INFO",`\u7248\u672C ${r} \u9879\u76EE\u5185\u5BB9\u83B7\u53D6\u5B8C\u6210`,{projectId:e,codeVersion:r,fileCount:y.files?y.files.length:0,command:o}),y}finally{try{c.existsSync(i)&&(await c.promises.rm(i,{recursive:!0,force:!0}),l(e,"INFO",`\u4E34\u65F6\u76EE\u5F55\u5DF2\u6E05\u7406: ${i}`,{projectId:e,codeVersion:r}))}catch(u){l(e,"WARN",`\u6E05\u7406\u4E34\u65F6\u76EE\u5F55\u5931\u8D25: ${u.message}`,{projectId:e,codeVersion:r,tempExtractDir:i,error:u.message})}}}export{p as getProjectContent,d as getProjectContentByVersion,F as traverseDirectory,_ as isBinaryFile,R as isImageFile};
1
+ import u from"fs";import m from"path";import g from"../../appConfig/index.js";import{log as c}from"../log/logUtils.js";import{ValidationError as $,SystemError as h,ResourceError as x}from"../error/errorHandler.js";import{extractZip as N}from"../common/zipUtils.js";import{getFrameworkInfo as O}from"./frameworkDetectorUtils.js";async function _(e){try{const o=u.readFileSync(e);if(o.includes(0))return!0;const f=o.toString("utf-8");for(let l=0;l<f.length;l++){const n=f.charCodeAt(l);if(n<32&&n!==9&&n!==10&&n!==13)return!0}return!1}catch{return!1}}function C(e){const o=m.extname(e).toLowerCase();return[".jpg",".jpeg",".png",".gif",".bmp",".webp",".svg"].includes(o)}async function D(e,o,f,l){const n=[],i=await u.promises.readdir(e,{withFileTypes:!0});i.sort((s,r)=>s.isDirectory()&&!r.isDirectory()?-1:!s.isDirectory()&&r.isDirectory()?1:s.name.toLowerCase().localeCompare(r.name.toLowerCase()));for(const s of i){const r=m.join(e,s.name);if(!(s.name.startsWith(".")||(g.CONTENT_TRAVERSE_EXCLUDE_FILES||[]).includes(s.name))&&!(s.isDirectory()&&g.TRAVERSE_EXCLUDE_DIRS.includes(s.name)))if(s.isDirectory()){const a=await D(r,o,f,l);n.push(...a)}else try{const a=await u.promises.stat(r),w=o||m.join(g.PROJECT_SOURCE_DIR,f),E=m.relative(w,r),F=await _(r),R=`${l}/${E}`,y={name:E,binary:F,sizeExceeded:a.size>g.MAX_INLINE_FILE_SIZE_BYTES,contents:"",fileProxyUrl:R};if(!y.sizeExceeded)if(y.binary){if(C(r)){const S=u.readFileSync(r);y.contents=S.toString("base64")}}else y.contents=u.readFileSync(r,"utf-8");n.push(y)}catch(a){c(f,"WARN",`\u5904\u7406\u6587\u4EF6\u5931\u8D25: ${r} - ${a.message}`,{filePath:r,error:a.message})}}return n}async function U(e,o,f){const l=Date.now(),n=m.basename(e);try{c(n,"INFO","\u5F00\u59CB\u83B7\u53D6\u9879\u76EE\u5185\u5BB9",{projectPath:e,command:o}),c(n,"DEBUG","\u5F00\u59CB\u904D\u5386\u9879\u76EE\u76EE\u5F55",{projectPath:e});const i=await D(e,null,n,f);c(n,"DEBUG","\u9879\u76EE\u76EE\u5F55\u904D\u5386\u5B8C\u6210",{projectPath:e,fileCount:i.length});let s=i;o!=="cpage_config"&&(s=i.filter(a=>a.name!=="cpage_config.json")),c(n,"DEBUG","\u5F00\u59CB\u68C0\u6D4B\u6846\u67B6\u4FE1\u606F",{projectPath:e});const r=O(e),t={files:s,...r};return c(n,"INFO",`\u9879\u76EE\u5185\u5BB9\u83B7\u53D6\u5B8C\u6210\uFF0C\u5171${s.length}\u4E2A\u6587\u4EF6`,{projectPath:e,fileCount:s.length,command:o,elapsedMs:Date.now()-l}),t}catch(i){throw c(n,"ERROR",`\u83B7\u53D6\u9879\u76EE\u5185\u5BB9\u5931\u8D25: ${i.message}`,{projectPath:e,originalError:i.message,elapsedMs:Date.now()-l}),new h(`\u83B7\u53D6\u9879\u76EE\u5185\u5BB9\u5931\u8D25: ${i.message}`,{projectPath:e,originalError:i.message})}}async function T(e,o,f,l){const n=Date.now(),i=Number(o);if(!Number.isFinite(i))throw new $("\u4EE3\u7801\u7248\u672C\u5FC5\u987B\u662F\u6570\u5B57",{field:"codeVersion"});const s=m.join(g.UPLOAD_PROJECT_DIR,e),r=m.join(s,`${e}-v${i}.zip`);if(!u.existsSync(r))throw new x(`\u7248\u672C ${i} \u7684\u5907\u4EFD\u6587\u4EF6\u4E0D\u5B58\u5728`,{projectId:e,codeVersion:i,backupZipPath:r});const t=m.join(g.PROJECT_SOURCE_DIR,"_his",e);try{u.existsSync(t)&&await u.promises.rm(t,{recursive:!0,force:!0}),await u.promises.mkdir(t,{recursive:!0}),c(e,"INFO",`\u5F00\u59CB\u89E3\u538B\u7248\u672C ${i} \u7684\u5907\u4EFD\u6587\u4EF6`,{projectId:e,codeVersion:i,backupZipPath:r,tempExtractDir:t}),c(e,"DEBUG","\u5F00\u59CB\u89E3\u538B\u7248\u672C\u5907\u4EFD\u6587\u4EF6",{projectId:e,backupZipPath:r,tempExtractDir:t}),await N(r,t),c(e,"DEBUG",`\u7248\u672C ${i} \u5907\u4EFD\u6587\u4EF6\u89E3\u538B\u5B8C\u6210`,{projectId:e,codeVersion:i,tempExtractDir:t}),c(e,"DEBUG","\u5F00\u59CB\u904D\u5386\u7248\u672C\u76EE\u5F55",{projectId:e,tempExtractDir:t});const a=await D(t,t,e,l);let w=a;f!=="cpage_config"&&(w=a.filter(F=>F.name!=="cpage_config.json"));const E={files:w};return c(e,"INFO",`\u7248\u672C ${i} \u9879\u76EE\u5185\u5BB9\u83B7\u53D6\u5B8C\u6210`,{projectId:e,codeVersion:i,fileCount:E.files?E.files.length:0,command:f,elapsedMs:Date.now()-n}),E}finally{try{u.existsSync(t)&&(await u.promises.rm(t,{recursive:!0,force:!0}),c(e,"INFO",`\u4E34\u65F6\u76EE\u5F55\u5DF2\u6E05\u7406: ${t}`,{projectId:e,codeVersion:i}))}catch(a){c(e,"WARN",`\u6E05\u7406\u4E34\u65F6\u76EE\u5F55\u5931\u8D25: ${a.message}`,{projectId:e,codeVersion:i,tempExtractDir:t,error:a.message})}}}export{U as getProjectContent,T as getProjectContentByVersion,D as traverseDirectory,_ as isBinaryFile,C 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.2.0",
4
+ "version": "1.2.2",
5
5
  "description": "跨平台的文件服务部署工具,支持 start/stop/restart 命令行操作",
6
6
  "type": "module",
7
7
  "main": "index.js",
@@ -19,32 +19,6 @@
19
19
  "linux",
20
20
  "win32"
21
21
  ],
22
- "scripts": {
23
- "start": "nodemon src/server.js",
24
- "dev": "nodemon scripts/start-dev.js",
25
- "test": "nodemon scripts/start-test.js",
26
- "prod": "node scripts/start-prod.js",
27
- "test:run": "NODE_OPTIONS='--experimental-vm-modules' jest --coverage",
28
- "test:watch": "jest --watch",
29
- "test:unit": "jest --testPathPattern=tests/unit",
30
- "test:integration": "jest --testPathPattern=tests/integration",
31
- "start:test": "nodemon scripts/start-test.js",
32
- "pnpm:check:dev": "NODE_ENV=development node scripts/pnpm-check-wrapper.js",
33
- "pnpm:check:prod": "NODE_ENV=production node scripts/pnpm-check-wrapper.js",
34
- "pnpm:check:test": "NODE_ENV=test node scripts/pnpm-check-wrapper.js",
35
- "pnpm:prune": "pnpm store prune",
36
- "pnpm:prune:manual": "node scripts/pnpm-prune-manual.js",
37
- "pnpm:status": "pnpm store status",
38
- "cli:start": "node src/cli.js start",
39
- "cli:stop": "node src/cli.js stop",
40
- "cli:restart": "node src/cli.js restart",
41
- "cli:status": "node src/cli.js status",
42
- "cli:start:dev": "node src/cli.js start --env development",
43
- "cli:start:prod": "node src/cli.js start --env production",
44
- "cli:start:test": "node src/cli.js start --env test",
45
- "build": "node scripts/build.js",
46
- "prepublishOnly": "npm run build"
47
- },
48
22
  "author": "",
49
23
  "license": "ISC",
50
24
  "keywords": [
@@ -89,5 +63,31 @@
89
63
  "jest": "^29.7.0",
90
64
  "nodemon": "^3.1.10",
91
65
  "supertest": "^6.3.4"
66
+ },
67
+ "scripts": {
68
+ "preinstall": "npx only-allow pnpm",
69
+ "start": "nodemon src/server.js",
70
+ "dev": "nodemon scripts/start-dev.js",
71
+ "test": "nodemon scripts/start-test.js",
72
+ "prod": "node scripts/start-prod.js",
73
+ "test:run": "NODE_OPTIONS='--experimental-vm-modules' jest --coverage",
74
+ "test:watch": "jest --watch",
75
+ "test:unit": "jest --testPathPattern=tests/unit",
76
+ "test:integration": "jest --testPathPattern=tests/integration",
77
+ "start:test": "nodemon scripts/start-test.js",
78
+ "pnpm:check:dev": "NODE_ENV=development node scripts/pnpm-check-wrapper.js",
79
+ "pnpm:check:prod": "NODE_ENV=production node scripts/pnpm-check-wrapper.js",
80
+ "pnpm:check:test": "NODE_ENV=test node scripts/pnpm-check-wrapper.js",
81
+ "pnpm:prune": "pnpm store prune",
82
+ "pnpm:prune:manual": "node scripts/pnpm-prune-manual.js",
83
+ "pnpm:status": "pnpm store status",
84
+ "cli:start": "node src/cli.js start",
85
+ "cli:stop": "node src/cli.js stop",
86
+ "cli:restart": "node src/cli.js restart",
87
+ "cli:status": "node src/cli.js status",
88
+ "cli:start:dev": "node src/cli.js start --env development",
89
+ "cli:start:prod": "node src/cli.js start --env production",
90
+ "cli:start:test": "node src/cli.js start --env test",
91
+ "build": "node scripts/build.js"
92
92
  }
93
- }
93
+ }