gulpfile.js 2.9 KB

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