代码拉取完成,页面将自动刷新
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();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。