gulpfile.js 7.0 KB

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