cleanup-log-files.js 948 B

1234567891011121314151617181920212223242526272829303132
  1. // module to clean out the old log files in cache/_logs
  2. // this is a best-effort attempt. if a rm fails, we just
  3. // log a message about it and move on. We do return a
  4. // Promise that succeeds when we've tried to delete everything,
  5. // just for the benefit of testing this function properly.
  6. const { resolve } = require('path')
  7. const rimraf = require('rimraf')
  8. const glob = require('glob')
  9. module.exports = (cache, max, warn) => {
  10. /* eslint-disable promise/param-names */
  11. return new Promise(done => {
  12. glob(resolve(cache, '_logs', '*-debug.log'), (er, files) => {
  13. if (er)
  14. return done()
  15. let pending = files.length - max
  16. if (pending <= 0)
  17. return done()
  18. for (let i = 0; i < files.length - max; i++) {
  19. rimraf(files[i], (er) => {
  20. if (er)
  21. warn('log', 'failed to remove log file', files[i])
  22. if (--pending === 0)
  23. done()
  24. })
  25. }
  26. })
  27. })
  28. }