gulpfile.js 2.4 KB

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