gulpfile.js 8.4 KB

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