mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Save deploy settings into VFS
This commit is contained in:
5 files changed
+300
-134
No files matched your search
@@ -24,7 +24,6 @@ import { useProjectDeploySettings } from '@/hooks/useProjectDeploySettings';
|
||||
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { ShakespeareAdapter, NetlifyAdapter, VercelAdapter, DeployAdapter } from '@/lib/deploy';
|
||||
import type { DeployProvider } from '@/contexts/DeploySettingsContext';
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
interface DeployDialogProps {
|
||||
|
||||
@@ -1,32 +1,48 @@
|
||||
import { useState, useEffect, useMemo, type ReactNode } from 'react';
|
||||
import { DeploySettingsContext, type DeploySettings, type DeployProvider } from '@/contexts/DeploySettingsContext';
|
||||
|
||||
const STORAGE_KEY = 'shakespeare-deploy-settings';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { readDeploySettings, writeDeploySettings } from '@/lib/configUtils';
|
||||
|
||||
const DEFAULT_SETTINGS: DeploySettings = {
|
||||
providers: [],
|
||||
};
|
||||
|
||||
export function DeploySettingsProvider({ children }: { children: ReactNode }) {
|
||||
const [settings, setSettings] = useState<DeploySettings>(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored) {
|
||||
return JSON.parse(stored);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load deploy settings:', error);
|
||||
}
|
||||
return DEFAULT_SETTINGS;
|
||||
});
|
||||
const { fs } = useFS();
|
||||
const [settings, setSettings] = useState<DeploySettings>(DEFAULT_SETTINGS);
|
||||
const [isInitialized, setIsInitialized] = useState(false);
|
||||
|
||||
// Initialize settings from VFS on mount
|
||||
useEffect(() => {
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
||||
} catch (error) {
|
||||
console.error('Failed to save deploy settings:', error);
|
||||
}
|
||||
}, [settings]);
|
||||
const initializeSettings = async () => {
|
||||
try {
|
||||
const settings = await readDeploySettings(fs);
|
||||
setSettings(settings);
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize deploy settings:', error);
|
||||
setSettings(DEFAULT_SETTINGS);
|
||||
} finally {
|
||||
setIsInitialized(true);
|
||||
}
|
||||
};
|
||||
|
||||
initializeSettings();
|
||||
}, [fs]);
|
||||
|
||||
// Save settings to VFS whenever they change (but not during initialization)
|
||||
useEffect(() => {
|
||||
if (!isInitialized) return;
|
||||
|
||||
const saveSettings = async () => {
|
||||
try {
|
||||
await writeDeploySettings(fs, settings);
|
||||
} catch (error) {
|
||||
console.error('Failed to save deploy settings:', error);
|
||||
}
|
||||
};
|
||||
|
||||
saveSettings();
|
||||
}, [fs, settings, isInitialized]);
|
||||
|
||||
const updateSettings = (updates: Partial<DeploySettings>) => {
|
||||
setSettings(prev => ({ ...prev, ...updates }));
|
||||
|
||||
@@ -3,11 +3,14 @@ import {
|
||||
readAISettings,
|
||||
writeAISettings,
|
||||
readGitSettings,
|
||||
writeGitSettings
|
||||
writeGitSettings,
|
||||
readDeploySettings,
|
||||
writeDeploySettings
|
||||
} from './configUtils';
|
||||
import type { JSRuntimeFS } from './JSRuntime';
|
||||
import type { AISettings } from '@/contexts/AISettingsContext';
|
||||
import type { GitSettings } from '@/contexts/GitSettingsContext';
|
||||
import type { DeploySettings } from '@/contexts/DeploySettingsContext';
|
||||
|
||||
// Mock filesystem
|
||||
const createMockFS = (): JSRuntimeFS => ({
|
||||
@@ -190,4 +193,91 @@ describe('configUtils', () => {
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('Deploy Settings', () => {
|
||||
const sampleDeploySettings: DeploySettings = {
|
||||
providers: [
|
||||
{
|
||||
name: 'My Netlify',
|
||||
type: 'netlify',
|
||||
apiKey: 'netlify-token-123',
|
||||
},
|
||||
{
|
||||
name: 'My Vercel',
|
||||
type: 'vercel',
|
||||
apiKey: 'vercel-token-456',
|
||||
},
|
||||
{
|
||||
name: 'Shakespeare',
|
||||
type: 'shakespeare',
|
||||
},
|
||||
],
|
||||
defaultProviderId: 'netlify',
|
||||
};
|
||||
|
||||
describe('readDeploySettings', () => {
|
||||
it('should read Deploy settings from VFS', async () => {
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue(JSON.stringify(sampleDeploySettings));
|
||||
|
||||
const result = await readDeploySettings(mockFS);
|
||||
|
||||
expect(mockFS.readFile).toHaveBeenCalledWith('/config/deploy.json', 'utf8');
|
||||
expect(result).toEqual(sampleDeploySettings);
|
||||
});
|
||||
|
||||
it('should return default settings if file does not exist', async () => {
|
||||
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found'));
|
||||
|
||||
const result = await readDeploySettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should return default settings if JSON is invalid', async () => {
|
||||
vi.mocked(mockFS.readFile).mockResolvedValue('invalid json');
|
||||
|
||||
const result = await readDeploySettings(mockFS);
|
||||
|
||||
expect(result).toEqual({
|
||||
providers: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeDeploySettings', () => {
|
||||
it('should write Deploy settings to VFS', async () => {
|
||||
vi.mocked(mockFS.stat).mockRejectedValue(new Error('Directory not found'));
|
||||
vi.mocked(mockFS.mkdir).mockResolvedValue(undefined);
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
await writeDeploySettings(mockFS, sampleDeploySettings);
|
||||
|
||||
expect(mockFS.mkdir).toHaveBeenCalledWith('/config', { recursive: true });
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/deploy.json',
|
||||
JSON.stringify(sampleDeploySettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
|
||||
it('should not create directory if it already exists', async () => {
|
||||
vi.mocked(mockFS.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
});
|
||||
vi.mocked(mockFS.writeFile).mockResolvedValue(undefined);
|
||||
|
||||
await writeDeploySettings(mockFS, sampleDeploySettings);
|
||||
|
||||
expect(mockFS.mkdir).not.toHaveBeenCalled();
|
||||
expect(mockFS.writeFile).toHaveBeenCalledWith(
|
||||
'/config/deploy.json',
|
||||
JSON.stringify(sampleDeploySettings, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,10 +3,12 @@ import { filteredArray } from '@/lib/schema';
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
import type { AIProvider, AISettings } from '@/contexts/AISettingsContext';
|
||||
import type { GitSettings } from '@/contexts/GitSettingsContext';
|
||||
import type { DeployProvider, DeploySettings } from '@/contexts/DeploySettingsContext';
|
||||
|
||||
const CONFIG_DIR = '/config';
|
||||
const AI_CONFIG_PATH = `${CONFIG_DIR}/ai.json`;
|
||||
const GIT_CONFIG_PATH = `${CONFIG_DIR}/git.json`;
|
||||
const DEPLOY_CONFIG_PATH = `${CONFIG_DIR}/deploy.json`;
|
||||
|
||||
const aiProviderSchema: z.ZodType<AIProvider> = z.object({
|
||||
id: z.string(),
|
||||
@@ -46,6 +48,37 @@ const gitSettingsSchema = z.object({
|
||||
coAuthorEnabled: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const shakespeareDeployProviderSchema = z.object({
|
||||
name: z.string(),
|
||||
type: z.literal('shakespeare'),
|
||||
host: z.string().optional(),
|
||||
});
|
||||
|
||||
const netlifyProviderSchema = z.object({
|
||||
name: z.string(),
|
||||
type: z.literal('netlify'),
|
||||
apiKey: z.string(),
|
||||
baseURL: z.string().optional(),
|
||||
});
|
||||
|
||||
const vercelProviderSchema = z.object({
|
||||
name: z.string(),
|
||||
type: z.literal('vercel'),
|
||||
apiKey: z.string(),
|
||||
baseURL: z.string().optional(),
|
||||
});
|
||||
|
||||
const deployProviderSchema: z.ZodType<DeployProvider> = z.discriminatedUnion('type', [
|
||||
shakespeareDeployProviderSchema,
|
||||
netlifyProviderSchema,
|
||||
vercelProviderSchema,
|
||||
]);
|
||||
|
||||
const deploySettingsSchema = z.object({
|
||||
providers: filteredArray(deployProviderSchema),
|
||||
defaultProviderId: z.string().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Ensures the config directory exists
|
||||
*/
|
||||
@@ -119,3 +152,31 @@ export async function writeGitSettings(fs: JSRuntimeFS, settings: GitSettings):
|
||||
await fs.writeFile(GIT_CONFIG_PATH, JSON.stringify(settings, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Deploy settings from VFS
|
||||
*/
|
||||
export async function readDeploySettings(fs: JSRuntimeFS): Promise<DeploySettings> {
|
||||
const defaultSettings: DeploySettings = {
|
||||
providers: [],
|
||||
};
|
||||
|
||||
try {
|
||||
const content = await fs.readFile(DEPLOY_CONFIG_PATH, 'utf8');
|
||||
const data = JSON.parse(content);
|
||||
return deploySettingsSchema.parse(data);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
console.error('Deploy settings parsing error:', error.errors);
|
||||
}
|
||||
return defaultSettings;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write Deploy settings to VFS
|
||||
*/
|
||||
export async function writeDeploySettings(fs: JSRuntimeFS, settings: DeploySettings): Promise<void> {
|
||||
await ensureConfigDir(fs);
|
||||
await fs.writeFile(DEPLOY_CONFIG_PATH, JSON.stringify(settings, null, 2), 'utf8');
|
||||
}
|
||||
|
||||
+113
-113
@@ -371,134 +371,134 @@ export function DeploySettings() {
|
||||
<h4 className="text-sm font-medium">{t('addProvider')}</h4>
|
||||
<Accordion type="multiple" className="w-full space-y-2">
|
||||
{PRESET_PROVIDERS.map((preset) => {
|
||||
const isNostrPreset = preset.requiresNostr;
|
||||
const isLoggedIntoNostr = !!user;
|
||||
const showNostrLoginRequired = isNostrPreset && !isLoggedIntoNostr;
|
||||
const isNostrPreset = preset.requiresNostr;
|
||||
const isLoggedIntoNostr = !!user;
|
||||
const showNostrLoginRequired = isNostrPreset && !isLoggedIntoNostr;
|
||||
|
||||
return (
|
||||
<AccordionItem key={preset.id} value={preset.id} className="border rounded-lg">
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center justify-between w-full mr-3">
|
||||
<div className="flex flex-col items-start gap-1">
|
||||
<span className="font-medium">{preset.name}</span>
|
||||
<span className="text-xs text-muted-foreground font-normal">
|
||||
{preset.description}
|
||||
</span>
|
||||
</div>
|
||||
{preset.apiKeyURL && (
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-muted-foreground underline hover:text-foreground whitespace-nowrap ml-2"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
window.open(preset.apiKeyURL, '_blank');
|
||||
}}
|
||||
>
|
||||
{t('getApiKey')}
|
||||
</button>
|
||||
)}
|
||||
return (
|
||||
<AccordionItem key={preset.id} value={preset.id} className="border rounded-lg">
|
||||
<AccordionTrigger className="px-4 py-3 hover:no-underline">
|
||||
<div className="flex items-center justify-between w-full mr-3">
|
||||
<div className="flex flex-col items-start gap-1">
|
||||
<span className="font-medium">{preset.name}</span>
|
||||
<span className="text-xs text-muted-foreground font-normal">
|
||||
{preset.description}
|
||||
</span>
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
{showNostrLoginRequired ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t('loginToNostrRequired')}
|
||||
</p>
|
||||
<Button asChild className="w-full">
|
||||
<Link to="/settings/nostr">
|
||||
{t('goToNostrSettings')}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-name`}>
|
||||
{preset.apiKeyURL && (
|
||||
<button
|
||||
type="button"
|
||||
className="text-xs text-muted-foreground underline hover:text-foreground whitespace-nowrap ml-2"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
window.open(preset.apiKeyURL, '_blank');
|
||||
}}
|
||||
>
|
||||
{t('getApiKey')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
{showNostrLoginRequired ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t('loginToNostrRequired')}
|
||||
</p>
|
||||
<Button asChild className="w-full">
|
||||
<Link to="/settings/nostr">
|
||||
{t('goToNostrSettings')}
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-name`}>
|
||||
Name
|
||||
</Label>
|
||||
<Input
|
||||
id={`preset-${preset.id}-name`}
|
||||
placeholder="Provider name"
|
||||
value={presetNames[preset.id] || preset.name}
|
||||
onChange={(e) => setPresetNames(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{!preset.requiresNostr && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-apiKey`}>
|
||||
{preset.apiKeyLabel || t('apiKey')}
|
||||
</Label>
|
||||
<Input
|
||||
id={`preset-${preset.id}-name`}
|
||||
placeholder="Provider name"
|
||||
value={presetNames[preset.id] || preset.name}
|
||||
onChange={(e) => setPresetNames(prev => ({
|
||||
<PasswordInput
|
||||
id={`preset-${preset.id}-apiKey`}
|
||||
placeholder={t('enterApiKey')}
|
||||
value={presetApiKeys[preset.id] || ''}
|
||||
onChange={(e) => setPresetApiKeys(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!preset.requiresNostr && (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-apiKey`}>
|
||||
{preset.apiKeyLabel || t('apiKey')}
|
||||
</Label>
|
||||
<PasswordInput
|
||||
id={`preset-${preset.id}-apiKey`}
|
||||
placeholder={t('enterApiKey')}
|
||||
value={presetApiKeys[preset.id] || ''}
|
||||
onChange={(e) => setPresetApiKeys(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{preset.type === 'shakespeare' ? (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-host`}>
|
||||
{preset.type === 'shakespeare' ? (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-host`}>
|
||||
Host (Optional)
|
||||
</Label>
|
||||
<Input
|
||||
id={`preset-${preset.id}-host`}
|
||||
placeholder="shakespeare.wtf"
|
||||
value={presetHosts[preset.id] || ''}
|
||||
onChange={(e) => setPresetHosts(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-baseURL`}>
|
||||
</Label>
|
||||
<Input
|
||||
id={`preset-${preset.id}-host`}
|
||||
placeholder="shakespeare.wtf"
|
||||
value={presetHosts[preset.id] || ''}
|
||||
onChange={(e) => setPresetHosts(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`preset-${preset.id}-baseURL`}>
|
||||
Base URL (Optional)
|
||||
</Label>
|
||||
<Input
|
||||
id={`preset-${preset.id}-baseURL`}
|
||||
placeholder={
|
||||
preset.type === 'netlify' ? 'https://api.netlify.com/api/v1' :
|
||||
</Label>
|
||||
<Input
|
||||
id={`preset-${preset.id}-baseURL`}
|
||||
placeholder={
|
||||
preset.type === 'netlify' ? 'https://api.netlify.com/api/v1' :
|
||||
'https://api.vercel.com'
|
||||
}
|
||||
value={presetBaseURLs[preset.id] || ''}
|
||||
onChange={(e) => setPresetBaseURLs(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
}
|
||||
value={presetBaseURLs[preset.id] || ''}
|
||||
onChange={(e) => setPresetBaseURLs(prev => ({
|
||||
...prev,
|
||||
[preset.id]: e.target.value,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={
|
||||
(preset.requiresNostr && !isLoggedIntoNostr) ||
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={
|
||||
(preset.requiresNostr && !isLoggedIntoNostr) ||
|
||||
(!preset.requiresNostr && !presetApiKeys[preset.id]?.trim())
|
||||
}
|
||||
className="w-full gap-2"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
);
|
||||
})}
|
||||
</Accordion>
|
||||
</div>
|
||||
}
|
||||
className="w-full gap-2"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
{t('add')}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
);
|
||||
})}
|
||||
</Accordion>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user