zyd-server-build2 1.0.12 → 1.0.16
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/README.md +253 -71
- package/index.js +9 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,76 +1,233 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Powered by zydsoft™
|
|
3
|
+
* 2021-9-14
|
|
4
|
+
* zz
|
|
5
|
+
*/
|
|
6
|
+
const { minify } = require("terser")
|
|
7
|
+
const { execSync } = require('child_process')
|
|
8
|
+
const fs = require("fs")
|
|
9
|
+
const path = require("path")
|
|
10
|
+
const count = [0, 0, 0]
|
|
11
|
+
const timer = []
|
|
12
|
+
const version = require('./package.json').version
|
|
13
|
+
let baseDst = ''
|
|
14
|
+
const operatingDir = generateComplexRandomString(5)
|
|
7
15
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
16
|
+
async function build({ src, dst, ignoreDir = [], ignoreFile = [], copyright = "", noBuildFile = [], noBuildDir = [], noEncryptionPathFile = [], confused = 1, options = {}, encryptionPath = 0 }) {
|
|
17
|
+
if (!src || !dst) {
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
ignoreDir.length > 0 && ignoreDir.push(dst)
|
|
21
|
+
await fs.readdirSync(src).forEach(async file => {
|
|
22
|
+
const _src = src + "/" + file
|
|
23
|
+
let _dst = dst + "/" + file
|
|
24
|
+
if (encryptionPath && !ignore(noBuildFile, _src) && confused === 1 && path.extname(_src) === ".js" && !ignore(noEncryptionPathFile, _src)) {
|
|
25
|
+
_dst = `${baseDst}/z.${operatingDir}/${generateComplexRandomString(5)}.${Math.random().toString(36).substring(2, 10)}.js`
|
|
26
|
+
}
|
|
27
|
+
const st = fs.statSync(_src)
|
|
28
|
+
if (st.isFile()) {
|
|
29
|
+
count[2] += st.size
|
|
30
|
+
count[0]++
|
|
31
|
+
if (ignore(ignoreFile, _src)) {
|
|
32
|
+
count[2] -= st.size
|
|
33
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[31mSkip x\x1B[0m")
|
|
34
|
+
count[0]--
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
if (path.extname(_src) === ".js") {
|
|
38
|
+
if (ignore(noBuildFile, _src) || confused === 0) {
|
|
39
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[32m√\x1B[0m")
|
|
40
|
+
readable = fs.createReadStream(_src);
|
|
41
|
+
writable = fs.createWriteStream(_dst);
|
|
42
|
+
readable.pipe(writable);
|
|
43
|
+
} else {
|
|
44
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[32mCode confused √\x1B[0m")
|
|
45
|
+
await execSync(`npx babel ${_src} --out-file ${_dst}`)
|
|
46
|
+
readable = await fs.readFileSync(_dst, 'utf-8')
|
|
47
|
+
const result = await minify(readable, {
|
|
48
|
+
parse: {
|
|
49
|
+
// parse options
|
|
50
|
+
},
|
|
51
|
+
compress: options,
|
|
52
|
+
mangle: {
|
|
53
|
+
// mangle options
|
|
54
|
+
},
|
|
55
|
+
format: {
|
|
56
|
+
// format options (can also use `output` for backwards compatibility)
|
|
57
|
+
},
|
|
58
|
+
sourceMap: {
|
|
59
|
+
// source map options
|
|
60
|
+
},
|
|
61
|
+
ecma: 2016, // specify one of: 5, 2015, 2016, etc.
|
|
62
|
+
enclose: false, // or specify true, or "args:values"
|
|
63
|
+
ie8: false,
|
|
64
|
+
module: true,
|
|
65
|
+
nameCache: null, // or specify a name cache object
|
|
66
|
+
safari10: false,
|
|
67
|
+
keep_classnames: true, // 默认 false。传 true来防止压缩器干掉对象名。对那些依赖
|
|
68
|
+
keep_fnames: true, // 默认 false。传 true来防止压缩器干掉函数名。对那些依赖
|
|
69
|
+
toplevel: false
|
|
70
|
+
})
|
|
71
|
+
await fs.writeFileSync(_dst, (copyright ? "/* " + copyright + " */" : "") + result.code)
|
|
72
|
+
count[1]++
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[32m√\x1B[0m")
|
|
76
|
+
readable = fs.createReadStream(_src)
|
|
77
|
+
writable = fs.createWriteStream(_dst)
|
|
78
|
+
readable.pipe(writable)
|
|
79
|
+
}
|
|
80
|
+
// sleep(100)
|
|
81
|
+
// count[3] += fs.statSync(_dst).size
|
|
82
|
+
} else if (st.isDirectory()) {
|
|
83
|
+
if (ignore(ignoreDir, _src)) {
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
makeDir(_dst)
|
|
87
|
+
await build({ src: _src, dst: _dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir, noEncryptionPathFile, confused: ignore(noBuildDir, _src) ? 0 : 1, encryptionPath })
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ignore(arr, str) {
|
|
93
|
+
let d = false
|
|
94
|
+
arr.forEach(item => {
|
|
95
|
+
item === str && (d = true)
|
|
96
|
+
})
|
|
97
|
+
return d
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function exists(dst, noDeleteDir) {
|
|
101
|
+
if (!fs.existsSync(dst)) {
|
|
102
|
+
makeDir(dst)
|
|
103
|
+
} else {
|
|
104
|
+
// del(dst)
|
|
105
|
+
delDir(dst, noDeleteDir)
|
|
106
|
+
// makeDir(dst)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
12
109
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
110
|
+
function makeDir(dirpath) {
|
|
111
|
+
if (!fs.existsSync(dirpath)) {
|
|
112
|
+
var pathtmp;
|
|
113
|
+
dirpath.split("/").forEach(dirname => {
|
|
114
|
+
if (pathtmp) {
|
|
115
|
+
pathtmp = path.join(pathtmp, dirname);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
//如果在linux系统中,第一个dirname的值为空,所以赋值为"/"
|
|
119
|
+
if (dirname) {
|
|
120
|
+
pathtmp = dirname;
|
|
121
|
+
} else {
|
|
122
|
+
pathtmp = "/";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!fs.existsSync(pathtmp)) {
|
|
126
|
+
if (!fs.mkdirSync(pathtmp)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
25
129
|
}
|
|
26
|
-
|
|
27
|
-
|
|
130
|
+
});
|
|
131
|
+
// } else {
|
|
132
|
+
// deleteFolderFiles(dirpath);
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
28
135
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
136
|
+
|
|
137
|
+
const delDir = (url, noDeleteDir) => {
|
|
138
|
+
var files = []
|
|
139
|
+
files = fs.readdirSync(url)
|
|
140
|
+
for (var i = 0; i < files.length; i++) {
|
|
141
|
+
if (!noDeleteDir.find(item => item === files[i])) {
|
|
142
|
+
var path = url + '/' + files[i]
|
|
143
|
+
if (fs.statSync(path).isFile()) {
|
|
144
|
+
fs.unlinkSync(path)
|
|
145
|
+
} else {
|
|
146
|
+
delDir(path, noDeleteDir)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
35
149
|
}
|
|
36
150
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
function bytesToSize(bytes) {
|
|
154
|
+
if (bytes === 0) return '0 B';
|
|
155
|
+
var k = 1024;
|
|
156
|
+
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
157
|
+
i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
158
|
+
//return (bytes / Math.pow(k, i)) + ' ' + sizes[i];
|
|
159
|
+
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
|
|
160
|
+
//toPrecision(3) 后面保留两位小数,如1.00GB
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 阻塞函数
|
|
165
|
+
* @param {Number} milliSeconds 毫秒数
|
|
166
|
+
*/
|
|
167
|
+
function sleep(milliSeconds) {
|
|
168
|
+
const startTime = new Date().getTime()
|
|
169
|
+
while (new Date().getTime() < startTime + milliSeconds) { }
|
|
50
170
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
171
|
+
|
|
172
|
+
function formatDuration(ms) {
|
|
173
|
+
if (ms < 0) ms = -ms;
|
|
174
|
+
const time = {
|
|
175
|
+
day: Math.floor(ms / 86400000),
|
|
176
|
+
hour: Math.floor(ms / 3600000) % 24,
|
|
177
|
+
minute: Math.floor(ms / 60000) % 60,
|
|
178
|
+
second: Math.floor(ms / 1000) % 60,
|
|
179
|
+
millisecond: Math.floor(ms) % 1000
|
|
180
|
+
};
|
|
181
|
+
return Object.entries(time)
|
|
182
|
+
.filter(val => val[1] !== 0)
|
|
183
|
+
.map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
|
|
184
|
+
.join(', ');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function generateComplexRandomString(length) {
|
|
188
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
189
|
+
let result = '';
|
|
190
|
+
|
|
191
|
+
for (let i = 0; i < length; i++) {
|
|
192
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
193
|
+
}
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function safeDelete(dir) {
|
|
198
|
+
try {
|
|
199
|
+
if (!fs.existsSync(dir)) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
// 先递归处理子目录
|
|
203
|
+
const files = fs.readdirSync(dir);
|
|
204
|
+
for (const file of files) {
|
|
205
|
+
const fullPath = path.join(dir, file);
|
|
206
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
207
|
+
safeDelete(fullPath);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// 处理完子目录后,再判断自己是否为空
|
|
211
|
+
if (fs.readdirSync(dir).length === 0) {
|
|
212
|
+
fs.rmdirSync(dir);
|
|
213
|
+
// console.log(`已删除: ${dir}`);
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
// console.error(`删除失败 [${dir}]:`, error.message);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
module.exports = async ({
|
|
221
|
+
src = ".",
|
|
222
|
+
dst = "./build",
|
|
223
|
+
ignoreDir = ["./.git", "./node_modules"],
|
|
224
|
+
ignoreFile = ["./build.js"],
|
|
225
|
+
copyright = "",
|
|
226
|
+
noBuildFile = [],
|
|
227
|
+
noBuildDir = [],
|
|
228
|
+
noEncryptionPathFile = [],
|
|
229
|
+
noDeleteDir = [".git", "node_modules"],
|
|
230
|
+
options = {
|
|
74
231
|
drop_console: true, // 删除console
|
|
75
232
|
dead_code: true, // 移除没被引用的代码
|
|
76
233
|
drop_debugger: true, // 移除 debugger;
|
|
@@ -88,8 +245,33 @@ zsb({
|
|
|
88
245
|
DEBUG: false
|
|
89
246
|
},
|
|
90
247
|
},
|
|
91
|
-
encryptionPath
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
248
|
+
encryptionPath = 0
|
|
249
|
+
} = {}) => {
|
|
250
|
+
console.log(`\x1B[33m
|
|
251
|
+
┌─────────────────────┐
|
|
252
|
+
│ Powered by zydsoft® │
|
|
253
|
+
│ zyd-server-build │
|
|
254
|
+
└─────────────────────┘
|
|
255
|
+
\x1B[0m\x1B[37mversion:\x1B[0m\x1B[31m${version}\x1B[0m
|
|
256
|
+
`)
|
|
257
|
+
console.log("\x1B[36m\x1B[1mStart build...\x1B[0m\x1B[0m\n")
|
|
258
|
+
timer[0] = new Date()
|
|
259
|
+
exists(dst, noDeleteDir)
|
|
260
|
+
baseDst = dst.toString()
|
|
261
|
+
await execSync(`rm -rf ${dst}`)
|
|
262
|
+
await build({ src, dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir, noEncryptionPathFile, options, encryptionPath })
|
|
263
|
+
process.nextTick(async () => {
|
|
264
|
+
console.log("")
|
|
265
|
+
console.log(`number of files: \x1B[36m${count[0]}\x1B[0m`)
|
|
266
|
+
console.log(`Number of Obfuscated Files: \x1B[36m${count[1]}\x1B[0m`)
|
|
267
|
+
console.log(`Collection Size: \x1B[36m${bytesToSize(count[2])}\x1B[0m`)
|
|
268
|
+
console.log(`time consuming: \x1B[36m${formatDuration(new Date() - timer[0])}\x1B[0m`)
|
|
269
|
+
// console.log("\x1B[1m\x1B[32mbuild success √\x1B[0m\x1B[0m")
|
|
270
|
+
console.log(`\x1B[1m\x1B[32m
|
|
271
|
+
┌─────────────────┐
|
|
272
|
+
│ build success √ │
|
|
273
|
+
└─────────────────┘
|
|
274
|
+
\x1B[0m\x1B[0m`)
|
|
275
|
+
safeDelete(dst)
|
|
276
|
+
})
|
|
277
|
+
}
|
package/index.js
CHANGED
|
@@ -10,8 +10,10 @@ const path = require("path")
|
|
|
10
10
|
const count = [0, 0, 0]
|
|
11
11
|
const timer = []
|
|
12
12
|
const version = require('./package.json').version
|
|
13
|
+
let baseDst = ''
|
|
14
|
+
const operatingDir = generateComplexRandomString(5)
|
|
13
15
|
|
|
14
|
-
async function build({ src, dst, ignoreDir = [], ignoreFile = [], copyright = "", noBuildFile = [], noBuildDir = [], confused = 1, options = {}, encryptionPath = 0 }) {
|
|
16
|
+
async function build({ src, dst, ignoreDir = [], ignoreFile = [], copyright = "", noBuildFile = [], noBuildDir = [], noEncryptionPathFile = [], confused = 1, options = {}, encryptionPath = 0 }) {
|
|
15
17
|
if (!src || !dst) {
|
|
16
18
|
return
|
|
17
19
|
}
|
|
@@ -19,22 +21,9 @@ async function build({ src, dst, ignoreDir = [], ignoreFile = [], copyright = ""
|
|
|
19
21
|
await fs.readdirSync(src).forEach(async file => {
|
|
20
22
|
const _src = src + "/" + file
|
|
21
23
|
let _dst = dst + "/" + file
|
|
22
|
-
if (encryptionPath && !ignore(noBuildFile, _src)) {
|
|
23
|
-
|
|
24
|
-
if (dst.indexOf("/operating") !== -1) {
|
|
25
|
-
_dst = `${dst}/${generateComplexRandomString(5)}.${Math.random().toString(36).substring(2, 10)}.js`
|
|
26
|
-
} else {
|
|
27
|
-
_dst = `${dst}/operating/${generateComplexRandomString(5)}.${Math.random().toString(36).substring(2, 10)}.js`
|
|
28
|
-
}
|
|
29
|
-
} else {
|
|
30
|
-
if (dst.indexOf("/operating") !== -1) {
|
|
31
|
-
_dst = dst
|
|
32
|
-
} else {
|
|
33
|
-
_dst = dst + "/operating"
|
|
34
|
-
}
|
|
35
|
-
}
|
|
24
|
+
if (encryptionPath && !ignore(noBuildFile, _src) && confused === 1 && path.extname(_src) === ".js" && !ignore(noEncryptionPathFile, _src)) {
|
|
25
|
+
_dst = `${baseDst}/z.${operatingDir}/${generateComplexRandomString(5)}.${Math.random().toString(36).substring(2, 10)}.js`
|
|
36
26
|
}
|
|
37
|
-
|
|
38
27
|
const st = fs.statSync(_src)
|
|
39
28
|
if (st.isFile()) {
|
|
40
29
|
count[2] += st.size
|
|
@@ -95,7 +84,7 @@ async function build({ src, dst, ignoreDir = [], ignoreFile = [], copyright = ""
|
|
|
95
84
|
return
|
|
96
85
|
}
|
|
97
86
|
makeDir(_dst)
|
|
98
|
-
await build({ src: _src, dst: _dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir, confused: ignore(noBuildDir, _src) ? 0 : 1, encryptionPath })
|
|
87
|
+
await build({ src: _src, dst: _dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir, noEncryptionPathFile, confused: ignore(noBuildDir, _src) ? 0 : 1, encryptionPath })
|
|
99
88
|
}
|
|
100
89
|
})
|
|
101
90
|
}
|
|
@@ -213,6 +202,7 @@ module.exports = async ({
|
|
|
213
202
|
copyright = "",
|
|
214
203
|
noBuildFile = [],
|
|
215
204
|
noBuildDir = [],
|
|
205
|
+
noEncryptionPathFile = [],
|
|
216
206
|
noDeleteDir = [".git", "node_modules"],
|
|
217
207
|
options = {
|
|
218
208
|
drop_console: true, // 删除console
|
|
@@ -244,7 +234,8 @@ module.exports = async ({
|
|
|
244
234
|
console.log("\x1B[36m\x1B[1mStart build...\x1B[0m\x1B[0m\n")
|
|
245
235
|
timer[0] = new Date()
|
|
246
236
|
exists(dst, noDeleteDir)
|
|
247
|
-
|
|
237
|
+
baseDst = dst.toString()
|
|
238
|
+
await build({ src, dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir, noEncryptionPathFile, options, encryptionPath })
|
|
248
239
|
process.nextTick(() => {
|
|
249
240
|
console.log("")
|
|
250
241
|
console.log(`number of files: \x1B[36m${count[0]}\x1B[0m`)
|