mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Deploy with nostrdeploy
This commit is contained in:
1 parent
9eb459e594
commit
0494ccef61
5 files changed
+80
-175
No files matched your search
@@ -10,6 +10,8 @@ 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();
|
||||
|
||||
@@ -43,8 +45,23 @@ 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<FileSystemTree> => {
|
||||
const tree: FileSystemTree = {};
|
||||
const items = await fsManager.fs.promises.readdir(dirPath);
|
||||
@@ -73,92 +90,45 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
|
||||
const fsTree = await buildFileTree(`/projects/${projectId}`);
|
||||
await webcontainer.mount(fsTree);
|
||||
|
||||
// Install dependencies and build
|
||||
const proc = await webcontainer.spawn('npm', ['i']);
|
||||
await proc.exit;
|
||||
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;
|
||||
|
||||
const proc2 = await webcontainer.spawn('npm', ['run', 'build']);
|
||||
proc2.output.pipeTo(new WritableStream({
|
||||
write(data) {
|
||||
console.log(data);
|
||||
}
|
||||
}));
|
||||
await proc2.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 });
|
||||
|
||||
// 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);
|
||||
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;
|
||||
|
||||
await copyDist();
|
||||
console.log('Project deployed');
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { fsManager } from '@/lib/fs';
|
||||
import { Tabs, TabsContent } from '@/components/ui/tabs';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
@@ -7,6 +7,7 @@ import { RefreshCw } from 'lucide-react';
|
||||
|
||||
import { FileTree } from './FileTree';
|
||||
import { FileEditor } from './FileEditor';
|
||||
import { getPublicKey, nip19 } from 'nostr-tools';
|
||||
|
||||
interface PreviewPaneProps {
|
||||
projectId: string;
|
||||
@@ -16,88 +17,22 @@ interface PreviewPaneProps {
|
||||
export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
const [selectedFile, setSelectedFile] = useState<string | null>(null);
|
||||
const [fileContent, setFileContent] = useState<string>('');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [hasBuiltProject, setHasBuiltProject] = useState(false);
|
||||
const [previewHtml, setPreviewHtml] = useState<string>('');
|
||||
|
||||
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
|
||||
}
|
||||
};
|
||||
const [projectUrl, setProjectUrl] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedFile) {
|
||||
loadFileContent(selectedFile);
|
||||
}
|
||||
}, [selectedFile, loadFileContent]);
|
||||
(async () => {
|
||||
const project = await fsManager.getProject(projectId);
|
||||
if (!project) return;
|
||||
|
||||
useEffect(() => {
|
||||
checkForBuiltProject();
|
||||
}, [checkForBuiltProject]);
|
||||
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]);
|
||||
|
||||
const handleFileSelect = (filePath: string) => {
|
||||
setSelectedFile(filePath);
|
||||
@@ -118,21 +53,21 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
<div className="h-full">
|
||||
<Tabs value={activeTab} className="h-full">
|
||||
<TabsContent value="preview" className="h-full mt-0">
|
||||
{hasBuiltProject ? (
|
||||
{projectUrl ? (
|
||||
<div className="h-full w-full flex flex-col">
|
||||
<div className="flex items-center justify-between p-2 border-b bg-background">
|
||||
<span className="text-sm text-muted-foreground">Project Preview</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={checkForBuiltProject}
|
||||
onClick={() => {}}
|
||||
className="h-8 w-8 p-0"
|
||||
>
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
<iframe
|
||||
srcDoc={previewHtml}
|
||||
src={projectUrl}
|
||||
className="w-full flex-1 border-0"
|
||||
title="Project Preview"
|
||||
/>
|
||||
@@ -151,7 +86,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={checkForBuiltProject}
|
||||
onClick={() => {}}
|
||||
className="gap-2"
|
||||
>
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
@@ -184,7 +119,7 @@ export function PreviewPane({ projectId, activeTab }: PreviewPaneProps) {
|
||||
filePath={selectedFile}
|
||||
content={fileContent}
|
||||
onSave={handleFileSave}
|
||||
isLoading={isLoading}
|
||||
isLoading={false} // Replace with actual loading state if needed
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full flex items-center justify-center">
|
||||
|
||||
@@ -2,6 +2,7 @@ import LightningFS from '@isomorphic-git/lightning-fs';
|
||||
import git from 'isomorphic-git';
|
||||
import http from 'isomorphic-git/http/web';
|
||||
import { Buffer } from 'buffer';
|
||||
import { generateSecretKey, nip19 } from 'nostr-tools';
|
||||
|
||||
// Polyfill Buffer for browser
|
||||
if (typeof window !== 'undefined') {
|
||||
@@ -15,6 +16,7 @@ export interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
path: string;
|
||||
nsec: string;
|
||||
createdAt: Date;
|
||||
lastModified: Date;
|
||||
}
|
||||
@@ -46,6 +48,7 @@ export class FileSystemManager {
|
||||
id,
|
||||
name,
|
||||
path: projectPath,
|
||||
nsec: nip19.nsecEncode(generateSecretKey()),
|
||||
createdAt: new Date(),
|
||||
lastModified: new Date(),
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user