invalidate-babel-cache-if-needed.js 815 B

123456789101112131415161718192021222324
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const logger = require('@overleaf/logger')
  4. const Settings = require('@overleaf/settings')
  5. module.exports = function invalidateBabelCacheIfNeeded() {
  6. const cachePath = Path.join(__dirname, '../../node_modules/.cache')
  7. const statePath = Path.join(cachePath, 'last-overleafModuleImports.json')
  8. let lastState = ''
  9. try {
  10. lastState = fs.readFileSync(statePath, { encoding: 'utf-8' })
  11. } catch (e) {}
  12. const newState = JSON.stringify(Settings.overleafModuleImports)
  13. if (lastState !== newState) {
  14. logger.warn(
  15. {},
  16. 'Detected change in overleafModuleImports, purging babel cache!'
  17. )
  18. fs.rmSync(cachePath, { recursive: true, force: true, maxRetries: 5 })
  19. fs.mkdirSync(cachePath)
  20. fs.writeFileSync(statePath, newState)
  21. }
  22. }