gulpfile.js 6.7 KB

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