()
| 8 | const __dirname = Helper.__dirname(import.meta) |
| 9 | |
| 10 | export default async function clearTmp() { |
| 11 | const tmp = [tmpdir(), join(__dirname, '../tmp')] |
| 12 | const deletedFiles = [] // Array to store deleted file paths |
| 13 | |
| 14 | await Promise.allSettled( |
| 15 | tmp.map(async dir => { |
| 16 | const files = await fs.readdir(dir) |
| 17 | for (const file of files) { |
| 18 | if (!file.endsWith('.file')) { |
| 19 | const filePath = join(dir, file) |
| 20 | const stat = await fs.stat(filePath) |
| 21 | if (stat.isFile() && Date.now() - stat.mtimeMs >= maxtime) { |
| 22 | // Check if the file can be opened |
| 23 | if (platform() === 'win32') { |
| 24 | let fileHandle |
| 25 | try { |
| 26 | fileHandle = await fs.open(filePath, 'r+') |
| 27 | } catch (e) { |
| 28 | // Log the error but continue with other files |
| 29 | console.error('[clearTmp] Error opening file:', e.message) |
| 30 | continue |
| 31 | } finally { |
| 32 | await fileHandle?.close() |
| 33 | } |
| 34 | } |
| 35 | // Delete the file |
| 36 | await fs.unlink(filePath) |
| 37 | deletedFiles.push(filePath) // Add the deleted file to the array |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | }) |
| 42 | ) |
| 43 | |
| 44 | // Log the number of deleted files |
| 45 | console.log(`[clearTmp] Deleted ${deletedFiles.length} files.`) |
| 46 | } |
nothing calls this directly
no outgoing calls
no test coverage detected