-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension-zips.cjs
104 lines (84 loc) · 2.93 KB
/
extension-zips.cjs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const archiver = require('archiver');
const AdmZip = require('adm-zip');
const { execSync } = require('child_process');
const EXT_NAME = 'gzker-plus';
const { version } = require('./package.json');
const distPath = path.resolve(__dirname, 'dist');
const outputDir = path.resolve(__dirname, '.output');
const chromeZipFileName = `${EXT_NAME}-${version}-chromium.zip`;
const firefoxZipFileName = `${EXT_NAME}-${version}-firefox.zip`;
const sourcesZipFileName = `${EXT_NAME}-${version}-sources.zip`;
const chromeOutputPath = path.resolve(outputDir, chromeZipFileName);
const firefoxOutputPath = path.resolve(outputDir, firefoxZipFileName);
const sourcesOutputPath = path.resolve(outputDir, sourcesZipFileName);
// eslint-disable-next-line max-params
function compressFolder(inputPath, outputPath, zipFileName, ignore) {
return new Promise((resolve, reject) => {
const output = fs.createWriteStream(outputPath);
const archive = archiver('zip', { zlib: { level: 9 } });
output.on('close', () => {
console.log(`🎉 ${zipFileName} compression completed`);
resolve();
});
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.warn(err);
} else {
reject(err);
}
});
archive.on('error', (err) => {
reject(err);
});
archive.pipe(output);
archive.glob('**', {
cwd: inputPath,
ignore,
dot: true,
});
archive.finalize();
});
}
function unzipFile(zipFilePath) {
const outputPath = zipFilePath.split('.zip')[0];
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
const zip = new AdmZip(zipFilePath);
zip.extractAllTo(outputPath, true);
}
async function runCompression() {
try {
// 创建输出文件夹
if (fs.existsSync(outputDir)) {
fse.emptyDirSync(outputDir);
} else {
fs.mkdirSync(outputDir);
}
// 执行 pnpm build 构建 Chrome 版本
console.log('📦 start compressing the chrome version...');
execSync('pnpm build', { stdio: 'ignore' });
// 压缩 Chrome 版本
await compressFolder(distPath, chromeOutputPath, chromeZipFileName);
unzipFile(chromeOutputPath);
// 执行 pnpm build:ff 构建 Firefox 版本
console.log('📦 start compressing the firefox version...');
execSync('pnpm build:ff', { stdio: 'ignore' });
// 压缩 Firefox 版本
await compressFolder(distPath, firefoxOutputPath, firefoxZipFileName);
unzipFile(firefoxOutputPath);
console.log('📦 start compressing the source code of the project...');
// 压缩项目源码
await compressFolder(__dirname, sourcesOutputPath, sourcesZipFileName, [
'**/{.git,.github,.output,dist,node_modules}/**',
'.webextrc.json',
]);
} catch (err) {
console.error('An error occurred:', err);
}
}
runCompression();