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); let path7z = __dirname+"/7z/7z.exe"; let pathNsis = __dirname+"/NSIS/makensis.exe"; let pathUninstallExe = ""; (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 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); // 读取配置文件 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(/\<\#copyright\#\>/g, packageInfo.softInfo.copyright); nsiContent = nsiContent.replace(/\<\#statisticsUrl\#\>/g, packageInfo.softInfo.statisticsUrl); nsiContent = nsiContent.replace(/\<\#uninstallUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/uninstallWeb/'); pathUninstallExe = __dirname +"/../build/" + packageInfo.name + process.env.arch + ".exe" if(process.env.arch == 'x64'){ nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + '_' + packageInfo.version +'.7z'); nsiContent = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + process.env.arch); fs.writeFileSync(nsiOutPath,nsiContent); makeX64(); }else if(process.env.arch == 'ia32'){ nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + '_' + packageInfo.version +'.7z'); nsiContent = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + process.env.arch); fs.writeFileSync(nsiOutPath,nsiContent); makeIa32(); }else{ pathUninstallExe = __dirname +"/../build/" + packageInfo.name + "ia32.exe"; nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + '_' + packageInfo.version +'.7z'); let nsiContent1 = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + "ia32"); fs.writeFileSync(nsiOutPath,nsiContent1); makeIa32(); setTimeout(() => { pathUninstallExe = __dirname +"/../build/" + packageInfo.name + "x64.exe"; nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + packageInfo.version +'.7z'); let nsiContent2 = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + "x64"); fs.writeFileSync(nsiOutPath,nsiContent2); makeX64(); },15000) } })(); async function makeIa32(){ // 文件夹7z压缩 if(fs.existsSync(__dirname + '/../build/lastest_ia32_'+packageInfo.version+'.7z')){ fs.unlinkSync(__dirname + '/../build/lastest_ia32_'+packageInfo.version+'.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_ia32_'+packageInfo.version+'.7z', __dirname + '/../build/' + dir + '/*']).then(res =>{ console.log(chalk.yellow.bold("32位 软件目录压缩完成!")); }).catch(err => { console.log(chalk.red.bold("err - 32位 软件目录压缩失败!"),err); }); } }) }) }else{ logStats(path7z,'nsis 32位 开始打包!'); await spawnExec([pathNsis,__dirname+'/test.nsi']).then(async(res) =>{ console.log(chalk.yellow.bold("nsis 32位 打包完成!")); // 执行静默安装 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("32位 生成卸载文件!")); }).catch(err => { console.log(chalk.red.bold("err - 32位 生成卸载文件失败,请查看配置是否正确!"), err); return false; }); }).catch(err => { console.log(chalk.red.bold("err - 32位 打包失败,请查看配置是否正确!"), err); return false; }); } } async function makeX64(){ // 文件夹7z压缩 if(fs.existsSync(__dirname + '/../build/lastest_x64_'+packageInfo.version+'.7z')){ fs.unlinkSync(__dirname + '/../build/lastest_x64_'+packageInfo.version+'.7z'); } if (process.env.BUILD_TARGET == 'pack') { fs.readdir(__dirname+"/../build",(err,files) => { files.map(dir => { if(dir.indexOf(packageInfo.name+'-win32-x64') != -1){ spawnExec([path7z, 'a', __dirname + '/../build/lastest_x64_'+packageInfo.version+'.7z', __dirname + '/../build/' + dir + '/*']).then(res =>{ console.log(chalk.yellow.bold("64位 软件目录压缩完成!")); }).catch(err => { console.log(chalk.red.bold("err - 64位 软件目录压缩失败!"),err); }); } }) }) }else{ logStats(path7z,'nsis 64位 开始打包!'); await spawnExec([pathNsis,__dirname+'/test.nsi']).then(async(res) =>{ console.log(chalk.yellow.bold("nsis 64位 打包完成!")); // 执行静默安装 await spawnExec([pathUninstallExe,'/S']).then(res =>{ fs.copyFileSync(os.tmpdir() + '/uninst.exe', __dirname +"/../build/"+packageInfo.name+"-win32-x64/uninst.exe") console.log(chalk.yellow.bold("64位 生成卸载文件!")); }).catch(err => { console.log(chalk.red.bold("err - 64位 生成卸载文件失败,请查看配置是否正确!"), err); return false; }); }).catch(err => { console.log(chalk.red.bold("err - 64位 打包失败,请查看配置是否正确!"), err); return false; }); } } 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) }