gulpfile.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var fn_config = './app/src/config.js';
  48. const config = require('./app/src/config');
  49. let c = fs.readFileSync(plist_fn, 'utf-8');
  50. let m;
  51. let v;
  52. let v2;
  53. m = c.match(/CFBundleShortVersionString[^\d]*?([\d\.]+)/);
  54. if (!m) {
  55. console.log('CFBundleShortVersionString not found!');
  56. return;
  57. }
  58. v = m[1];
  59. console.log(`short version: ${v} -> ${config.VERSION}`);
  60. c = c.replace(/(CFBundleShortVersionString[^\d]*?)([\d\.]+)/, `$1${config.VERSION}`);
  61. m = c.match(/CFBundleVersion[^\d]*?(\d+)/);
  62. if (!m) {
  63. console.log('CFBundleVersion not found!');
  64. return;
  65. }
  66. v = parseInt(m[1]);
  67. v2 = v + 1;
  68. console.log(`version: ${v} -> ${v2}`);
  69. c = c.replace(/(CFBundleVersion[^\d]*?)(\d+)/, `$1${v2}`);
  70. //console.log(c);
  71. fs.writeFileSync(plist_fn, c, 'utf-8');
  72. c = fs.readFileSync(fn_config, 'utf-8');
  73. c = c.replace(/(bundle_version:\s*)(\d+)/, `$1${v2}`);
  74. fs.writeFileSync(fn_config, c, 'utf-8');
  75. });
  76. gulp.task('js', ['ver'], function () {
  77. gulp.src(['app/src/main.js'])
  78. .pipe(shell(TPL_FILE_INFO))
  79. //.pipe(sourcemaps.init())
  80. .pipe(browserify({
  81. debug: IS_DEBUG
  82. }))
  83. .pipe(gulpif(!IS_DEBUG, uglify({
  84. output: output,
  85. compress: {
  86. drop_console: !IS_DEBUG
  87. }
  88. })))
  89. .pipe(gulp.dest('app/SH3/public/js'))
  90. ;
  91. });
  92. gulp.task('default', function () {
  93. gulp.start('js');
  94. gulp.watch('app/src/**/*.js', ['js']);
  95. });