gulpfile.js 3.0 KB

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