2023-10-23 23:16:24 +02:00
|
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
import fs from "node:fs"
|
|
|
|
import crypto from "node:crypto"
|
2022-09-22 16:16:21 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { docker } from "./Docker.js"
|
2022-09-22 16:16:21 +02:00
|
|
|
|
|
|
|
export class TemporaryFolder {
|
|
|
|
public readonly id: string
|
|
|
|
public readonly path: string
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.id = crypto.randomUUID()
|
|
|
|
this.path = fileURLToPath(new URL(`../../temp/${this.id}`, import.meta.url))
|
|
|
|
}
|
|
|
|
|
|
|
|
public async create(): Promise<void> {
|
|
|
|
await fs.promises.mkdir(this.path, { recursive: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
public async delete(): Promise<void> {
|
|
|
|
await fs.promises.rm(this.path, { recursive: true, force: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async cleanAll(): Promise<void> {
|
|
|
|
try {
|
2023-01-10 23:15:36 +01:00
|
|
|
const temporaryPath = fileURLToPath(
|
2023-10-23 23:16:24 +02:00
|
|
|
new URL("../../temp", import.meta.url),
|
2023-01-10 23:15:36 +01:00
|
|
|
)
|
2022-09-22 16:16:21 +02:00
|
|
|
await fs.promises.rm(temporaryPath, { recursive: true, force: true })
|
|
|
|
await docker.removeImages()
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
}
|