gulpfile.js 3.8 KB

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