All files / lib/utils cleanup-log-files.js

100% Statements 18/18
100% Branches 8/8
100% Functions 4/4
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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