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

123456789101112131415161718192021222324252627282930
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const Settings = require('@overleaf/settings')
  4. module.exports = function invalidateBabelCacheIfNeeded() {
  5. const cachePath = Path.join(__dirname, '../../node_modules/.cache')
  6. const statePath = Path.join(cachePath, 'last-overleafModuleImports.json')
  7. let lastState = ''
  8. try {
  9. lastState = fs.readFileSync(statePath, { encoding: 'utf-8' })
  10. } catch (e) {}
  11. const newState = JSON.stringify(Settings.overleafModuleImports)
  12. if (lastState !== newState) {
  13. // eslint-disable-next-line no-console
  14. console.warn(
  15. 'Detected change in overleafModuleImports, purging babel cache!'
  16. )
  17. // Gracefully handle cache mount in Server Pro build, only purge nested folders and keep .cache/ folder.
  18. fs.mkdirSync(cachePath, { recursive: true })
  19. for (const name of fs.readdirSync(cachePath)) {
  20. fs.rmSync(Path.join(cachePath, name), {
  21. recursive: true,
  22. force: true,
  23. maxRetries: 5,
  24. })
  25. }
  26. fs.writeFileSync(statePath, newState)
  27. }
  28. }