Export individual projects as zip

This commit is contained in:
Alex Gleason committed 2025-07-25 20:07:59 -05:00
1 parent fc296b44d8
commit 7b43eed28d
1 file changed
+120 -16
+120 -16
View File
@@ -16,6 +16,7 @@ import { useFS } from '@/hooks/useFS';
import type { Project } from '@/lib/ProjectsManager';
import { cn } from '@/lib/utils';
import JSZip from 'jszip';
import git from 'isomorphic-git';
interface ProjectSidebarProps {
selectedProject: Project | null;
@@ -105,24 +106,106 @@ export function ProjectSidebar({
const isFavorite = (projectId: string) => favorites.includes(projectId);
const exportFilesAsZip = async (projectName: string) => {
const zip = new JSZip();
// Recursive function to add files and directories to zip
const addToZip = async (dirPath: string, zipFolder: JSZip) => {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = dirPath === '/' ? `/${entry.name}` : `${dirPath}/${entry.name}`;
if (entry.isDirectory()) {
// Create folder in zip and recursively add its contents
const folder = zipFolder.folder(entry.name);
if (folder) {
await addToZip(fullPath, folder);
}
} else if (entry.isFile()) {
// Add file to zip
try {
const fileContent = await fs.readFile(fullPath);
zipFolder.file(entry.name, fileContent);
} catch (error) {
console.warn(`Failed to read file ${fullPath}:`, error);
}
}
}
} catch (error) {
console.warn(`Failed to read directory ${dirPath}:`, error);
}
};
// Start from root directory
await addToZip('/', zip);
// Generate zip file
const content = await zip.generateAsync({ type: 'blob' });
// Create download link
const url = URL.createObjectURL(content);
const link = document.createElement('a');
link.href = url;
// Generate filename with date
const now = new Date();
const dateStr = now.toISOString().split('T')[0]; // YYYY-MM-DD format
link.download = `${projectName}-${dateStr}.zip`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
const handleExportFiles = async () => {
setIsExporting(true);
try {
const zip = new JSZip();
const projectName = selectedProject?.name || 'shakespeare';
await exportFilesAsZip(projectName);
// Recursive function to add files and directories to zip
const addToZip = async (dirPath: string, zipFolder: JSZip) => {
toast({
title: "Files exported successfully",
description: "Your project files have been downloaded as a zip file.",
});
} catch (error) {
console.error('Failed to export files:', error);
toast({
title: "Failed to export files",
description: error instanceof Error ? error.message : "An unexpected error occurred",
variant: "destructive",
});
} finally {
setIsExporting(false);
}
};
const handleExportProject = async (project: Project) => {
setIsExporting(true);
try {
const zip = new JSZip();
const projectPath = `/projects/${project.id}`;
// Recursive function to add files and directories to zip from a specific project
const addProjectToZip = async (dirPath: string, zipFolder: JSZip) => {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = dirPath === '/' ? `/${entry.name}` : `${dirPath}/${entry.name}`;
const fullPath = `${dirPath}/${entry.name}`;
// Skip .git directory to avoid including git metadata
if (entry.name === '.git') {
continue;
}
if (entry.isDirectory()) {
// Create folder in zip and recursively add its contents
const folder = zipFolder.folder(entry.name);
if (folder) {
await addToZip(fullPath, folder);
await addProjectToZip(fullPath, folder);
}
} else if (entry.isFile()) {
// Add file to zip
@@ -139,8 +222,8 @@ export function ProjectSidebar({
}
};
// Start from root directory
await addToZip('/', zip);
// Start from the project directory
await addProjectToZip(projectPath, zip);
// Generate zip file
const content = await zip.generateAsync({ type: 'blob' });
@@ -150,11 +233,25 @@ export function ProjectSidebar({
const link = document.createElement('a');
link.href = url;
// Generate filename with date
const now = new Date();
const dateStr = now.toISOString().split('T')[0]; // YYYY-MM-DD format
const projectName = selectedProject?.name || 'shakespeare';
link.download = `${projectName}-${dateStr}.zip`;
// Generate filename with commit hash
let filename = `${project.name}.zip`;
try {
// Get the current commit hash
const commits = await git.log({
fs,
dir: projectPath,
depth: 1,
});
if (commits.length > 0) {
const commitHash = commits[0].oid.substring(0, 7); // Truncate to 7 characters
filename = `${project.name}-${commitHash}.zip`;
}
} catch (error) {
console.warn('Failed to get commit hash, using project name only:', error);
}
link.download = filename;
document.body.appendChild(link);
link.click();
@@ -162,13 +259,13 @@ export function ProjectSidebar({
URL.revokeObjectURL(url);
toast({
title: "Files exported successfully",
description: "Your project files have been downloaded as a zip file.",
title: "Project exported successfully",
description: `"${project.name}" has been downloaded as a zip file.`,
});
} catch (error) {
console.error('Failed to export files:', error);
console.error('Failed to export project:', error);
toast({
title: "Failed to export files",
title: "Failed to export project",
description: error instanceof Error ? error.message : "An unexpected error occurred",
variant: "destructive",
});
@@ -334,6 +431,13 @@ export function ProjectSidebar({
<Eye className="h-4 w-4 mr-2" />
Open Project
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => handleExportProject(project)}
disabled={isExporting}
>
<Download className="h-4 w-4 mr-2" />
{isExporting ? 'Exporting...' : 'Export Project'}
</DropdownMenuItem>
<AlertDialog>
<AlertDialogTrigger asChild>
<DropdownMenuItem