gulpfile.js 9.2 KB

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