-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaper.js
167 lines (147 loc) · 3.78 KB
/
reaper.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Clean out duplicate data files
* Keep the 2 most recent
* Delete all other duplicates,
* saving the oldest
*/
const config = require('config')
const debug = require('debug')('reaper')
const path = require('path')
const glob = require('glob')
const fs = require('fs')
const { cwd } = require('process')
const md5File = require('md5-file')
const { program } = require('commander')
program
.option('-d, --dry-run', 'Dry run (i.e. do not delete any files)')
.option('-r, --run', 'Real run (i.e. do delete any files)')
.option('--dir <folder>', 'Data Directory')
.option(
'-s, --status <value>',
'Additional status to watch (default: [open, merged])'
)
.option('-q, --quiet', 'Turn off info messages')
.option('-x, --exclude <filename>', 'Additional file to ignore/exclude')
.option('-v, --verbose', 'Turn on extra info messages')
program.parse(process.argv)
const options = program.opts()
let dryRun = options.dryRun
? true
: config.has('reaper') && config.reaper.has('dryRun')
? config.reaper.dryRun
: false
if (options.run) {
if (options.dryRun) {
console.error(`Cannot specify dry-run and run at the same time.`)
process.exit(101)
} else {
dryRun = false
}
}
const statusList = ['open', 'merged']
if (options.status) {
statusList.push(options.status)
}
if (options.quiet) {
debug.enabled = false
}
if (options.verbose) {
debug.enabled = true
}
let excludeFiles = []
if (options.exclude) {
excludeFiles.push(options.exclude)
}
let dataDir = false
if (options.dir) {
debug(`options.dir: ${options.dir}`)
dataDir = options.dir
} else {
dataDir = config.dataDir
}
if (!dataDir) {
console.error(`No data directory specified.`)
process.exit(-102)
} else if (!fs.existsSync(dataDir)) {
console.error(`Data Directory ${dataDir} not found.`)
process.exit(-103)
}
debug(`Config/Options:
- dryRun: ${dryRun}
- data dir: ${dataDir}
- statusList: ${statusList.join(',')}
- exclude: ${excludeFiles.join(',')}
- verbose: ${debug.enabled}
`)
// 1. Get the list of files to exclude
debug(`chdir(${dataDir})...`)
process.chdir(dataDir)
let log = JSON.parse(
fs.readFileSync(config.monitor.log.filename + config.dataFileExt)
)
// Update the exclude file list
statusList.forEach((status) => {
excludeFiles.push(
config.monitor.log.filename +
'-' +
status +
'-' +
log.latest.timestamp +
config.dataFileExt
)
excludeFiles.push(
config.monitor.log.filename +
'-' +
status +
'-' +
log.previous.timestamp +
config.dataFileExt
)
})
debug(excludeFiles)
let keep = []
let skipped = []
let deleted = []
// 2. Process the full list of files
statusList.forEach((status) => {
let flist = glob.sync(`${config.monitor.log.filename}-${status}*.json`)
debug(`flist: `, flist)
// 2. Calc md5sums
let lastHash = false
let lastFile = false
flist.forEach((f) => {
if (excludeFiles.includes(f)) {
debug(`skipping ${f} / excluded file`)
skipped.push(f)
} else {
// process
let hash = md5File.sync(f)
if (hash == lastHash) {
debug(`deleting ${f} / hash matches ${lastFile}`)
deleted.push(f)
if (dryRun) {
debug(`\t>>> Dry Run: Not deleting file ${f}`)
} else {
try {
fs.unlinkSync(f)
} catch (err) {
console.error(`Couldn't delete file ${f}: ${err}`)
}
}
} else {
debug(`keeping ${f}`)
keep.push(f)
}
lastHash = hash
lastFile = f
}
})
})
// 3. Print the summary
console.log(`Keeping ${keep.length} files:\n- ${keep.join('\n- ')}`)
console.log(`Skipped ${skipped.length} files:\n- ${skipped.join('\n- ')}`)
console.log(
`Deleted ${deleted.length} files${
dryRun ? ' *** DRY RUN ONLY ***' : ''
}:\n- ${deleted.join('\n- ')}`
)