gulpfile.js 7.5 KB

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