gulpfile.js 10 KB

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