node/deps/npm/lib/utils/cleanup-log-files.js
Luigi Pinca dc05c70c8f
deps: upgrade npm to 7.0.7
PR-URL: https://github.com/nodejs/node/pull/35908
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Ruy Adorno <ruyadorno@github.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
2020-11-02 16:18:50 -05:00

33 lines
948 B
JavaScript

// 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()
})
}
})
})
}