process.env.NODE_ENV = 'production' const fs = require('fs') const os = require('os') const chalk = require('chalk') const spawnExec = require("./spawnExec.js"); let content = fs.readFileSync("./package.json"); content = content.toString(); let packageInfo = JSON.parse(content); const axios = require('axios'); const FormData = require('form-data'); let path7z = __dirname+"/7z/7z.exe"; let pathNsis = __dirname+"/NSIS/makensis.exe"; let pathUninstallExe = __dirname +"/../build/"+packageInfo.name+"_XY.exe"; (async () => { logStats(path7z,'开始压缩资源目录!'); if(fs.existsSync(__dirname+'/output/skin.zip')){ fs.unlinkSync(__dirname+'/output/skin.zip'); } await spawnExec([path7z,'a', __dirname+'/output/skin.zip', __dirname+'/skin/*']).then(res =>{ console.log(chalk.yellow.bold("资源目录压缩完成!")); }).catch(err => { console.log(chalk.red.bold("err - 资源目录压缩失败!"),err); return false; }); // 读取配置文件 let nsiPath = "./nsis/test.tmp"; let nsiOutPath = "./nsis/test.nsi"; let nsiContent = fs.readFileSync(nsiPath); nsiContent = nsiContent.toString(); nsiContent = nsiContent.replace(/\<\#name\#\>/g, packageInfo.name); nsiContent = nsiContent.replace(/\<\#version\#\>/g, packageInfo.version); nsiContent = nsiContent.replace(/\<\#softMid\#\>/g, packageInfo.softInfo.softMid); nsiContent = nsiContent.replace(/\<\#softName\#\>/g, packageInfo.softInfo.softName); nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest'+packageInfo.version+'.7z'); nsiContent = nsiContent.replace(/\<\#copyright\#\>/g, packageInfo.softInfo.copyright); nsiContent = nsiContent.replace(/\<\#statisticsUrl\#\>/g, packageInfo.softInfo.statisticsUrl); nsiContent = nsiContent.replace(/\<\#uninstallUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/uninstallWeb/'); fs.writeFileSync(nsiOutPath,nsiContent); // 修改卸载推广的网址 let uninstallPath = "./nsis/uninstallpage.xml.tmp"; let uninstallOutPath = "./nsis/skin/uninstallpage.xml"; let uninstallContent = fs.readFileSync(uninstallPath); uninstallContent = uninstallContent.toString(); uninstallContent = uninstallContent.replace(/\<\#uninstallUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/uninstallWeb/'); fs.writeFileSync(uninstallOutPath,uninstallContent); // 写入version let versionOutPath = "./build/version.json"; let verisonContent = JSON.stringify({"v":packageInfo.version, "c":packageInfo.softInfo.upgradeLog}); fs.writeFileSync(versionOutPath, verisonContent); // 文件夹7z压缩 if(fs.existsSync(__dirname + '/../build/lastest.7z')){ //fs.unlinkSync(__dirname + '/../build/lastest.7z'); } if (process.env.BUILD_TARGET == 'pack') { fs.readdir(__dirname+"/../build",(err,files) => { files.map(dir => { if(dir.indexOf(packageInfo.name+'-win32-ia32') != -1){ spawnExec([path7z, 'a', __dirname + '/../build/lastest.7z', __dirname + '/../build/' + dir + '/*']).then(res =>{ console.log(chalk.yellow.bold("软件目录压缩完成!")); }).catch(err => { console.log(chalk.red.bold("err - 软件目录压缩失败!"),err); }); } }) }) }else{ logStats(path7z,'nsis 开始打包!'); await spawnExec([pathNsis,__dirname+'/test.nsi']).then(async(res) =>{ console.log(chalk.yellow.bold("nsis 打包完成!")); // 执行静默安装 await spawnExec([pathUninstallExe,'/S']).then(res =>{ fs.copyFileSync(os.tmpdir() + '/uninst.exe', __dirname +"/../build/"+packageInfo.name+"-win32-ia32/uninst.exe") console.log(chalk.yellow.bold("生成卸载文件!")); }).catch(err => { console.log(chalk.red.bold("err - 生成卸载文件失败,请查看配置是否正确!"), err); return false; }); }).catch(err => { console.log(chalk.red.bold("err - 打包失败,请查看配置是否正确!"), err); return false; }); } })(); function getFormData (data) { let formData = new FormData(); for (let i in data) { formData.append(i, data[i]); } return formData; }; function logStats (proc, data) { let log = '' log += chalk.yellow.bold(`┏ ${proc}`) log += '\n\n' if (typeof data === 'object') { data.toString({ colors: true, chunks: false }).split(/\r?\n/).forEach(line => { log += ' ' + line + '\n' }) } else { log += ` ${data}\n` } log += '\n' + chalk.yellow.bold(`┗ ${new Array(28 + 1).join('-')}`) + '\n' console.log(log) }