gulpfile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. /**
  7. * 注:ELECTRON_VERSION 为对应的 Electron 版本
  8. * 直接运行命令会自动下载对应文件,
  9. * 也可手动从 https://github.com/electron/electron/releases 下载最新版本,放到 ~/.electron 目录下
  10. * 淘宝镜像:https://npm.taobao.org/mirrors/electron/
  11. */
  12. const ELECTRON_VERSION = '1.4.15';
  13. const fs = require('fs');
  14. const path = require('path');
  15. // const util = require('util');
  16. const exec = require('child_process').exec;
  17. const gulp = require('gulp');
  18. const shell = require('gulp-shell');
  19. const beautify = require('js-beautify').js_beautify;
  20. const args = require('yargs').argv;
  21. console.log(args);
  22. // const IS_DEBUG = !!args.debug;
  23. // const TPL_FILE_INFO = "echo '> (DEBUG " + (IS_DEBUG ? "on" : "off") + ") <%= file.path %>'";
  24. gulp.task('ver', () => {
  25. let fn = path.join(__dirname, 'app', 'version.js');
  26. let version = require('./app/version').version;
  27. version[3]++;
  28. console.log(`version -> ${version.join('.')}`);
  29. let cnt = `exports.version = ${JSON.stringify(version)};`;
  30. fs.writeFileSync(fn, cnt, 'utf-8');
  31. function updatePackage(fn) {
  32. cnt = fs.readFileSync(fn);
  33. let d = JSON.parse(cnt);
  34. d.version = version.slice(0, 3).join('.');
  35. cnt = beautify(JSON.stringify(d), {indent_size: 2});
  36. fs.writeFileSync(fn, cnt, 'utf-8');
  37. }
  38. // update package.json
  39. updatePackage(path.join(__dirname, 'package.json'));
  40. updatePackage(path.join(__dirname, 'app', 'package.json'));
  41. });
  42. gulp.task('pack', (done) => {
  43. let version = require('./app/version').version;
  44. let v1 = version.slice(0, 3).join('.');
  45. let v2 = version[3];
  46. let pack = {};
  47. pack.macOS = `
  48. # for macOS
  49. electron-packager ./app 'SwitchHosts!' --platform=darwin --arch=x64 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.icns --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
  50. cp ./assets/Credits.rtf dist/SwitchHosts\!-darwin-x64/SwitchHosts\!.app/Contents/Resources/en.lproj
  51. `;
  52. pack.win64 = `
  53. # for windows x64
  54. electron-packager ./app 'SwitchHosts!' --platform=win32 --arch=x64 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.ico --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
  55. `;
  56. pack.win32 = `
  57. # for windows ia32
  58. electron-packager ./app 'SwitchHosts!' --platform=win32 --arch=ia32 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.ico --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
  59. `;
  60. pack.linux = `
  61. # for linux x86_64
  62. electron-packager ./app 'SwitchHosts!' --platform=linux --arch=x64 --version=${ELECTRON_VERSION} --overwrite --asar=true --prune --icon=./assets/app.ico --ignore=node_modules/.bin --ignore=.git --ignore=dist --ignore=node_modules/electron-* --out=dist --app-version=${v1} --build-version=${v2}
  63. `;
  64. let cmds = [];
  65. if (!args.platform) {
  66. cmds = [pack.macOS, pack.win64, pack.win32, pack.linux];
  67. } else {
  68. let a = args.platform.split(',');
  69. a.map(i => cmds.push(pack[i] || ''));
  70. }
  71. console.log(`start packing, v: ${v1}.${v2} ..`);
  72. console.log(cmds.join('\n'));
  73. exec(cmds.join('\n'), (error, stdout, stderr) => {
  74. console.log('end pack.');
  75. if (error) {
  76. console.error(`exec error: ${error}`);
  77. }
  78. // if (stdout) console.log(`${stdout}`);
  79. // if (stderr) console.log(`${stderr}`);
  80. done();
  81. });
  82. });
  83. gulp.task('zip', (done) => {
  84. let version = require('./app/version').version;
  85. let v = version.join('.');
  86. let cmds = `
  87. cd ./dist
  88. rm -f ./SwitchHosts-*.zip
  89. zip -ry SwitchHosts-macOS-x64_v${v}.zip ./SwitchHosts\\!-darwin-x64/SwitchHosts\\!.app
  90. zip -ry SwitchHosts-win32-x64_v${v}.zip ./SwitchHosts\\!-win32-x64
  91. zip -ry SwitchHosts-win32-ia32_v${v}.zip ./SwitchHosts\\!-win32-ia32
  92. zip -ry SwitchHosts-linux-x64_v${v}.zip ./SwitchHosts\\!-linux-x64
  93. cd ..
  94. `;
  95. console.log(`start zip ..`);
  96. exec(cmds, (error, stdout, stderr) => {
  97. console.log('end zip.');
  98. if (error) {
  99. console.error(`exec error: ${error}`);
  100. }
  101. // if (stdout) console.log(`${stdout}`);
  102. // if (stderr) console.log(`${stderr}`);
  103. done();
  104. });
  105. });
  106. gulp.task('default', () => {
  107. // gulp.start('ver');
  108. gulp.watch([
  109. './app/main.js',
  110. './app/index.html',
  111. './app/src/**/*.*',
  112. '!./app/version.js'
  113. ], ['ver']);
  114. });