在本文中,我将修改 Bolt.new 以允许在该工具中创建的应用程序下载到本地。此功能将促进 Bolt.new 应用程序的内部部署,使其对企业环境特别有用。
在下一篇文章中,我们将介绍如何修改 Bolt.new 以与 Azure OpenAI 服务集成,从而简化企业级用例的应用程序。请检查一下。
为了启用以ZIP形式下载项目文件的功能,我们修改文件bolt.new/app/components/workbench/EditorPanel.tsx。为了清楚起见,更改被标记在 // Append start 和 // Append end 之间。
... // Append start import JSZip from 'jszip'; // Append end ... export const EditorPanel = memo( ({ files, unsavedFiles, editorDocument, selectedFile, isStreaming, onFileSelect, onEditorChange, onEditorScroll, onFileSave, onFileReset, }: EditorPanelProps) => { ... // Append start const handleDownloadZip = useCallback(async () => { const zip = new JSZip(); const files = workbenchStore.files.get(); // Check if there are files to download if (Object.keys(files).length === 0) { toast.error('No files to download'); return; } try { // Add files to the ZIP, maintaining relative paths from WORK_DIR Object.entries(files).forEach(([path, content]) => { if (content && content.content) { const relativePath = path.startsWith(WORK_DIR) ? path.slice(WORK_DIR.length + 1) : path; zip.file(relativePath, content.content); } }); const zipBlob = await zip.generateAsync({ type: 'blob' }); const downloadLink = document.createElement('a'); downloadLink.href = URL.createObjectURL(zipBlob); // Use the project name from `package.json` if available const projectName = files[`${WORK_DIR}/package.json`]?.content ? JSON.parse(files[`${WORK_DIR}/package.json`].content).name : 'project'; downloadLink.download = `${projectName}.zip`; downloadLink.click(); URL.revokeObjectURL(downloadLink.href); toast.success('Files downloaded successfully'); } catch (error) { toast.error('Failed to create zip file'); console.error(error); } }, []); // Append end return ( <PanelGroup direction="vertical"> <Panel defaultSize={showTerminal ? DEFAULT_EDITOR_SIZE : 100} minSize={20}> <PanelGroup direction="horizontal"> <Panel defaultSize={20} minSize={10} collapsible> <div className="flex flex-col border-r border-bolt-elements-borderColor h-full"> <PanelHeader> <div className="i-ph:tree-structure-duotone shrink-0" /> Files {/* Append start */} <div className="flex-1" /> <button className="px-2 py-1 rounded-md text-bolt-elements-item-contentDefault bg-transparent enabled:hover:text-bolt-elements-item-contentActive enabled:hover:bg-bolt-elements-item-backgroundActive" onClick={handleDownloadZip} title="Download as ZIP" > <div className="i-ph:download-simple text-xl" /> </button> {/* Append end */} </PanelHeader> ...
添加了 JSZip 导入:
该库用于生成 ZIP 存档。
handleDownloadZip 函数:
界面中的下载按钮:
通过此更改,用户可以直接从界面下载 ZIP 存档形式的项目文件。
要使用 JSZip 库生成 ZIP 存档,请将其添加到 package.json 中的依赖项中。
{ ... "dependencies": { "@ai-sdk/anthropic": "^0.0.39", // Append start "jszip": "^3.10.1", // Append end }, ... }
pnpm install
这可确保您的项目中提供 ZIP 生成功能所需的库。
完成修改后,请按照以下步骤验证新功能:
运行以下命令来构建并启动bolt.new:
... // Append start import JSZip from 'jszip'; // Append end ... export const EditorPanel = memo( ({ files, unsavedFiles, editorDocument, selectedFile, isStreaming, onFileSelect, onEditorChange, onEditorScroll, onFileSave, onFileReset, }: EditorPanelProps) => { ... // Append start const handleDownloadZip = useCallback(async () => { const zip = new JSZip(); const files = workbenchStore.files.get(); // Check if there are files to download if (Object.keys(files).length === 0) { toast.error('No files to download'); return; } try { // Add files to the ZIP, maintaining relative paths from WORK_DIR Object.entries(files).forEach(([path, content]) => { if (content && content.content) { const relativePath = path.startsWith(WORK_DIR) ? path.slice(WORK_DIR.length + 1) : path; zip.file(relativePath, content.content); } }); const zipBlob = await zip.generateAsync({ type: 'blob' }); const downloadLink = document.createElement('a'); downloadLink.href = URL.createObjectURL(zipBlob); // Use the project name from `package.json` if available const projectName = files[`${WORK_DIR}/package.json`]?.content ? JSON.parse(files[`${WORK_DIR}/package.json`].content).name : 'project'; downloadLink.download = `${projectName}.zip`; downloadLink.click(); URL.revokeObjectURL(downloadLink.href); toast.success('Files downloaded successfully'); } catch (error) { toast.error('Failed to create zip file'); console.error(error); } }, []); // Append end return ( <PanelGroup direction="vertical"> <Panel defaultSize={showTerminal ? DEFAULT_EDITOR_SIZE : 100} minSize={20}> <PanelGroup direction="horizontal"> <Panel defaultSize={20} minSize={10} collapsible> <div className="flex flex-col border-r border-bolt-elements-borderColor h-full"> <PanelHeader> <div className="i-ph:tree-structure-duotone shrink-0" /> Files {/* Append start */} <div className="flex-1" /> <button className="px-2 py-1 rounded-md text-bolt-elements-item-contentDefault bg-transparent enabled:hover:text-bolt-elements-item-contentActive enabled:hover:bg-bolt-elements-item-backgroundActive" onClick={handleDownloadZip} title="Download as ZIP" > <div className="i-ph:download-simple text-xl" /> </button> {/* Append end */} </PanelHeader> ...
在浏览器中打开应用程序并导航到应用程序界面的“代码”选项卡。
确认“文件”部分中可见下载按钮,如下所示:
以上是在本地启用应用程序下载 Bolt.new的详细内容。更多信息请关注PHP中文网其他相关文章!