gulpfile.js 2.5 KB

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