gulpfile.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const fs = require('fs')
  2. const utils = require('util')
  3. const cp = require('child_process')
  4. const exec = utils.promisify(cp.exec)
  5. const path = require('path')
  6. const gulp = require('gulp')
  7. const cleanCSS = require('gulp-clean-css')
  8. const del = require('del')
  9. const outputPath = path.join(__dirname, 'static')
  10. const resourcesPath = path.join(__dirname, 'resources')
  11. const publicStaticPath = path.join(__dirname, 'public/static')
  12. const sourcePath = path.join(__dirname, 'src/main/frontend')
  13. const resourceFilePath = path.join(resourcesPath, '**')
  14. const outputFilePath = path.join(outputPath, '**')
  15. const css = {
  16. watchCSS () {
  17. return cp.spawn(`yarn css:watch`, {
  18. shell: true,
  19. stdio: 'inherit'
  20. })
  21. },
  22. buildCSS (...params) {
  23. return gulp.series(
  24. () => exec(`yarn css:build`, {}),
  25. css._optimizeCSSForRelease
  26. )(...params)
  27. },
  28. _optimizeCSSForRelease () {
  29. return gulp.src(path.join(outputPath, 'css', 'style.css'))
  30. .pipe(cleanCSS())
  31. .pipe(gulp.dest(path.join(outputPath, 'css')))
  32. }
  33. }
  34. const common = {
  35. clean () {
  36. return del(['./static/**/*', '!./static/yarn.lock', '!./static/node_modules'])
  37. },
  38. syncResourceFile () {
  39. return gulp.src(resourceFilePath).pipe(gulp.dest(outputPath))
  40. },
  41. keepSyncResourceFile () {
  42. return gulp.watch(resourceFilePath, {
  43. ignoreInitial: true,
  44. usePolling: false // Don't know why but have to set explicitly, or high cpu usage
  45. }, common.syncResourceFile)
  46. },
  47. syncAllStatic () {
  48. return gulp.src([
  49. outputFilePath,
  50. '!' + path.join(outputPath, 'node_modules/**')
  51. ]).pipe(gulp.dest(publicStaticPath))
  52. },
  53. syncJS_CSSinRt () {
  54. return gulp.src([
  55. path.join(outputPath, 'js/**'),
  56. path.join(outputPath, 'css/**')
  57. ], { base: outputPath }).pipe(gulp.dest(publicStaticPath))
  58. },
  59. keepSyncStaticInRt () {
  60. return gulp.watch([
  61. path.join(outputPath, 'js/**'),
  62. path.join(outputPath, 'css/**')
  63. ], {
  64. ignoreInitial: true,
  65. usePolling: false // Don't know why but have to set explicitly, or high cpu usage
  66. }, common.syncJS_CSSinRt)
  67. }
  68. }
  69. exports.electron = () => {
  70. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  71. cp.execSync('yarn', {
  72. cwd: outputPath,
  73. stdio: 'inherit'
  74. })
  75. }
  76. cp.execSync('yarn electron:dev', {
  77. cwd: outputPath,
  78. stdio: 'inherit'
  79. })
  80. }
  81. exports.electronMaker = async () => {
  82. cp.execSync('yarn cljs:release-electron', {
  83. stdio: 'inherit'
  84. })
  85. const pkgPath = path.join(outputPath, 'package.json')
  86. const pkg = require(pkgPath)
  87. const version = fs.readFileSync(path.join(__dirname, 'src/main/frontend/version.cljs'))
  88. .toString().match(/[0-9.]{3,}/)[0]
  89. if (!version) {
  90. throw new Error('release version error in src/**/*/version.cljs')
  91. }
  92. pkg.version = version
  93. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
  94. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  95. cp.execSync('yarn', {
  96. cwd: outputPath,
  97. stdio: 'inherit'
  98. })
  99. }
  100. cp.execSync('yarn electron:make', {
  101. cwd: outputPath,
  102. stdio: 'inherit'
  103. })
  104. }
  105. exports.clean = common.clean
  106. exports.watch = gulp.series(common.syncResourceFile, common.syncAllStatic,
  107. gulp.parallel(common.keepSyncResourceFile, css.watchCSS, common.keepSyncStaticInRt))
  108. exports.build = gulp.series(common.clean, common.syncResourceFile, css.buildCSS)