gulpfile.js 3.4 KB

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