Skip to content

Commit

Permalink
Merge pull request stackblitz-labs#19 from dustinwloring1988/unique-n…
Browse files Browse the repository at this point in the history
…ame-on-download-zip

Unique name on download zip
  • Loading branch information
dustinwloring1988 authored Dec 1, 2024
2 parents 197a7cb + c3b8bab commit e698ce9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/lib/stores/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { saveAs } from 'file-saver';
import { Octokit, type RestEndpointMethodTypes } from '@octokit/rest';
import * as nodePath from 'node:path';
import { extractRelativePath } from '~/utils/diff';
import { description } from '../persistence';

export interface ArtifactState {
id: string;
Expand Down Expand Up @@ -168,6 +169,7 @@ export class WorkbenchStore {
this.#editorStore.setSelectedFile(filePath);
}


async saveFile(filePath: string) {
const documents = this.#editorStore.documents.get();
const document = documents[filePath];
Expand Down Expand Up @@ -327,6 +329,12 @@ export class WorkbenchStore {
async downloadZip() {
const zip = new JSZip();
const files = this.files.get();
// Get the project name from the description input, or use a default name
const projectName = (description.value ?? 'project').toLocaleLowerCase().split(' ').join('_');

// Generate a simple 6-character hash based on the current timestamp
const timestampHash = Date.now().toString(36).slice(-6);
const uniqueProjectName = `${projectName}_${timestampHash}`;

for (const [filePath, dirent] of Object.entries(files)) {
if (dirent?.type === 'file' && !dirent.isBinary) {
Expand All @@ -349,9 +357,10 @@ export class WorkbenchStore {
}
}
}

// Generate the zip file and save it
const content = await zip.generateAsync({ type: 'blob' });
saveAs(content, 'project.zip');
saveAs(content, `${uniqueProjectName}.zip`);

}

async syncFiles(targetHandle: FileSystemDirectoryHandle) {
Expand Down

0 comments on commit e698ce9

Please sign in to comment.