From d0190cacf9e9cef2861661612e3dca93f6da1b5d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 19 Jul 2025 13:34:49 -0500 Subject: [PATCH] Revert "Deploy with nostrdeploy" This reverts commit 9b35a16d7b7dbabadef596d1e142baa45d27a3bc. --- package-lock.json | 15 ++- package.json | 2 +- src/components/Shakespeare/ChatPane.tsx | 132 +++++++++++++-------- src/components/Shakespeare/PreviewPane.tsx | 105 ++++++++++++---- src/lib/fs.ts | 3 - 5 files changed, 176 insertions(+), 81 deletions(-) diff --git a/package-lock.json b/package-lock.json index 061c9769..f1fe6e25 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,7 +57,7 @@ "isomorphic-git": "^1.32.1", "lucide-react": "^0.525.0", "next-themes": "^0.3.0", - "nostr-tools": "^2.15.1", + "nostr-tools": "^2.13.0", "react": "^18.3.1", "react-day-picker": "^8.10.1", "react-dom": "^18.3.1", @@ -6710,9 +6710,9 @@ } }, "node_modules/nostr-tools": { - "version": "2.15.1", - "resolved": "https://registry.npmjs.org/nostr-tools/-/nostr-tools-2.15.1.tgz", - "integrity": "sha512-LpetHDR9ltnkpJDkva/SONgyKBbsoV+5yLB8DWc0/U3lCWGtoWJw6Nbc2vR2Ai67RIQYrBQeZLyMlhwVZRK/9A==", + "version": "2.13.0", + "resolved": "https://registry.npmjs.org/nostr-tools/-/nostr-tools-2.13.0.tgz", + "integrity": "sha512-A1arGsvpULqVK0NmZQqK1imwaCiPm8gcG/lo+cTax2NbNqBEYsuplbqAFdVqcGHEopmkByYbTwF76x25+oEbew==", "license": "Unlicense", "dependencies": { "@noble/ciphers": "^0.5.1", @@ -6720,7 +6720,9 @@ "@noble/hashes": "1.3.1", "@scure/base": "1.1.1", "@scure/bip32": "1.3.1", - "@scure/bip39": "1.2.1", + "@scure/bip39": "1.2.1" + }, + "optionalDependencies": { "nostr-wasm": "0.1.0" }, "peerDependencies": { @@ -6736,7 +6738,8 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/nostr-wasm/-/nostr-wasm-0.1.0.tgz", "integrity": "sha512-78BTryCLcLYv96ONU8Ws3Q1JzjlAt+43pWQhIl86xZmWeegYCNLPml7yQ+gG3vR6V5h4XGj+TxO+SS5dsThQIA==", - "license": "MIT" + "license": "MIT", + "optional": true }, "node_modules/nwsapi": { "version": "2.2.20", diff --git a/package.json b/package.json index 872bff12..7d63553d 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "isomorphic-git": "^1.32.1", "lucide-react": "^0.525.0", "next-themes": "^0.3.0", - "nostr-tools": "^2.15.1", + "nostr-tools": "^2.13.0", "react": "^18.3.1", "react-day-picker": "^8.10.1", "react-dom": "^18.3.1", diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index ce5eea48..7449aa76 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -10,8 +10,6 @@ import { cn } from '@/lib/utils'; import { useAISettings } from '@/contexts/AISettingsContext'; import { FsToolSet } from '@/lib/FsToolSet'; import { fsManager } from '@/lib/fs'; -import { getPublicKey, nip19 } from 'nostr-tools'; -import { bytesToHex } from 'nostr-tools/utils'; const webcontainerPromise = WebContainer.boot(); @@ -45,23 +43,8 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { }; const runBuild = async () => { - const project = await fsManager.getProject(projectId); - - if (!project) { - console.error('Project not found'); - return; - } - - const decoded = nip19.decode(project.nsec); - if (decoded.type !== 'nsec') { - console.error('Invalid NSEC key'); - return; - } - const webcontainer = await webcontainerPromise; - console.log('Deploying project'); - const buildFileTree = async (dirPath: string): Promise => { const tree: FileSystemTree = {}; const items = await fsManager.fs.promises.readdir(dirPath); @@ -90,45 +73,92 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) { const fsTree = await buildFileTree(`/projects/${projectId}`); await webcontainer.mount(fsTree); - await webcontainer.fs.writeFile('.env.nostr-deploy.local', `# Nostr Deploy CLI Configuration -# This file contains sensitive information - do not commit to version control - -# Nostr Authentication -NOSTR_PRIVATE_KEY=${bytesToHex(decoded.data)} -NOSTR_PUBLIC_KEY=${getPublicKey(decoded.data)} -NOSTR_RELAYS=wss://relay.nostr.band,wss://relay.primal.net,wss://ditto.pub/relay - -# Blossom File Storage -BLOSSOM_SERVERS=https://cdn.hzrd149.com,https://blossom.primal.net,https://blossom.band,https://blossom.f7z.io - -# Deployment Settings -BASE_DOMAIN=nostrdeploy.com`); - - const proc1 = await webcontainer.spawn('npm', ['install']); - proc1.output.pipeTo(new WritableStream({ - write(data) { - console.log(data); - } - })); - await proc1.exit; + // Install dependencies and build + const proc = await webcontainer.spawn('npm', ['i']); + await proc.exit; const proc2 = await webcontainer.spawn('npm', ['run', 'build']); - proc2.output.pipeTo(new WritableStream({ - write(data) { - console.log(data); - } - })); await proc2.exit; - const proc3 = await webcontainer.spawn('npx', ["-y", "nostr-deploy-cli", "deploy", "--skip-setup"]); - proc3.output.pipeTo(new WritableStream({ - write(data) { - console.log(data); - } - })); - await proc3.exit; + // Copy "dist" out of the webcontainer back to the project directory + const copyDist = async () => { + try { + // Read the dist directory from webcontainer + const distContents = await webcontainer.fs.readdir('dist', { withFileTypes: true }); - console.log('Project deployed'); + // Create dist directory in project if it doesn't exist + const distPath = `/projects/${projectId}/dist`; + try { + await fsManager.fs.promises.mkdir(distPath); + } catch { + // Directory might already exist, that's fine + } + + // Helper function to copy directory recursively + const copyDirectory = async (sourcePath: string, destPath: string) => { + try { + console.log(`Copying directory: ${sourcePath}`); + const items = await webcontainer.fs.readdir(sourcePath, { withFileTypes: true }); + console.log(`Found ${items.length} items in ${sourcePath}`); + + for (const item of items) { + const sourceItemPath = `${sourcePath}/${item.name}`; + const destItemPath = `${destPath}/${item.name}`; + + try { + if (item.isDirectory()) { + console.log(`Creating directory: ${destItemPath}`); + try { + await fsManager.fs.promises.mkdir(destItemPath); + } catch { + // Directory might already exist + } + await copyDirectory(sourceItemPath, destItemPath); + } else { + console.log(`Copying file: ${sourceItemPath} -> ${destItemPath}`); + const content = await webcontainer.fs.readFile(sourceItemPath); + await fsManager.fs.promises.writeFile(destItemPath, content); + } + } catch (itemError) { + console.warn(`Failed to copy item ${item.name}:`, itemError); + } + } + } catch (error) { + console.warn(`Failed to copy ${sourcePath}:`, error); + } + }; + + // Copy each package in dist + for (const item of distContents) { + const sourcePath = `dist/${item.name}`; + const destPath = `${distPath}/${item.name}`; + + try { + if (item.isDirectory()) { + console.log(`Processing directory: ${item.name}`); + try { + await fsManager.fs.promises.mkdir(destPath); + } catch { + // Directory might already exist + } + await copyDirectory(sourcePath, destPath); + } else { + console.log(`Processing file: ${item.name}`); + const content = await webcontainer.fs.readFile(sourcePath); + await fsManager.fs.promises.writeFile(destPath, content); + } + } catch (itemError) { + console.warn(`Failed to process ${item.name}:`, itemError); + } + } + + console.log('Successfully copied node_modules from webcontainer'); + } catch (error) { + console.error('Failed to copy node_modules:', error); + } + }; + + await copyDist(); }; useEffect(() => { diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index d36b9821..38e2b8ae 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useCallback } from 'react'; import { fsManager } from '@/lib/fs'; import { Tabs, TabsContent } from '@/components/ui/tabs'; import { ScrollArea } from '@/components/ui/scroll-area'; @@ -7,7 +7,6 @@ import { RefreshCw } from 'lucide-react'; import { FileTree } from './FileTree'; import { FileEditor } from './FileEditor'; -import { getPublicKey, nip19 } from 'nostr-tools'; interface PreviewPaneProps { projectId: string; @@ -17,22 +16,88 @@ interface PreviewPaneProps { export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) { const [selectedFile, setSelectedFile] = useState(null); const [fileContent, setFileContent] = useState(''); - const [projectUrl, setProjectUrl] = useState(''); + const [isLoading, setIsLoading] = useState(false); + const [hasBuiltProject, setHasBuiltProject] = useState(false); + const [previewHtml, setPreviewHtml] = useState(''); + + const loadFileContent = useCallback(async (filePath: string) => { + setIsLoading(true); + try { + const content = await fsManager.readFile(projectId, filePath); + setFileContent(content); + } catch (_error) { + console.error('Failed to load file:', _error); + setFileContent(''); + } finally { + setIsLoading(false); + } + }, [projectId]); + + const checkForBuiltProject = useCallback(async () => { + try { + const exists = await fsManager.fileExists(projectId, 'dist/index.html'); + setHasBuiltProject(exists); + + if (exists) { + // Load the built HTML content + const htmlContent = await fsManager.readFile(projectId, 'dist/index.html'); + + // Process the HTML to add base tag for asset resolution + const processedHtml = processHtmlForPreview(htmlContent, projectId); + setPreviewHtml(processedHtml); + } + } catch (error) { + console.error('Failed to check for built project:', error); + setHasBuiltProject(false); + } + }, [projectId]); + + const processHtmlForPreview = (html: string, projectId: string): string => { + try { + // Create a temporary DOM to process the HTML + const parser = new DOMParser(); + const doc = parser.parseFromString(html, 'text/html'); + + // Rewrite script src attributes + const scripts = doc.querySelectorAll('script[src]'); + scripts.forEach((script) => { + const src = script.getAttribute('src'); + if (src && !src.startsWith('http') && !src.startsWith('//')) { + // Convert relative path to raw file path + const cleanPath = src.startsWith('./') ? src.slice(2) : src.startsWith('/') ? src.slice(1) : src; + const newSrc = `/project/${projectId}/files/dist/${cleanPath}`; + script.setAttribute('src', newSrc); + } + }); + + // Rewrite link href attributes (for stylesheets and other resources) + const links = doc.querySelectorAll('link[href]'); + links.forEach((link) => { + const href = link.getAttribute('href'); + if (href && !href.startsWith('http') && !href.startsWith('//')) { + // Convert relative path to raw file path + const cleanPath = href.startsWith('./') ? href.slice(2) : href.startsWith('/') ? href.slice(1) : href; + const newHref = `/project/${projectId}/files/dist/${cleanPath}`; + link.setAttribute('href', newHref); + } + }); + + return doc.documentElement.outerHTML; + } catch (error) { + console.error('Failed to process HTML for preview:', error); + return html; // Return original HTML if processing fails + } + }; useEffect(() => { - (async () => { - const project = await fsManager.getProject(projectId); - if (!project) return; + if (selectedFile) { + loadFileContent(selectedFile); + } + }, [selectedFile, loadFileContent]); - const decoded = nip19.decode(project.nsec); - if (decoded.type !== 'nsec') return; - - const pubkey = getPublicKey(decoded.data); - const npub = nip19.npubEncode(pubkey); - - setProjectUrl(`https://${npub}.nostrdeploy.com/`); - })(); - }, [projectId]); + useEffect(() => { + checkForBuiltProject(); + }, [checkForBuiltProject]); const handleFileSelect = (filePath: string) => { setSelectedFile(filePath); @@ -53,21 +118,21 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
- {projectUrl ? ( + {hasBuiltProject ? (
Project Preview