Netlify deploy improvements

This commit is contained in:
Alex Gleason committed 2025-10-18 19:42:04 -05:00
1 parent b39e52487c
commit 6eb6bde5bb
9 files changed
+482 -71

No files matched your search

+26 -20
View File
@@ -1,4 +1,5 @@
import JSZip from 'jszip';
import type { JSRuntimeFS } from '../JSRuntime';
import type { DeployAdapter, DeployOptions, DeployResult, NetlifyDeployConfig } from './types';
interface NetlifySite {
@@ -21,50 +22,55 @@ interface NetlifyBuildResponse {
* Uses Netlify API with personal access token
*/
export class NetlifyAdapter implements DeployAdapter {
private config: NetlifyDeployConfig;
private fs: JSRuntimeFS;
private apiKey: string;
private baseURL: string;
private siteName?: string;
private siteId?: string;
constructor(config: NetlifyDeployConfig) {
this.config = config;
this.fs = config.fs;
this.apiKey = config.apiKey;
this.baseURL = config.baseURL || 'https://api.netlify.com/api/v1';
this.siteName = config.siteName;
this.siteId = config.siteId;
}
async deploy(options: DeployOptions): Promise<DeployResult> {
const { projectId, projectName, fs, projectPath } = options;
const { apiKey, siteId, siteName } = this.config;
const { projectId, projectPath } = options;
// Check if dist directory exists and contains index.html
const distPath = `${projectPath}/dist`;
try {
await fs.readFile(`${distPath}/index.html`, 'utf8');
await this.fs.readFile(`${distPath}/index.html`, 'utf8');
} catch {
throw new Error('No index.html found in dist directory. Please build the project first.');
}
// Get or create site
let activeSiteId = siteId;
let activeSiteId = this.siteId;
let siteUrl = '';
if (!activeSiteId) {
// Create new site
const site = await this.createSite(apiKey, siteName || projectName || projectId);
const site = await this.createSite(this.siteName || projectId);
activeSiteId = site.id;
siteUrl = site.url;
} else {
// Get existing site URL
const site = await this.getSite(apiKey, activeSiteId);
const site = await this.getSite(activeSiteId);
siteUrl = site.url;
}
// Create ZIP of dist directory
const zip = new JSZip();
await this.addDirectoryToZip(fs, distPath, zip);
await this.addDirectoryToZip(distPath, zip);
// Generate ZIP blob
const zipBlob = await zip.generateAsync({ type: 'blob' });
// Deploy to Netlify
await this.triggerDeploy(apiKey, activeSiteId, zipBlob);
await this.triggerDeploy(activeSiteId, zipBlob);
return {
url: siteUrl,
@@ -75,11 +81,11 @@ export class NetlifyAdapter implements DeployAdapter {
};
}
private async createSite(apiKey: string, name: string): Promise<NetlifySite> {
private async createSite(name: string): Promise<NetlifySite> {
const response = await fetch(`${this.baseURL}/sites`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
@@ -96,11 +102,11 @@ export class NetlifyAdapter implements DeployAdapter {
return response.json();
}
private async getSite(apiKey: string, siteId: string): Promise<NetlifySite> {
private async getSite(siteId: string): Promise<NetlifySite> {
const response = await fetch(`${this.baseURL}/sites/${siteId}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Authorization': `Bearer ${this.apiKey}`,
},
});
@@ -112,11 +118,11 @@ export class NetlifyAdapter implements DeployAdapter {
return response.json();
}
private async triggerDeploy(apiKey: string, siteId: string, zipBlob: Blob): Promise<NetlifyBuildResponse> {
private async triggerDeploy(siteId: string, zipBlob: Blob): Promise<NetlifyBuildResponse> {
const response = await fetch(`${this.baseURL}/sites/${siteId}/deploys`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Authorization': `Bearer ${this.apiKey}`,
},
body: zipBlob,
});
@@ -129,9 +135,9 @@ export class NetlifyAdapter implements DeployAdapter {
return response.json();
}
private async addDirectoryToZip(fs: DeployOptions['fs'], dirPath: string, zip: JSZip): Promise<void> {
private async addDirectoryToZip(dirPath: string, zip: JSZip): Promise<void> {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
const entries = await this.fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = `${dirPath}/${entry.name}`;
@@ -140,12 +146,12 @@ export class NetlifyAdapter implements DeployAdapter {
// Create folder in zip and recursively add its contents
const folder = zip.folder(entry.name);
if (folder) {
await this.addDirectoryToZip(fs, fullPath, folder);
await this.addDirectoryToZip(fullPath, folder);
}
} else if (entry.isFile()) {
// Add file to zip
try {
const fileContent = await fs.readFile(fullPath);
const fileContent = await this.fs.readFile(fullPath);
zip.file(entry.name, fileContent);
} catch {
console.warn(`Failed to read file ${fullPath}`);
+20 -15
View File
@@ -1,5 +1,6 @@
import JSZip from 'jszip';
import { NIP98Client } from '@nostrify/nostrify';
import type { JSRuntimeFS } from '../JSRuntime';
import type { DeployAdapter, DeployOptions, DeployResult, ShakespeareDeployConfig } from './types';
/**
@@ -7,35 +8,39 @@ import type { DeployAdapter, DeployOptions, DeployResult, ShakespeareDeployConfi
* Uses NIP-98 authentication with Nostr signer
*/
export class ShakespeareAdapter implements DeployAdapter {
private config: ShakespeareDeployConfig;
private fs: JSRuntimeFS;
private signer: ShakespeareDeployConfig['signer'];
private host: string;
private subdomain?: string;
constructor(config: ShakespeareDeployConfig) {
this.config = config;
this.fs = config.fs;
this.signer = config.signer;
this.host = config.host || 'shakespeare.wtf';
this.subdomain = config.subdomain;
}
async deploy(options: DeployOptions): Promise<DeployResult> {
const { projectId, fs, projectPath } = options;
const { signer, deployServer, customHostname } = this.config;
const { projectId, projectPath } = options;
// Use custom hostname if provided, otherwise construct from projectId.host
const effectiveHost = deployServer || this.host;
const hostname = customHostname || `${projectId}.${effectiveHost}`;
const deployUrl = `https://${effectiveHost}/deploy`;
// Use custom subdomain if provided, otherwise construct from projectId
const hostname = this.subdomain
? `${this.subdomain}.${this.host}`
: `${projectId}.${this.host}`;
const deployUrl = `https://${this.host}/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');
await this.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 this.addDirectoryToZip(fs, distPath, zip);
await this.addDirectoryToZip(distPath, zip);
// Generate ZIP blob
const zipBlob = await zip.generateAsync({ type: 'blob' });
@@ -46,7 +51,7 @@ export class ShakespeareAdapter implements DeployAdapter {
formData.append('file', zipBlob, `${projectId}.zip`);
// Create NIP-98 authenticated client and deploy
const nip98Client = new NIP98Client({ signer });
const nip98Client = new NIP98Client({ signer: this.signer });
const response = await nip98Client.fetch(deployUrl, {
method: 'POST',
body: formData,
@@ -66,9 +71,9 @@ export class ShakespeareAdapter implements DeployAdapter {
};
}
private async addDirectoryToZip(fs: DeployOptions['fs'], dirPath: string, zip: JSZip): Promise<void> {
private async addDirectoryToZip(dirPath: string, zip: JSZip): Promise<void> {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
const entries = await this.fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = `${dirPath}/${entry.name}`;
@@ -77,12 +82,12 @@ export class ShakespeareAdapter implements DeployAdapter {
// Create folder in zip and recursively add its contents
const folder = zip.folder(entry.name);
if (folder) {
await this.addDirectoryToZip(fs, fullPath, folder);
await this.addDirectoryToZip(fullPath, folder);
}
} else if (entry.isFile()) {
// Add file to zip
try {
const fileContent = await fs.readFile(fullPath);
const fileContent = await this.fs.readFile(fullPath);
zip.file(entry.name, fileContent);
} catch {
console.warn(`Failed to read file ${fullPath}`);
+19 -14
View File
@@ -1,3 +1,4 @@
import type { JSRuntimeFS } from '../JSRuntime';
import type { DeployAdapter, DeployOptions, DeployResult, VercelDeployConfig } from './types';
interface VercelFile {
@@ -17,33 +18,38 @@ interface VercelDeployment {
* Uses Vercel REST API with access token
*/
export class VercelAdapter implements DeployAdapter {
private config: VercelDeployConfig;
private fs: JSRuntimeFS;
private apiKey: string;
private baseURL: string;
private teamId?: string;
private projectName?: string;
constructor(config: VercelDeployConfig) {
this.config = config;
this.fs = config.fs;
this.apiKey = config.apiKey;
this.baseURL = config.baseURL || 'https://api.vercel.com';
this.teamId = config.teamId;
this.projectName = config.projectName;
}
async deploy(options: DeployOptions): Promise<DeployResult> {
const { projectId, projectName, fs, projectPath } = options;
const { apiKey, teamId } = this.config;
const { projectId, projectPath } = options;
// Check if dist directory exists and contains index.html
const distPath = `${projectPath}/dist`;
try {
await fs.readFile(`${distPath}/index.html`, 'utf8');
await this.fs.readFile(`${distPath}/index.html`, 'utf8');
} catch {
throw new Error('No index.html found in dist directory. Please build the project first.');
}
// Collect all files from dist directory
const files: VercelFile[] = [];
await this.collectFiles(fs, distPath, '', files);
await this.collectFiles(distPath, '', files);
// Create deployment payload
const deploymentPayload = {
name: this.config.projectName || projectName || projectId,
name: this.projectName || projectId,
files,
projectSettings: {
framework: null, // Static deployment
@@ -53,15 +59,15 @@ export class VercelAdapter implements DeployAdapter {
// Build API URL with optional team parameter
let apiUrl = `${this.baseURL}/v13/deployments`;
if (teamId) {
apiUrl += `?teamId=${teamId}`;
if (this.teamId) {
apiUrl += `?teamId=${this.teamId}`;
}
// Deploy to Vercel
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(deploymentPayload),
@@ -85,13 +91,12 @@ export class VercelAdapter implements DeployAdapter {
}
private async collectFiles(
fs: DeployOptions['fs'],
dirPath: string,
relativePath: string,
files: VercelFile[]
): Promise<void> {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
const entries = await this.fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = `${dirPath}/${entry.name}`;
@@ -99,11 +104,11 @@ export class VercelAdapter implements DeployAdapter {
if (entry.isDirectory()) {
// Recursively collect files from subdirectories
await this.collectFiles(fs, fullPath, entryRelativePath, files);
await this.collectFiles(fullPath, entryRelativePath, files);
} else if (entry.isFile()) {
try {
// Read file content
const fileContent = await fs.readFile(fullPath);
const fileContent = await this.fs.readFile(fullPath);
// Convert to base64 for Vercel API
let base64Content: string;
+7 -10
View File
@@ -4,12 +4,8 @@ import type { NostrSigner } from '@nostrify/nostrify';
export interface DeployOptions {
/** Project ID */
projectId: string;
/** Filesystem instance containing the project */
fs: JSRuntimeFS;
/** Path to the project directory */
projectPath: string;
/** Project name */
projectName?: string;
}
export interface DeployResult {
@@ -25,23 +21,24 @@ export interface DeployAdapter {
}
export interface ShakespeareDeployConfig {
fs: JSRuntimeFS;
signer: NostrSigner;
deployServer?: string;
customHostname?: string;
host?: string;
subdomain?: string;
}
export interface NetlifyDeployConfig {
fs: JSRuntimeFS;
apiKey: string;
siteId?: string;
siteName?: string;
baseURL?: string;
siteName?: string;
siteId?: string;
}
export interface VercelDeployConfig {
fs: JSRuntimeFS;
apiKey: string;
projectId?: string;
baseURL?: string;
teamId?: string;
projectName?: string;
baseURL?: string;
}