gulpfile.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. const fs = require('fs');
  7. const path = require('path');
  8. const gulp = require('gulp');
  9. const shell = require("gulp-shell");
  10. const gulpif = require("gulp-if");
  11. const uglify = require("gulp-uglify");
  12. const browserify = require("gulp-browserify");
  13. const stylus = require("gulp-stylus");
  14. const args = require("yargs").argv;
  15. const IS_DEBUG = !!args.debug;
  16. console.log("IS_DEBUG: ", IS_DEBUG);
  17. console.log("--------------------");
  18. const TPL_FILE_INFO = "echo '> (DEBUG " + (IS_DEBUG ? "on" : "off") + ") <%= file.path %>'";
  19. const plist_fn = path.join(__dirname, 'app/SH3/MacGap/SwitchHosts!-Info.plist');
  20. const output = {
  21. // 汉字 -> unicode
  22. ascii_only: true
  23. };
  24. gulp.task('zip', function () {
  25. const config = require('./app/src/config');
  26. let dir = args.dir;
  27. if (!dir || !fs.existsSync(dir)) {
  28. console.log(`bad --dir: "${dir}"`);
  29. return;
  30. }
  31. let c = fs.readFileSync(plist_fn, 'utf-8');
  32. let m = c.match(/CFBundleVersion[^\d]*?(\d+)/);
  33. if (!m) {
  34. console.log('CFBundleVersion not found!');
  35. return;
  36. }
  37. let v = m[1];
  38. let fn = `SwitchHosts!_v${config.VERSION}.${v}_for_mac.zip`;
  39. console.log('fn', fn);
  40. gulp.src('app/src/main.js')
  41. .pipe(shell([
  42. `cd ${dir} && zip -ry ${fn} ./SwitchHosts!.app`
  43. ]))
  44. ;
  45. });
  46. gulp.task('ver', function () {
  47. const config = require('./app/src/config');
  48. let c = fs.readFileSync(plist_fn, 'utf-8');
  49. let m;
  50. let v;
  51. m = c.match(/CFBundleShortVersionString[^\d]*?([\d\.]+)/);
  52. if (!m) {
  53. console.log('CFBundleShortVersionString not found!');
  54. return;
  55. }
  56. v = m[1];
  57. console.log(`short version: ${v} -> ${config.VERSION}`);
  58. c = c.replace(/(CFBundleShortVersionString[^\d]*?)([\d\.]+)/, `$1${config.VERSION}`);
  59. m = c.match(/CFBundleVersion[^\d]*?(\d+)/);
  60. if (!m) {
  61. console.log('CFBundleVersion not found!');
  62. return;
  63. }
  64. v = parseInt(m[1]);
  65. console.log(`version: ${v} -> ${v + 1}`);
  66. c = c.replace(/(CFBundleVersion[^\d]*?)(\d+)/, `$1${v + 1}`);
  67. //console.log(c);
  68. fs.writeFileSync(plist_fn, c, 'utf-8');
  69. });
  70. gulp.task('js', ['ver'], function () {
  71. gulp.src(['app/src/main.js'])
  72. .pipe(shell(TPL_FILE_INFO))
  73. //.pipe(sourcemaps.init())
  74. .pipe(browserify({
  75. debug: IS_DEBUG
  76. }))
  77. .pipe(gulpif(!IS_DEBUG, uglify({
  78. output: output,
  79. compress: {
  80. drop_console: !IS_DEBUG
  81. }
  82. })))
  83. .pipe(gulp.dest('app/SH3/public/js'))
  84. ;
  85. });
  86. gulp.task('default', function () {
  87. gulp.start('js');
  88. gulp.watch('app/src/**/*.js', ['js']);
  89. });