gulpfile.js 8.5 KB

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