gulpfile.js 10 KB

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