gulpfile.js 7.6 KB

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