manager.ts

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: GPL-3.0-or-later
 4
 5import { mkdtemp, rm, chmod } from "node:fs/promises";
 6import { tmpdir } from "node:os";
 7import { join } from "node:path";
 8import { WorkspaceError } from "../util/errors.js";
 9
10export interface Workspace {
11  path: string;
12  cleanup: () => Promise<void>;
13}
14
15export interface WorkspaceOptions {
16  cleanup: boolean;
17}
18
19export async function createWorkspace(options: WorkspaceOptions): Promise<Workspace> {
20  try {
21    const root = await mkdtemp(join(tmpdir(), "rumilo-"));
22    await chmod(root, 0o700);
23
24    const cleanup = async () => {
25      if (!options.cleanup) return;
26      await rm(root, { recursive: true, force: true });
27    };
28
29    return { path: root, cleanup };
30  } catch (error) {
31    throw new WorkspaceError(`Failed to create workspace: ${String(error)}`);
32  }
33}