invalidate-babel-cache-if-needed.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const Settings = require('@overleaf/settings')
  4. const cypressCacheVariant = require('./cypress-cache-variant')
  5. module.exports = function invalidateBabelCacheIfNeeded() {
  6. // Use a unique subdirectory per Cypress CT variant to avoid parallel jobs
  7. // racing on the shared babel cache and state file.
  8. const suffix = cypressCacheVariant() ?? ''
  9. const cacheDir = Path.join(__dirname, '../../node_modules/.cache', suffix)
  10. const cachePath = Path.join(cacheDir, 'babel-loader')
  11. const statePath = Path.join(cacheDir, 'last-overleafModuleImports.json')
  12. let lastState = ''
  13. try {
  14. lastState = fs.readFileSync(statePath, { encoding: 'utf-8' })
  15. } catch (e) {}
  16. const newState = JSON.stringify(Settings.overleafModuleImports)
  17. if (lastState !== newState) {
  18. // eslint-disable-next-line no-console
  19. console.warn(
  20. 'Detected change in overleafModuleImports, purging babel cache!'
  21. )
  22. // Gracefully handle cache mount in Server Pro build, only purge nested folder and keep .cache/ folder.
  23. fs.mkdirSync(cacheDir, { recursive: true })
  24. fs.rmSync(cachePath, {
  25. recursive: true,
  26. force: true,
  27. maxRetries: 5,
  28. })
  29. fs.writeFileSync(statePath, newState)
  30. }
  31. }