Deploy with picohost

This commit is contained in:
Alex Gleason committed 2025-09-07 17:58:19 -05:00
1 parent fd9aa5b683
commit 26635a4b6a
10 files changed
+367 -59

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
import JSZip from 'jszip';
import { NIP98Client } from './NIP98Client';
import type { NostrSigner } from '@nostrify/nostrify';
import type { JSRuntimeFS } from './JSRuntime';
export interface DeployOptions {
/** Project ID to use for hostname construction */
projectId: string;
/** Deploy server domain (e.g., "shakespeare.wtf") */
deployServer: string;
/** Filesystem instance containing the project */
fs: JSRuntimeFS;
/** Path to the project directory */
projectPath: string;
/** Nostr signer for NIP-98 authentication */
signer: NostrSigner;
}
export interface DeployResult {
/** The deployed URL */
url: string;
/** The hostname used */
hostname: string;
}
/**
* Deploy a project by creating a ZIP of the dist directory and uploading it
* to the deployment server using NIP-98 authentication.
*/
export async function deployProject(options: DeployOptions): Promise<DeployResult> {
const { projectId, deployServer, fs, projectPath, signer } = options;
// Construct hostname and URL
const hostname = `${projectId}.${deployServer}`;
const deployUrl = `https://${deployServer}/deploy`;
const siteUrl = `https://${hostname}`;
// Check if dist directory exists and contains index.html
const distPath = `${projectPath}/dist`;
try {
await fs.readFile(`${distPath}/index.html`, 'utf8');
} catch {
throw new Error('No index.html found in dist directory. Please build the project first.');
}
// Create ZIP of dist directory
const zip = new JSZip();
await addDirectoryToZip(fs, distPath, zip);
// Generate ZIP blob
const zipBlob = await zip.generateAsync({ type: 'blob' });
// Create FormData
const formData = new FormData();
formData.append('hostname', hostname);
formData.append('file', zipBlob, `${projectId}.zip`);
// Create NIP-98 authenticated client and deploy
const nip98Client = new NIP98Client(signer);
const response = await nip98Client.fetch(deployUrl, {
method: 'POST',
body: formData,
});
if (!response.ok) {
const errorText = await response.text().catch(() => 'Unknown error');
throw new Error(`Deployment failed: ${response.status} ${response.statusText}. ${errorText}`);
}
return {
url: siteUrl,
hostname,
};
}
/**
* Recursively add all files in a directory to a JSZip instance
*/
async function addDirectoryToZip(fs: JSRuntimeFS, dirPath: string, zip: JSZip): Promise<void> {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = `${dirPath}/${entry.name}`;
if (entry.isDirectory()) {
// Create folder in zip and recursively add its contents
const folder = zip.folder(entry.name);
if (folder) {
await addDirectoryToZip(fs, fullPath, folder);
}
} else if (entry.isFile()) {
// Add file to zip
try {
const fileContent = await fs.readFile(fullPath);
zip.file(entry.name, fileContent);
} catch {
console.warn(`Failed to read file ${fullPath}`);
}
}
}
} catch {
console.warn(`Failed to read directory ${dirPath}`);
}
}
+64
View File
@@ -0,0 +1,64 @@
import { z } from "zod";
import type { Tool } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime";
import { deployProject } from "../deploy";
import type { NostrSigner } from "@nostrify/nostrify";
interface DeployProjectParams {
deployServer?: string;
}
export class DeployProjectTool implements Tool<DeployProjectParams> {
private fs: JSRuntimeFS;
private cwd: string;
private signer: NostrSigner;
private projectId: string;
readonly description = "Deploy the project to a hosting server using NIP-98 authentication. Creates a ZIP of the dist directory and uploads it to the deployment server. Requires the user to be logged in with Nostr.";
readonly inputSchema = z.object({
deployServer: z.string().optional().describe("Deploy server domain (e.g., 'shakespeare.wtf'). If not provided, uses the default configured server."),
});
constructor(fs: JSRuntimeFS, cwd: string, signer: NostrSigner, projectId: string) {
this.fs = fs;
this.cwd = cwd;
this.signer = signer;
this.projectId = projectId;
}
async execute(args: DeployProjectParams): Promise<string> {
try {
// Use provided deployServer or default to shakespeare.wtf
const deployServer = args.deployServer || "shakespeare.wtf";
// Check if we're in a valid project directory
try {
await this.fs.readFile(`${this.cwd}/package.json`, "utf8");
} catch {
throw new Error(`❌ Could not find package.json at ${this.cwd}. Make sure you're in a valid project directory.`);
}
// Check if dist directory exists and contains index.html
try {
await this.fs.readFile(`${this.cwd}/dist/index.html`, "utf8");
} catch {
throw new Error(`❌ Could not find index.html in dist directory. Please build the project first using the build tool.`);
}
// Deploy the project
const result = await deployProject({
projectId: this.projectId,
deployServer,
fs: this.fs,
projectPath: this.cwd,
signer: this.signer,
});
return `✅ Successfully deployed project!\n\n🌐 Live URL: ${result.url}\n📡 Hostname: ${result.hostname}\n\n🚀 Your project is now live and accessible to anyone with the URL!`;
} catch (error) {
throw new Error(`❌ Deployment failed: ${String(error)}`);
}
}
}