gulpfile.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 ip = require('ip')
  10. const outputPath = path.join(__dirname, 'static')
  11. const resourcesPath = path.join(__dirname, 'resources')
  12. const publicStaticPath = path.join(__dirname, 'public/static')
  13. const sourcePath = path.join(__dirname, 'src/main/frontend')
  14. const resourceFilePath = path.join(resourcesPath, '**')
  15. const outputFilePath = path.join(outputPath, '**')
  16. const css = {
  17. watchCSS () {
  18. return cp.spawn(`yarn css:watch`, {
  19. shell: true,
  20. stdio: 'inherit'
  21. })
  22. },
  23. buildCSS (...params) {
  24. return gulp.series(
  25. () => exec(`yarn css:build`, {}),
  26. css._optimizeCSSForRelease
  27. )(...params)
  28. },
  29. _optimizeCSSForRelease () {
  30. return gulp.src(path.join(outputPath, 'css', 'style.css'))
  31. .pipe(cleanCSS())
  32. .pipe(gulp.dest(path.join(outputPath, 'css')))
  33. }
  34. }
  35. const common = {
  36. clean () {
  37. return del(['./static/**/*', '!./static/yarn.lock', '!./static/node_modules'])
  38. },
  39. syncResourceFile () {
  40. return gulp.src(resourceFilePath).pipe(gulp.dest(outputPath))
  41. },
  42. // NOTE: All assets from node_modules are copied to the output directory
  43. syncAssetFiles (...params) {
  44. return gulp.series(
  45. () => gulp.src([
  46. './node_modules/@excalidraw/excalidraw/dist/excalidraw-assets/**',
  47. '!**/*/i18n-*.js'
  48. ])
  49. .pipe(gulp.dest(path.join(outputPath, 'js', 'excalidraw-assets'))),
  50. () => gulp.src('node_modules/katex/dist/katex.min.js')
  51. .pipe(gulp.dest(path.join(outputPath, 'js'))),
  52. () => gulp.src('node_modules/html2canvas/dist/html2canvas.min.js')
  53. .pipe(gulp.dest(path.join(outputPath, 'js'))),
  54. () => gulp.src('node_modules/swiped-events/dist/swiped-events.min.js')
  55. .pipe(gulp.dest(path.join(outputPath, 'js'))),
  56. () => gulp.src('node_modules/interactjs/dist/interact.min.js')
  57. .pipe(gulp.dest(path.join(outputPath, 'js'))),
  58. () => gulp.src('node_modules/photoswipe/dist/umd/*.js')
  59. .pipe(gulp.dest(path.join(outputPath, 'js'))),
  60. () => gulp.src('node_modules/@tabler/icons/iconfont/tabler-icons.min.css')
  61. .pipe(gulp.dest(path.join(outputPath, 'css'))),
  62. () => gulp.src('node_modules/inter-ui/inter.css')
  63. .pipe(gulp.dest(path.join(outputPath, 'css'))),
  64. () => gulp.src('node_modules/inter-ui/Inter (web)/*.*')
  65. .pipe(gulp.dest(path.join(outputPath, 'css', 'Inter (web)'))),
  66. () => gulp.src([
  67. 'node_modules/@tabler/icons/iconfont/fonts/**',
  68. 'node_modules/katex/dist/fonts/*.woff2'
  69. ])
  70. .pipe(gulp.dest(path.join(outputPath, 'css', 'fonts'))),
  71. )(...params)
  72. },
  73. keepSyncResourceFile () {
  74. return gulp.watch(resourceFilePath, { ignoreInitial: true }, common.syncResourceFile)
  75. },
  76. syncAllStatic () {
  77. return gulp.src([
  78. outputFilePath,
  79. '!' + path.join(outputPath, 'node_modules/**')
  80. ]).pipe(gulp.dest(publicStaticPath))
  81. },
  82. syncJS_CSSinRt () {
  83. return gulp.src([
  84. path.join(outputPath, 'js/**'),
  85. path.join(outputPath, 'css/**')
  86. ], { base: outputPath }).pipe(gulp.dest(publicStaticPath))
  87. },
  88. keepSyncStaticInRt () {
  89. return gulp.watch([
  90. path.join(outputPath, 'js/**'),
  91. path.join(outputPath, 'css/**')
  92. ], { ignoreInitial: true }, common.syncJS_CSSinRt)
  93. },
  94. async runCapWithLocalDevServerEntry (cb) {
  95. const mode = process.env.PLATFORM || 'ios'
  96. const IP = ip.address()
  97. const LOGSEQ_APP_SERVER_URL = `http://${IP}:3001`
  98. if (typeof global.fetch === 'function') {
  99. try {
  100. await fetch(LOGSEQ_APP_SERVER_URL)
  101. } catch (e) {
  102. return cb(new Error(`/* ❌ Please check if the service is ON. (${LOGSEQ_APP_SERVER_URL}) ❌ */`))
  103. }
  104. }
  105. console.log(`------ Cap ${mode.toUpperCase()} -----`)
  106. console.log(`Dev serve at: ${LOGSEQ_APP_SERVER_URL}`)
  107. console.log(`--------------------------------------`)
  108. cp.execSync(`npx cap sync ${mode}`, {
  109. stdio: 'inherit',
  110. env: Object.assign(process.env, {
  111. LOGSEQ_APP_SERVER_URL
  112. })
  113. })
  114. cp.execSync(`rm -rf ios/App/App/public/static/out`, {
  115. stdio: 'inherit'
  116. })
  117. cp.execSync(`npx cap run ${mode} --external`, {
  118. stdio: 'inherit',
  119. env: Object.assign(process.env, {
  120. LOGSEQ_APP_SERVER_URL
  121. })
  122. })
  123. cb()
  124. }
  125. }
  126. exports.electron = () => {
  127. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  128. cp.execSync('yarn', {
  129. cwd: outputPath,
  130. stdio: 'inherit'
  131. })
  132. }
  133. cp.execSync('yarn electron:dev', {
  134. cwd: outputPath,
  135. stdio: 'inherit'
  136. })
  137. }
  138. exports.electronMaker = async () => {
  139. cp.execSync('yarn cljs:release-electron', {
  140. stdio: 'inherit'
  141. })
  142. const pkgPath = path.join(outputPath, 'package.json')
  143. const pkg = require(pkgPath)
  144. const version = fs.readFileSync(path.join(__dirname, 'src/main/frontend/version.cljs'))
  145. .toString().match(/[0-9.]{3,}/)[0]
  146. if (!version) {
  147. throw new Error('release version error in src/**/*/version.cljs')
  148. }
  149. pkg.version = version
  150. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
  151. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  152. cp.execSync('yarn', {
  153. cwd: outputPath,
  154. stdio: 'inherit'
  155. })
  156. }
  157. cp.execSync('yarn electron:make', {
  158. cwd: outputPath,
  159. stdio: 'inherit'
  160. })
  161. }
  162. exports.cap = common.runCapWithLocalDevServerEntry
  163. exports.clean = common.clean
  164. exports.watch = gulp.series(common.syncResourceFile, common.syncAssetFiles, common.syncAllStatic,
  165. gulp.parallel(common.keepSyncResourceFile, css.watchCSS))
  166. exports.build = gulp.series(common.clean, common.syncResourceFile, common.syncAssetFiles, css.buildCSS)