gulpfile.js 6.8 KB

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