File size: 1,357 Bytes
a2b2aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Helper from './helper.js'
import { promises as fs } from 'fs'
import { tmpdir, platform } from 'os'
import { join } from 'path'

const TIME = 1000 * 60 * 3

const __dirname = Helper.__dirname(import.meta)

export default async function clearTmp() {
    const tmp = [tmpdir(), join(__dirname, '../tmp')]
    const filename = []

    await Promise.allSettled(tmp.map(async (dir) => {
        const files = await fs.readdir(dir)
        for (const file of files) filename.push(join(dir, file))
    }))

    return await Promise.allSettled(filename.map(async (file) => {
        const stat = await fs.stat(file)
        if (stat.isFile() && (Date.now() - stat.mtimeMs >= TIME)) {
            // https://stackoverflow.com/questions/28588707/node-js-check-if-a-file-is-open-before-copy
            if (platform() === 'win32') {
                // https://github.com/nodejs/node/issues/20548
                // https://nodejs.org/api/fs.html#filehandleclose
                let fileHandle
                try {
                    fileHandle = await fs.open(file, 'r+')
                } catch (e) {
                    console.error('[clearTmp]', e, 'Skipping', file)
                    return e
                } finally {
                    await fileHandle?.close()
                }
            }
            await fs.unlink(file)
        }
    }))
}