const fs = require('fs')
let filedir = '/usr/local/fxk/static/'
const filename = process.argv[2]
let new_filename = filename
if (new_filename.indexOf('-h5') > -1) {
new_filename = new_filename.replace('-h5', '')
}
if (new_filename.indexOf('-tag') > -1) {
new_filename = new_filename.replace('-tag', '')
}
filedir = filedir + new_filename + '/'
function sortNumber(a,b){
return a - b
}
fs.readdir(filedir, (err, data) => {
if (err) throw err
if (data.length > 5) {
var list = []
var isTag = false
if (filename.indexOf('-tag') > -1) {
isTag = true
}
data.forEach((key) => {
if (!isTag && key.indexOf('-tag') > -1) return
if (key.indexOf(filename) > -1 && key.indexOf('tar.gz') > -1) {
const f = key.split('.tar.gz')[0]
const s = f.split('-')
const q = s[s.length - 1]
list.push(q)
}
})
}else {
return
}
if (list.length >= 10) {
delFc(list.sort(sortNumber))
}
})
function delFc(list) {
const _list = list.slice(0, 5)
_list.forEach(key => {
const name = `${filedir}${filename}-${key}.tar.gz`
fs.unlink(name, error => {
if (error) throw error
console.log(`${name}删除成功~`)
})
})
}