-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
76 lines (60 loc) · 2.05 KB
/
build.js
File metadata and controls
76 lines (60 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import archiver from 'archiver';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const distDir = path.join(__dirname, 'app/dist');
const outputDir = path.join(__dirname, 'output');
const projectDir = "G:/projects/BotServer/BotServer/Resources/WebUi";
// 复制一份dist到项目目录
fs.rmSync(projectDir + '*', { recursive: true, force: true });
fs.cpSync(distDir, projectDir, { recursive: true });
// 确保输出目录存在
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// 文件名年月日时分,不足补零
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
const hour = now.getHours().toString().padStart(2, '0');
const minute = now.getMinutes().toString().padStart(2, '0');
const fileName = `${year}${month}${day}${hour}${minute}.zip`;
const outputFilePath = path.join(outputDir, fileName);
fs.readdir(distDir, async (err, files) => {
if (err) {
console.error('无法读取dist目录:', err);
return;
}
// 确保输出文件存在
fs.open(outputFilePath, 'w', (err, fd) => {
if (err) {
console.error('无法创建输出文件:', err);
return;
}
fs.close(fd, (err) => {
if (err) {
console.error('无法关闭输出文件:', err);
return;
}
return
});
});
// 打包成zip文件
const output = fs.createWriteStream(outputFilePath);
const archive = archiver('zip', {
zlib: { level: 9 } // 设置压缩级别
});
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('压缩完成');
});
archive.on('error', function (err) {
throw err;
});
archive.pipe(output);
archive.directory(distDir, false);
await archive.finalize();
});