build.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. process.env.NODE_ENV = 'production'
  2. const fs = require('fs')
  3. const os = require('os')
  4. const chalk = require('chalk')
  5. const spawnExec = require("./spawnExec.js");
  6. let content = fs.readFileSync("./package.json");
  7. content = content.toString();
  8. let packageInfo = JSON.parse(content);
  9. let path7z = __dirname+"/7z/7z.exe";
  10. let pathNsis = __dirname+"/NSIS/makensis.exe";
  11. let pathUninstallExe = "";
  12. (async () => {
  13. logStats(path7z,'开始压缩资源目录!');
  14. if(fs.existsSync(__dirname+'/output/skin.zip')){
  15. fs.unlinkSync(__dirname+'/output/skin.zip');
  16. }
  17. await spawnExec([path7z,'a', __dirname+'/output/skin.zip', __dirname+'/skin/*']).then(res =>{
  18. console.log(chalk.yellow.bold("资源目录压缩完成!"));
  19. }).catch(err => {
  20. console.log(chalk.red.bold("err - 资源目录压缩失败!"),err);
  21. return false;
  22. });
  23. // 修改卸载推广的网址
  24. let uninstallPath = "./nsis/uninstallpage.xml.tmp";
  25. let uninstallOutPath = "./nsis/skin/uninstallpage.xml";
  26. let uninstallContent = fs.readFileSync(uninstallPath);
  27. uninstallContent = uninstallContent.toString();
  28. uninstallContent = uninstallContent.replace(/\<\#uninstallUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/uninstallWeb/');
  29. fs.writeFileSync(uninstallOutPath,uninstallContent);
  30. // 写入version
  31. let versionOutPath = "./build/version.json";
  32. let verisonContent = JSON.stringify({"v":packageInfo.version, "c":packageInfo.softInfo.upgradeLog});
  33. fs.writeFileSync(versionOutPath, verisonContent);
  34. // 读取配置文件
  35. let nsiPath = "./nsis/test.tmp";
  36. let nsiOutPath = "./nsis/test.nsi";
  37. let nsiContent = fs.readFileSync(nsiPath);
  38. nsiContent = nsiContent.toString();
  39. nsiContent = nsiContent.replace(/\<\#name\#\>/g, packageInfo.name);
  40. nsiContent = nsiContent.replace(/\<\#version\#\>/g, packageInfo.version);
  41. nsiContent = nsiContent.replace(/\<\#softMid\#\>/g, packageInfo.softInfo.softMid);
  42. nsiContent = nsiContent.replace(/\<\#softName\#\>/g, packageInfo.softInfo.softName);
  43. nsiContent = nsiContent.replace(/\<\#copyright\#\>/g, packageInfo.softInfo.copyright);
  44. nsiContent = nsiContent.replace(/\<\#statisticsUrl\#\>/g, packageInfo.softInfo.statisticsUrl);
  45. nsiContent = nsiContent.replace(/\<\#uninstallUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/uninstallWeb/');
  46. pathUninstallExe = __dirname +"/../build/" + packageInfo.name + process.env.arch + ".exe"
  47. if(process.env.arch == 'x64'){
  48. nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + '_' + packageInfo.version +'.7z');
  49. nsiContent = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + process.env.arch);
  50. fs.writeFileSync(nsiOutPath,nsiContent);
  51. makeX64();
  52. }else if(process.env.arch == 'ia32'){
  53. nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + '_' + packageInfo.version +'.7z');
  54. nsiContent = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + process.env.arch);
  55. fs.writeFileSync(nsiOutPath,nsiContent);
  56. makeIa32();
  57. }else{
  58. pathUninstallExe = __dirname +"/../build/" + packageInfo.name + "ia32.exe";
  59. nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + '_' + packageInfo.version +'.7z');
  60. let nsiContent1 = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + "ia32");
  61. fs.writeFileSync(nsiOutPath,nsiContent1);
  62. makeIa32();
  63. setTimeout(() => {
  64. pathUninstallExe = __dirname +"/../build/" + packageInfo.name + "x64.exe";
  65. nsiContent = nsiContent.replace(/\<\#downloadUrl\#\>/g, packageInfo.softInfo.downloadPrefix + '/' + packageInfo.name + '/lastest_' + process.env.arch + packageInfo.version +'.7z');
  66. let nsiContent2 = nsiContent.replace(/\<\#installOutputName\#\>/g, packageInfo.name + "x64");
  67. fs.writeFileSync(nsiOutPath,nsiContent2);
  68. makeX64();
  69. },15000)
  70. }
  71. })();
  72. async function makeIa32(){
  73. // 文件夹7z压缩
  74. if(fs.existsSync(__dirname + '/../build/lastest_ia32_'+packageInfo.version+'.7z')){
  75. fs.unlinkSync(__dirname + '/../build/lastest_ia32_'+packageInfo.version+'.7z');
  76. }
  77. if (process.env.BUILD_TARGET == 'pack') {
  78. fs.readdir(__dirname+"/../build",(err,files) => {
  79. files.map(dir => {
  80. if(dir.indexOf(packageInfo.name+'-win32-ia32') != -1){
  81. spawnExec([path7z, 'a', __dirname + '/../build/lastest_ia32_'+packageInfo.version+'.7z', __dirname + '/../build/' + dir + '/*']).then(res =>{
  82. console.log(chalk.yellow.bold("32位 软件目录压缩完成!"));
  83. }).catch(err => {
  84. console.log(chalk.red.bold("err - 32位 软件目录压缩失败!"),err);
  85. });
  86. }
  87. })
  88. })
  89. }else{
  90. logStats(path7z,'nsis 32位 开始打包!');
  91. await spawnExec([pathNsis,__dirname+'/test.nsi']).then(async(res) =>{
  92. console.log(chalk.yellow.bold("nsis 32位 打包完成!"));
  93. // 执行静默安装
  94. await spawnExec([pathUninstallExe,'/S']).then(res =>{
  95. fs.copyFileSync(os.tmpdir() + '/uninst.exe', __dirname +"/../build/"+packageInfo.name+"-win32-ia32/uninst.exe")
  96. console.log(chalk.yellow.bold("32位 生成卸载文件!"));
  97. }).catch(err => {
  98. console.log(chalk.red.bold("err - 32位 生成卸载文件失败,请查看配置是否正确!"), err);
  99. return false;
  100. });
  101. }).catch(err => {
  102. console.log(chalk.red.bold("err - 32位 打包失败,请查看配置是否正确!"), err);
  103. return false;
  104. });
  105. }
  106. }
  107. async function makeX64(){
  108. // 文件夹7z压缩
  109. if(fs.existsSync(__dirname + '/../build/lastest_x64_'+packageInfo.version+'.7z')){
  110. fs.unlinkSync(__dirname + '/../build/lastest_x64_'+packageInfo.version+'.7z');
  111. }
  112. if (process.env.BUILD_TARGET == 'pack') {
  113. fs.readdir(__dirname+"/../build",(err,files) => {
  114. files.map(dir => {
  115. if(dir.indexOf(packageInfo.name+'-win32-x64') != -1){
  116. spawnExec([path7z, 'a', __dirname + '/../build/lastest_x64_'+packageInfo.version+'.7z', __dirname + '/../build/' + dir + '/*']).then(res =>{
  117. console.log(chalk.yellow.bold("64位 软件目录压缩完成!"));
  118. }).catch(err => {
  119. console.log(chalk.red.bold("err - 64位 软件目录压缩失败!"),err);
  120. });
  121. }
  122. })
  123. })
  124. }else{
  125. logStats(path7z,'nsis 64位 开始打包!');
  126. await spawnExec([pathNsis,__dirname+'/test.nsi']).then(async(res) =>{
  127. console.log(chalk.yellow.bold("nsis 64位 打包完成!"));
  128. // 执行静默安装
  129. await spawnExec([pathUninstallExe,'/S']).then(res =>{
  130. fs.copyFileSync(os.tmpdir() + '/uninst.exe', __dirname +"/../build/"+packageInfo.name+"-win32-x64/uninst.exe")
  131. console.log(chalk.yellow.bold("64位 生成卸载文件!"));
  132. }).catch(err => {
  133. console.log(chalk.red.bold("err - 64位 生成卸载文件失败,请查看配置是否正确!"), err);
  134. return false;
  135. });
  136. }).catch(err => {
  137. console.log(chalk.red.bold("err - 64位 打包失败,请查看配置是否正确!"), err);
  138. return false;
  139. });
  140. }
  141. }
  142. function logStats (proc, data) {
  143. let log = ''
  144. log += chalk.yellow.bold(`┏ ${proc}`)
  145. log += '\n\n'
  146. if (typeof data === 'object') {
  147. data.toString({
  148. colors: true,
  149. chunks: false
  150. }).split(/\r?\n/).forEach(line => {
  151. log += ' ' + line + '\n'
  152. })
  153. } else {
  154. log += ` ${data}\n`
  155. }
  156. log += '\n' + chalk.yellow.bold(`┗ ${new Array(28 + 1).join('-')}`) + '\n'
  157. console.log(log)
  158. }