gulpfile.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 sourcePath = path.join(__dirname, 'src/main/frontend')
  12. const resourceFilePath = path.join(resourcesPath, '**')
  13. const css = {
  14. watchCSS () {
  15. return exec(`yarn css:watch`, {})
  16. },
  17. buildCSS (...params) {
  18. return gulp.series(
  19. () => exec(`yarn css:build`, {}),
  20. css._optimizeCSSForRelease)(...params)
  21. },
  22. _optimizeCSSForRelease () {
  23. return gulp.src(path.join(outputPath, 'css', 'style.css'))
  24. .pipe(cleanCSS())
  25. .pipe(gulp.dest(path.join(outputPath, 'css')))
  26. },
  27. }
  28. const common = {
  29. clean () {
  30. return del(['./static/**/*', '!./static/yarn.lock', '!./static/node_modules'])
  31. },
  32. syncResourceFile () {
  33. return gulp.src(resourceFilePath).pipe(gulp.dest(outputPath))
  34. },
  35. keepSyncResourceFile () {
  36. return gulp.watch(resourceFilePath, { ignoreInitial: false }, common.syncResourceFile)
  37. }
  38. }
  39. exports.electron = () => {
  40. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  41. cp.execSync('yarn', {
  42. cwd: outputPath,
  43. stdio: 'inherit'
  44. })
  45. }
  46. cp.execSync('yarn electron:dev', {
  47. cwd: outputPath,
  48. stdio: 'inherit'
  49. })
  50. }
  51. exports.electronMaker = async () => {
  52. cp.execSync('yarn cljs:release', {
  53. stdio: 'inherit'
  54. })
  55. const pkgPath = path.join(outputPath, 'package.json')
  56. const pkg = require(pkgPath)
  57. const version = fs.readFileSync(path.join(__dirname, 'src/main/frontend/version.cljs'))
  58. .toString().match(/[0-9.]{3,}/)[0]
  59. if (!version) {
  60. throw new Error('release version error in src/**/*/version.cljs')
  61. }
  62. pkg.version = version
  63. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
  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:make', {
  71. cwd: outputPath,
  72. stdio: 'inherit'
  73. })
  74. }
  75. exports.clean = common.clean
  76. exports.watch = gulp.parallel(common.keepSyncResourceFile, css.watchCSS)
  77. exports.build = gulp.series(common.clean, common.syncResourceFile, css.buildCSS)