-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-for-windows.js
91 lines (85 loc) · 3.22 KB
/
build-for-windows.js
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
/** Build portable zip and bundle installer for Windows. */
import { spawn } from 'child_process';
import fs from 'fs';
import path from 'path';
import archiver from 'archiver';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
// ES模块中处理__dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* @param {string} productName
* @param {string} portableName
*/
function makePortable(productName, portableName) {
const releaseDir = path.join(__dirname, 'src-tauri/target/release');
const portableDir = path.join(releaseDir, 'portable');
const packDir = path.join(portableDir, portableName);
// 在 release 目录下创建 portable 目录
if (!fs.existsSync(packDir)) {
fs.mkdirSync(packDir, { recursive: true });
}
// 拷贝 exe 和 resources 目录到 portable 目录
fs.copyFileSync(
path.join(releaseDir, `${productName}.exe`),
path.join(packDir, `${productName}.exe`)
);
fs.cpSync(
path.join(releaseDir, 'resources'),
path.join(packDir, 'resources'),
{ recursive: true }
);
fs.unlinkSync(path.join(packDir, 'resources/icon.ico'));
// 拷贝并重命名 config-template.toml 为 config.toml
fs.copyFileSync(
path.join(releaseDir, 'resources/config-template.toml'),
path.join(packDir, 'config.toml')
);
// 将压缩 portable 目录为 zip
console.log(`开始压缩 ${portableName}.zip ...`);
const output = fs.createWriteStream(path.join(portableDir, `${portableName}.zip`));
output.on('close', function () {
const sizeInBytes = archive.pointer();
const sizeInMegabytes = (sizeInBytes / (1024 * 1024)).toFixed(2);
console.log(`${portableName}.zip 压缩完成,总大小: ${sizeInMegabytes} MB`);
});
const archive = archiver('zip', {
zlib: { level: 9 } // 设置压缩级别
});
archive.on('error', function (err) {
throw err;
});
archive.pipe(output);
archive.directory(packDir, false);
archive.finalize();
}
function main() {
/** @type {boolean} */
const skipBuild = process.argv.includes('--skip-build');
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
/** @type {string} */
const version = packageJson.version;
/** @type {string} */
const packageName = packageJson.name;
const tauriJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'src-tauri/tauri.conf.json'), 'utf8'));
/** @type {string} */
const productName = tauriJson.package.productName;
const portableName = `${packageName}_${version}_windows-x64-portable`;
if (skipBuild) {
console.log('tauri build 已跳过');
makePortable(productName, portableName);
return;
}
// 使用 spawn 替代 exec 来实时显示输出
// 使用 stdio: 'inherit' 保证 tauri 的彩色输出
const buildProcess = spawn('npm', ['run', 'tauri', 'build'], { shell: true, stdio: 'inherit' });
buildProcess.on('close', code => {
if (code !== 0) {
console.error(`tauri build 失败,退出码 ${code}`);
return;
}
makePortable(productName, portableName);
});
}
main();