gulpfile.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 'node_modules/@isomorphic-git/lightning-fs/dist/lightning-fs.min.js'
  61. ]).pipe(gulp.dest(path.join(outputPath, 'js'))),
  62. () => gulp.src([
  63. 'node_modules/magic-portal/dist/index.umd.js',
  64. ]).pipe(rename('magic_portal.js')).pipe(gulp.dest(path.join(outputPath, 'js'))),
  65. () => gulp.src([
  66. 'node_modules/@tabler/icons/iconfont/tabler-icons.min.css',
  67. 'node_modules/inter-ui/inter.css',
  68. 'node_modules/reveal.js/dist/theme/fonts/source-sans-pro/**',
  69. ]).pipe(gulp.dest(path.join(outputPath, 'css'))),
  70. () => gulp.src('node_modules/inter-ui/Inter (web)/*.*')
  71. .pipe(gulp.dest(path.join(outputPath, 'css', 'Inter (web)'))),
  72. () => gulp.src([
  73. 'node_modules/@tabler/icons/iconfont/fonts/**',
  74. 'node_modules/katex/dist/fonts/*.woff2'
  75. ]).pipe(gulp.dest(path.join(outputPath, 'css', 'fonts'))),
  76. )(...params)
  77. },
  78. keepSyncResourceFile () {
  79. return gulp.watch(resourceFilePath, { ignoreInitial: true }, common.syncResourceFile)
  80. },
  81. syncAllStatic () {
  82. return gulp.src([
  83. outputFilePath,
  84. '!' + path.join(outputPath, 'node_modules/**')
  85. ]).pipe(gulp.dest(publicStaticPath))
  86. },
  87. syncJS_CSSinRt () {
  88. return gulp.src([
  89. path.join(outputPath, 'js/**'),
  90. path.join(outputPath, 'css/**')
  91. ], { base: outputPath }).pipe(gulp.dest(publicStaticPath))
  92. },
  93. keepSyncStaticInRt () {
  94. return gulp.watch([
  95. path.join(outputPath, 'js/**'),
  96. path.join(outputPath, 'css/**')
  97. ], { ignoreInitial: true }, common.syncJS_CSSinRt)
  98. },
  99. async runCapWithLocalDevServerEntry (cb) {
  100. const mode = process.env.PLATFORM || 'ios'
  101. const IP = ip.address()
  102. const LOGSEQ_APP_SERVER_URL = `http://${IP}:3001`
  103. if (typeof global.fetch === 'function') {
  104. try {
  105. await fetch(LOGSEQ_APP_SERVER_URL)
  106. } catch (e) {
  107. return cb(new Error(`/* ❌ Please check if the service is ON. (${LOGSEQ_APP_SERVER_URL}) ❌ */`))
  108. }
  109. }
  110. console.log(`------ Cap ${mode.toUpperCase()} -----`)
  111. console.log(`Dev serve at: ${LOGSEQ_APP_SERVER_URL}`)
  112. console.log(`--------------------------------------`)
  113. cp.execSync(`npx cap sync ${mode}`, {
  114. stdio: 'inherit',
  115. env: Object.assign(process.env, {
  116. LOGSEQ_APP_SERVER_URL
  117. })
  118. })
  119. cp.execSync(`rm -rf ios/App/App/public/static/out`, {
  120. stdio: 'inherit'
  121. })
  122. cp.execSync(`npx cap run ${mode} --external`, {
  123. stdio: 'inherit',
  124. env: Object.assign(process.env, {
  125. LOGSEQ_APP_SERVER_URL
  126. })
  127. })
  128. cb()
  129. }
  130. }
  131. exports.electron = () => {
  132. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  133. cp.execSync('yarn', {
  134. cwd: outputPath,
  135. stdio: 'inherit'
  136. })
  137. }
  138. cp.execSync('yarn electron:dev', {
  139. cwd: outputPath,
  140. stdio: 'inherit'
  141. })
  142. }
  143. exports.electronMaker = async () => {
  144. cp.execSync('yarn cljs:release-electron', {
  145. stdio: 'inherit'
  146. })
  147. const pkgPath = path.join(outputPath, 'package.json')
  148. const pkg = require(pkgPath)
  149. const version = fs.readFileSync(path.join(__dirname, 'src/main/frontend/version.cljs'))
  150. .toString().match(/[0-9.]{3,}/)[0]
  151. if (!version) {
  152. throw new Error('release version error in src/**/*/version.cljs')
  153. }
  154. pkg.version = version
  155. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
  156. if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
  157. cp.execSync('yarn', {
  158. cwd: outputPath,
  159. stdio: 'inherit'
  160. })
  161. }
  162. cp.execSync('yarn electron:make', {
  163. cwd: outputPath,
  164. stdio: 'inherit'
  165. })
  166. }
  167. exports.cap = common.runCapWithLocalDevServerEntry
  168. exports.clean = common.clean
  169. exports.watch = gulp.series(common.syncResourceFile, common.syncAssetFiles, common.syncAllStatic,
  170. gulp.parallel(common.keepSyncResourceFile, css.watchCSS))
  171. exports.build = gulp.series(common.clean, common.syncResourceFile, common.syncAssetFiles, css.buildCSS)