2024-01-04 07:31:13 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const Path = require('path')
|
|
|
|
const Settings = require('@overleaf/settings')
|
|
|
|
|
|
|
|
module.exports = function invalidateBabelCacheIfNeeded() {
|
|
|
|
const cachePath = Path.join(__dirname, '../../node_modules/.cache')
|
|
|
|
const statePath = Path.join(cachePath, 'last-overleafModuleImports.json')
|
|
|
|
let lastState = ''
|
|
|
|
try {
|
|
|
|
lastState = fs.readFileSync(statePath, { encoding: 'utf-8' })
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
const newState = JSON.stringify(Settings.overleafModuleImports)
|
|
|
|
if (lastState !== newState) {
|
2024-01-30 11:44:42 -05:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn(
|
2024-01-04 07:31:13 -05:00
|
|
|
'Detected change in overleafModuleImports, purging babel cache!'
|
|
|
|
)
|
2024-08-06 05:08:18 -04:00
|
|
|
// Gracefully handle cache mount in Server Pro build, only purge nested folders and keep .cache/ folder.
|
|
|
|
fs.mkdirSync(cachePath, { recursive: true })
|
|
|
|
for (const name of fs.readdirSync(cachePath)) {
|
|
|
|
fs.rmSync(Path.join(cachePath, name), {
|
|
|
|
recursive: true,
|
|
|
|
force: true,
|
|
|
|
maxRetries: 5,
|
|
|
|
})
|
|
|
|
}
|
2024-01-04 07:31:13 -05:00
|
|
|
fs.writeFileSync(statePath, newState)
|
|
|
|
}
|
|
|
|
}
|