|
1 | 1 | const os = require('os')
|
2 | 2 | const { join, dirname, basename } = require('path')
|
3 | 3 | const { format } = require('util')
|
4 |
| -const { glob } = require('glob') |
5 | 4 | const { Minipass } = require('minipass')
|
6 | 5 | const fsMiniPass = require('fs-minipass')
|
7 | 6 | const fs = require('fs/promises')
|
8 | 7 | const log = require('./log-shim')
|
9 | 8 | const Display = require('./display')
|
10 | 9 |
|
11 | 10 | const padZero = (n, length) => n.toString().padStart(length.toString().length, '0')
|
12 |
| -const globify = pattern => pattern.split('\\').join('/') |
13 | 11 |
|
14 | 12 | class LogFiles {
|
15 | 13 | // Default to a plain minipass stream so we can buffer
|
@@ -199,25 +197,49 @@ class LogFiles {
|
199 | 197 |
|
200 | 198 | try {
|
201 | 199 | const logPath = this.#getLogFilePath()
|
202 |
| - const logGlob = join(dirname(logPath), basename(logPath) |
| 200 | + const patternFileName = basename(logPath) |
203 | 201 | // tell glob to only match digits
|
204 |
| - .replace(/\d/g, '[0123456789]') |
| 202 | + .replace(/\d/g, 'd') |
205 | 203 | // Handle the old (prior to 8.2.0) log file names which did not have a
|
206 | 204 | // counter suffix
|
207 |
| - .replace(/-\.log$/, '*.log') |
208 |
| - ) |
| 205 | + .replace('-.log', '') |
| 206 | + |
| 207 | + let files = await fs.readdir( |
| 208 | + dirname(logPath), { |
| 209 | + withFileTypes: true, |
| 210 | + encoding: 'utf-8', |
| 211 | + }) |
| 212 | + files = files.sort((a, b) => basename(a.name).localeCompare(basename(b.name), 'en')) |
| 213 | + |
| 214 | + const logFiles = [] |
| 215 | + |
| 216 | + for (const file of files) { |
| 217 | + if (!file.isFile()) { |
| 218 | + continue |
| 219 | + } |
| 220 | + |
| 221 | + const genericFileName = file.name.replace(/\d/g, 'd') |
| 222 | + const filePath = join(dirname(logPath), basename(file.name)) |
| 223 | + |
| 224 | + // Always ignore the currently written files |
| 225 | + if ( |
| 226 | + genericFileName.includes(patternFileName) |
| 227 | + && genericFileName.endsWith('.log') |
| 228 | + && !this.#files.includes(filePath) |
| 229 | + ) { |
| 230 | + logFiles.push(filePath) |
| 231 | + } |
| 232 | + } |
209 | 233 |
|
210 |
| - // Always ignore the currently written files |
211 |
| - const files = await glob(globify(logGlob), { ignore: this.#files.map(globify), silent: true }) |
212 |
| - const toDelete = files.length - this.#logsMax |
| 234 | + const toDelete = logFiles.length - this.#logsMax |
213 | 235 |
|
214 | 236 | if (toDelete <= 0) {
|
215 | 237 | return
|
216 | 238 | }
|
217 | 239 |
|
218 | 240 | log.silly('logfile', `start cleaning logs, removing ${toDelete} files`)
|
219 | 241 |
|
220 |
| - for (const file of files.slice(0, toDelete)) { |
| 242 | + for (const file of logFiles.slice(0, toDelete)) { |
221 | 243 | try {
|
222 | 244 | await fs.rm(file, { force: true })
|
223 | 245 | } catch (e) {
|
|
0 commit comments