gulpfile.js 10 KB

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