Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions backend/src/services/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,47 @@ import { exec } from "child_process";
import fs from "fs/promises";
import path from "path";
import { promisify } from "util";
import * as processService from "./process";

const execAsync = promisify(exec);

export async function exportContainerCode(
containerId: string
): Promise<Buffer> {
const tempDir = `/tmp/export-${containerId}-${Date.now()}`;
export async function exportContainerCode(id: string): Promise<Buffer> {
const projectPath = processService.getProjectPath(id);
if (!projectPath) throw new Error("Prozess nicht gefunden");

const tempDir = `/tmp/export-${id}-${Date.now()}`;
const zipPath = `${tempDir}.zip`;

try {
await execAsync("pnpm build", { cwd: projectPath });
await fs.mkdir(tempDir, { recursive: true });

const copyCommand = `docker cp ${containerId}:/app/my-nextjs-app/. ${tempDir}/`;
await execAsync(copyCommand);

const nodeModulesPath = path.join(tempDir, "node_modules");
const nextPath = path.join(tempDir, ".next");

try {
await fs.rm(nodeModulesPath, { recursive: true, force: true });
} catch {}

try {
await fs.rm(nextPath, { recursive: true, force: true });
} catch {}

const zipCommand = `cd ${tempDir} && zip -r ${zipPath} . -x "*.DS_Store"`;
await execAsync(zipCommand);

await fs.cp(projectPath, tempDir, { recursive: true });

await fs.rm(path.join(tempDir, "node_modules"), {
recursive: true,
force: true,
});
await fs.rm(path.join(tempDir, ".next"), {
recursive: true,
force: true,
});

await execAsync(`zip -r ${zipPath} . -x "*.DS_Store"`, { cwd: tempDir });
const zipBuffer = await fs.readFile(zipPath);

await fs.rm(tempDir, { recursive: true, force: true });
await fs.rm(zipPath, { force: true });

return zipBuffer;
} catch (error) {
try {
await fs.rm(tempDir, { recursive: true, force: true });
await fs.rm(zipPath, { force: true });
} catch {}

throw new Error(
`Export failed: ${
`Export fehlgeschlagen: ${
error instanceof Error ? error.message : "Unknown error"
}`
);
}
}

48 changes: 48 additions & 0 deletions backend/src/services/file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, test, mock } from "bun:test";
import fs from "fs/promises";
import os from "os";
import path from "path";

test("file operations without Docker", async () => {
const projectPath = await fs.mkdtemp(path.join(os.tmpdir(), "project-"));
const id = "test-id";

mock.module("./process", () => ({
getProjectPath: () => projectPath,
}));

const {
getFileTree,
getFileContentTree,
writeFile,
readFile,
renameFile,
removeFile,
} = await import("./file");

try {
await writeFile(id, "dir/sample.txt", "hello");
const content = await readFile(id, "dir/sample.txt");
expect(content).toBe("hello");

await renameFile(id, "dir/sample.txt", "dir/renamed.txt");
const renamed = await readFile(id, "dir/renamed.txt");
expect(renamed).toBe("hello");

const tree = await getFileTree(id);
expect(tree.length).toBe(1);
expect(tree[0].name).toBe("dir");
expect(tree[0].children?.[0].name).toBe("renamed.txt");

const contentTree = await getFileContentTree(id);
const fileItem = contentTree[0].children?.[0];
expect(fileItem?.content).toBe("hello");

await removeFile(id, "dir/renamed.txt");
const treeAfter = await getFileTree(id);
expect(treeAfter[0].children?.length).toBe(0);
} finally {
mock.restore();
await fs.rm(projectPath, { recursive: true, force: true });
}
});
Loading