gulpfile.js 9.1 KB

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