Refactored to use a build script
This commit is contained in:
113
src/exporter.js
Normal file
113
src/exporter.js
Normal file
@ -0,0 +1,113 @@
|
||||
const ArtifactExporter = {
|
||||
|
||||
parseFileMap(fileMap) {
|
||||
const lines = fileMap.split('\n').filter(line => line.trim());
|
||||
const pathMapping = {};
|
||||
let currentPath = '';
|
||||
|
||||
lines.forEach(line => {
|
||||
const trimmedLine = line.trim();
|
||||
|
||||
// Check if this is a directory (ends with /)
|
||||
if (trimmedLine.endsWith('/')) {
|
||||
currentPath = trimmedLine;
|
||||
} else if (trimmedLine && !trimmedLine.startsWith('/')) {
|
||||
if (line.startsWith(' ')) {
|
||||
// File is indented, so it's in the current directory
|
||||
pathMapping[trimmedLine] = currentPath + trimmedLine;
|
||||
} else {
|
||||
// File is at root level
|
||||
pathMapping[trimmedLine] = trimmedLine;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return pathMapping;
|
||||
},
|
||||
|
||||
createFinalMapping(artifactCollection, pathMapping) {
|
||||
const finalMap = {};
|
||||
Object.entries(artifactCollection).forEach(([filename, content]) => {
|
||||
const fullPath = pathMapping[filename] || filename; // Use filename as-is if not in mapping
|
||||
finalMap[fullPath] = content;
|
||||
});
|
||||
return finalMap;
|
||||
},
|
||||
|
||||
generateZipFilename() {
|
||||
const now = new Date();
|
||||
const timestamp = now.getFullYear() + '-' +
|
||||
String(now.getMonth() + 1).padStart(2, '0') + '-' +
|
||||
String(now.getDate()).padStart(2, '0');
|
||||
return `${timestamp}_claude-artifacts.zip`;
|
||||
},
|
||||
|
||||
addFilesToZip(zip, finalMap) {
|
||||
Object.entries(finalMap).forEach(([fullPath, content]) => {
|
||||
// Handle directory structure
|
||||
const pathParts = fullPath.split('/');
|
||||
|
||||
if (pathParts.length > 1) {
|
||||
// File is in a directory
|
||||
const filename = pathParts.pop();
|
||||
const folderPath = pathParts.join('/');
|
||||
const folder = zip.folder(folderPath);
|
||||
folder.file(filename, content);
|
||||
} else {
|
||||
// File is at root level
|
||||
zip.file(fullPath, content);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
downloadZip(content, filename) {
|
||||
const url = URL.createObjectURL(content);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
console.log(`✓ Downloaded: ${filename}`);
|
||||
},
|
||||
|
||||
exportArtifacts(artifactCollection, fileMap) {
|
||||
// Validation
|
||||
if (!artifactCollection || Object.keys(artifactCollection).length === 0) {
|
||||
console.warn("No non-filemap artifacts were found. Ask the LLM to copy them from the project to this chat first.");
|
||||
return;
|
||||
}
|
||||
if (!fileMap) {
|
||||
console.warn("No filemap was found. Ask the LLM to generate one. See the README for a sample prompt.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the file map into paths
|
||||
const pathMapping = this.parseFileMap(fileMap);
|
||||
|
||||
// Create final map with full paths and contents
|
||||
const finalMap = this.createFinalMapping(artifactCollection, pathMapping);
|
||||
|
||||
// Create zip file
|
||||
const zip = new JSZip();
|
||||
|
||||
// Add each file to the zip
|
||||
this.addFilesToZip(zip, finalMap);
|
||||
|
||||
// Generate filename
|
||||
const zipFilename = this.generateZipFilename();
|
||||
|
||||
// Generate and download the zip
|
||||
zip.generateAsync({type: "blob"})
|
||||
.then((content) => {
|
||||
this.downloadZip(content, zipFilename);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Failed to generate zip:', error);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// No exports needed - will be in same scope after build
|
||||
Reference in New Issue
Block a user