gulpfile.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict';
  6. /*eslint-disable no-console*/
  7. const fs = require('fs');
  8. const path = require('path');
  9. const gulp = require('gulp');
  10. const shell = require('gulp-shell');
  11. const gulpif = require('gulp-if');
  12. const uglify = require('gulp-uglify');
  13. const browserify = require('gulp-browserify');
  14. const header = require('gulp-header');
  15. // const stylus = require('gulp-stylus');
  16. const babel = require('gulp-babel');
  17. const replace = require('gulp-replace-task');
  18. const args = require('yargs').argv;
  19. const moment = require('moment');
  20. const IS_DEBUG = !!args.debug;
  21. console.log('IS_DEBUG: ', IS_DEBUG);
  22. console.log('--------------------');
  23. const TPL_FILE_INFO = 'echo \'> (DEBUG ' + (IS_DEBUG ? 'on' : 'off') + ') <%= file.path %>\'';
  24. const plist_fn = path.join(__dirname, 'app/SH3/MacGap/SwitchHosts!-Info.plist');
  25. const output = {
  26. // 汉字 -> unicode
  27. ascii_only: true
  28. };
  29. gulp.task('zip', function () {
  30. const config = require('./app/src/config');
  31. let dir = args.dir;
  32. if (!dir || !fs.existsSync(dir)) {
  33. console.log(`bad --dir: "${dir}"`);
  34. return;
  35. }
  36. let c = fs.readFileSync(plist_fn, 'utf-8');
  37. let m = c.match(/CFBundleVersion[^\d]*?(\d+)/);
  38. if (!m) {
  39. console.log('CFBundleVersion not found!');
  40. return;
  41. }
  42. let v = m[1];
  43. let fn = `SwitchHosts!_v${config.VERSION}.${v}_for_mac.zip`;
  44. console.log('fn', fn);
  45. gulp.src('app/src/main.js')
  46. .pipe(shell([
  47. `cd ${dir} && zip -ry ${fn} ./SwitchHosts!.app`
  48. ]))
  49. ;
  50. });
  51. gulp.task('ver', function () {
  52. const fn_config = './app/src/config.js';
  53. const config = require('./app/src/config');
  54. let c = fs.readFileSync(plist_fn, 'utf-8');
  55. let m;
  56. let v;
  57. let v2;
  58. m = c.match(/CFBundleShortVersionString[^\d]*?([\d\.]+)/);
  59. if (!m) {
  60. console.log('CFBundleShortVersionString not found!');
  61. return;
  62. }
  63. v = m[1];
  64. console.log(`short version: ${v} -> ${config.VERSION}`);
  65. c = c.replace(/(CFBundleShortVersionString[^\d]*?)([\d\.]+)/, `$1${config.VERSION}`);
  66. m = c.match(/CFBundleVersion[^\d]*?(\d+)/);
  67. if (!m) {
  68. console.log('CFBundleVersion not found!');
  69. return;
  70. }
  71. v = parseInt(m[1]);
  72. v2 = v + 1;
  73. console.log(`version: ${v} -> ${v2}`);
  74. c = c.replace(/(CFBundleVersion[^\d]*?)(\d+)/, `$1${v2}`);
  75. //console.log(c);
  76. fs.writeFileSync(plist_fn, c, 'utf-8');
  77. c = fs.readFileSync(fn_config, 'utf-8');
  78. c = c.replace(/(bundle_version:\s*)(\d+)/, `$1${v2}`);
  79. fs.writeFileSync(fn_config, c, 'utf-8');
  80. });
  81. gulp.task('js', ['ver'], function () {
  82. const config = require('./app/src/config');
  83. let s = gulp.src(['app/src/**/*.js'])
  84. .pipe(babel({
  85. presets: ['es2015']
  86. }))
  87. .pipe(gulpif(!IS_DEBUG, replace({
  88. patterns: [{
  89. match: /\/\/\s*\$-FOR-TEST[\s\S]*?\/\/\s*\$-END/ig,
  90. replacement: ''
  91. }]
  92. })))
  93. .pipe(gulp.dest('tmp/'));
  94. s.on('end', () => {
  95. gulp.src(['tmp/main.js'])
  96. .pipe(shell(TPL_FILE_INFO))
  97. .pipe(browserify({
  98. debug: IS_DEBUG
  99. }))
  100. .pipe(gulpif(!IS_DEBUG, uglify({
  101. output: output,
  102. compress: {
  103. drop_console: true
  104. }
  105. })))
  106. .pipe(header(`/* ${moment().format('YYYY-MM-DD HH:mm:ss')} v${config.bundle_version} */\n`))
  107. .pipe(gulp.dest('app/SH3/public/js'))
  108. ;
  109. });
  110. });
  111. gulp.task('default', function () {
  112. gulp.start('js');
  113. gulp.watch('app/src/**/*.js', ['js']);
  114. });