1 Star 3 Fork 0

飛翔の猫/zimukit-public

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
build.js 4.35 KB
一键复制 编辑 原始数据 按行查看 历史
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const fse = require('fs-extra'); // 需要安装fs-extra模块
const Seven = require('node-7z');
const { path7za } = require('7zip-bin');
// 执行命令函数
const runCommand = (command, cwd) => {
return new Promise((resolve, reject) => {
exec(command, { cwd }, (error, stdout, stderr) => {
if (error) {
console.error(`执行命令出错: ${error.message}`);
reject(error);
} else {
resolve();
}
});
});
};
const build = async () => {
try {
//执行打包ui命令
console.log('开始打包前端项目...');
await runCommand('npm run build', path.join(process.cwd(), 'ui'));
console.log('打包前端项目完成');
//清空dist文件夹
let distPath = path.join(process.cwd(), 'dist');
if (fs.existsSync(distPath)) {
fse.emptyDirSync(distPath);
}
//在dist目录下创建linux目录
console.log('开始打包linux版本...');
let linuxPath = path.join(distPath, 'linux');
//把node_modules/7zip-bin/linux复制到dist/linux目录下7zip-bin
fse.copySync(path.join(process.cwd(), 'node_modules/7zip-bin/linux'), path.join(linuxPath, '7zip-bin'));
//把db文件夹里的app.db复制到linux目录下
fse.copySync(path.join(process.cwd(), 'db/app.db'), path.join(linuxPath, 'app.db'));
//把node_sqllite_build下的linux/build/Release/node_sqlite3.node复制并覆盖到node_modules/sqlite3/build/下
fse.copySync(path.join(process.cwd(), 'node_sqlite_build/linux/build/Release/node_sqlite3.node'), path.join(process.cwd(), 'node_modules/sqlite3/build/Release/node_sqlite3.node'));
//根目录运行pkg-linux命令
await runCommand('npm run pkg-linux', process.cwd());
console.log('打包linux版本完成');
//在dist目录下创建win目录
console.log('开始打包windows版本...');
let winPath = path.join(distPath, 'win');
//把db文件夹里的app.db复制到win目录下
fse.copySync(path.join(process.cwd(), 'db/app.db'), path.join(winPath, 'app.db'));
//因为用到了7za,先把node_modules/7zip-bin/win复制到dist/win目录下7zip-bin
fse.copySync(path.join(process.cwd(), 'node_modules/7zip-bin/win'), path.join(winPath, '7zip-bin'));
//把node_sqllite_build下的win/build/Release/node_sqlite3.node复制并覆盖到node_modules/sqlite3/build/下
fse.copySync(path.join(process.cwd(), 'node_sqlite_build/win/build/Release/node_sqlite3.node'), path.join(process.cwd(), 'node_modules/sqlite3/build/Release/node_sqlite3.node'));
//根目录运行pkg-win命令
await runCommand('npm run pkg-win', process.cwd());
console.log('打包windows版本完成');
//获取版本号并把win和linux版本打包成7z文件
let packageJson = require(path.join(process.cwd(), 'package.json'));
let version = packageJson.version;
let name = packageJson.name;
console.log('开始打包7z文件...');
//win版本
let win7zPath = path.join(distPath, `${name}-${version}-win.7z`);
//需要打包的文件夹
let win7zDir = path.join(distPath, 'win');
let winFiles = fs.readdirSync(win7zDir).map(file => path.join(win7zDir, file));
const myStream = Seven.add(win7zPath, winFiles, {
recursive: true,
$bin: path7za,
});
myStream.on('end', () => {
//删除win目录
fse.removeSync(win7zDir);
console.log('打包windows压缩包完成');
});
//linux版本
let linux7zPath = path.join(distPath, `${name}-${version}-linux.7z`);
//需要打包的文件夹
let linux7zDir = path.join(distPath, 'linux');
let linuxFiles = fs.readdirSync(linux7zDir).map(file => path.join(linux7zDir, file));
const myStream2 = Seven.add(linux7zPath, linuxFiles, {
recursive: true,
$bin: path7za,
});
myStream2.on('end', () => {
//删除linux目录
fse.removeSync(linux7zDir);
console.log('打包linux压缩包完成');
});
} catch (e) {
console.error('打包失败', e);
}
}
build();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/flycat0803/zimukit-public.git
git@gitee.com:flycat0803/zimukit-public.git
flycat0803
zimukit-public
zimukit-public
master

搜索帮助