gulpfile.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, { ignoreInitial: true }, common.syncResourceFile)
  43. },
  44. syncAllStatic () {
  45. return gulp.src([
  46. outputFilePath,
  47. '!' + path.join(outputPath, 'node_modules/**')
  48. ]).pipe(gulp.dest(publicStaticPath))
  49. },
  50. syncJS_CSSinRt () {
  51. return gulp.src([
  52. path.join(outputPath, 'js/**'),
  53. path.join(outputPath, 'css/**')
  54. ], { base: outputPath }).pipe(gulp.dest(publicStaticPath))
  55. },
  56. keepSyncStaticInRt () {
  57. return gulp.watch([
  58. path.join(outputPath, 'js/**'),
  59. path.join(outputPath, 'css/**')
  60. ], { ignoreInitial: true }, common.syncJS_CSSinRt)
  61. }
  62. }
  63. exports.electron = () => {
  64. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  65. cp.execSync('yarn', {
  66. cwd: outputPath,
  67. stdio: 'inherit'
  68. })
  69. }
  70. cp.execSync('yarn electron:dev', {
  71. cwd: outputPath,
  72. stdio: 'inherit'
  73. })
  74. }
  75. exports.electronMaker = async () => {
  76. cp.execSync('yarn cljs:release-electron', {
  77. stdio: 'inherit'
  78. })
  79. const pkgPath = path.join(outputPath, 'package.json')
  80. const pkg = require(pkgPath)
  81. const version = fs.readFileSync(path.join(__dirname, 'src/main/frontend/version.cljs'))
  82. .toString().match(/[0-9.]{3,}/)[0]
  83. if (!version) {
  84. throw new Error('release version error in src/**/*/version.cljs')
  85. }
  86. pkg.version = version
  87. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
  88. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  89. cp.execSync('yarn', {
  90. cwd: outputPath,
  91. stdio: 'inherit'
  92. })
  93. }
  94. cp.execSync('yarn electron:make', {
  95. cwd: outputPath,
  96. stdio: 'inherit'
  97. })
  98. }
  99. exports.clean = common.clean
  100. exports.watch = gulp.series(common.syncResourceFile, common.syncAllStatic,
  101. gulp.parallel(common.keepSyncResourceFile, css.watchCSS, common.keepSyncStaticInRt))
  102. exports.build = gulp.series(common.clean, common.syncResourceFile, css.buildCSS)