gulpfile.js 5.9 KB

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