25 lines
571 B
TypeScript
25 lines
571 B
TypeScript
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { WPopContext } from "./context";
|
|
|
|
export type TempDir = {
|
|
path: string;
|
|
cleanup: () => void;
|
|
};
|
|
|
|
export function createTempDir(context: WPopContext, prefix: string): TempDir {
|
|
if (context.dryRun) {
|
|
return {
|
|
path: join(tmpdir(), prefix),
|
|
cleanup: () => {},
|
|
};
|
|
}
|
|
|
|
const path = mkdtempSync(join(tmpdir(), `${prefix}-`));
|
|
return {
|
|
path,
|
|
cleanup: () => rmSync(path, { force: true, recursive: true }),
|
|
};
|
|
}
|