Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install npm packages after plan ran. External link option #410

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/api/ai/plan-parser.mts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,12 @@ export async function parsePlan(
throw new Error('Failed to parse XML response');
}
}

export function getPackagesToInstall(plan: Plan): string[] {
return plan.actions
.filter(
(action): action is NpmInstallCommand =>
action.type === 'command' && action.command === 'npm install',
)
.flatMap((action) => action.packages);
}
33 changes: 19 additions & 14 deletions packages/api/apps/app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { asc, desc, eq } from 'drizzle-orm';
import { npmInstall } from '../exec.mjs';
import { generateApp } from '../ai/generate.mjs';
import { toValidPackageName } from '../apps/utils.mjs';
import { parsePlan } from '../ai/plan-parser.mjs';
import { getPackagesToInstall, parsePlan } from '../ai/plan-parser.mjs';

function toSecondsSinceEpoch(date: Date): number {
return Math.floor(date.getTime() / 1000);
Expand Down Expand Up @@ -54,19 +54,24 @@ export async function createAppWithAi(data: CreateAppWithAiSchemaType): Promise<
const plan = await parsePlan(result, app, data.prompt, randomid());
await applyPlan(app, plan);

// Run npm install again since we don't have a good way of parsing the plan to know if we should...
npmInstall({
cwd: pathToApp(app.externalId),
stdout(data) {
console.log(data.toString('utf8'));
},
stderr(data) {
console.error(data.toString('utf8'));
},
onExit(code) {
console.log(`npm install exit code: ${code}`);
},
});
const packagesToInstall = getPackagesToInstall(plan);

if (packagesToInstall.length > 0) {
console.log('installing packages', packagesToInstall);
npmInstall({
cwd: pathToApp(app.externalId),
packages: packagesToInstall,
stdout(data) {
console.log(data.toString('utf8'));
},
stderr(data) {
console.error(data.toString('utf8'));
},
onExit(code) {
console.log(`npm install exit code: ${code}`);
},
});
}

return app;
}
Expand Down
62 changes: 42 additions & 20 deletions packages/web/src/components/apps/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CircleAlertIcon,
PanelBottomOpenIcon,
PanelBottomCloseIcon,
ExternalLinkIcon,
} from 'lucide-react';
import { Link } from 'react-router-dom';
import { SrcbookLogo } from '@/components/logos';
Expand Down Expand Up @@ -46,7 +47,7 @@ type PropsType = {

export default function EditorHeader(props: PropsType) {
const { app, updateApp } = useApp();
const { start: startPreview, stop: stopPreview, status: previewStatus } = usePreview();
const { url, start: startPreview, stop: stopPreview, status: previewStatus } = usePreview();
const { status: npmInstallStatus, nodeModulesExists } = usePackageJson();
const [isExporting, setIsExporting] = useState(false);
const { open, togglePane, panelIcon } = useLogs();
Expand Down Expand Up @@ -170,25 +171,46 @@ export default function EditorHeader(props: PropsType) {
</TooltipProvider>
) : null}
{props.tab === 'preview' && previewStatus !== 'stopped' ? (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="icon"
size="icon"
onClick={() => {
stopPreview();
props.onChangeTab('code');
}}
className="active:translate-y-0"
disabled={previewStatus === 'booting' || previewStatus === 'connecting'}
>
<StopCircleIcon size={18} />
</Button>
</TooltipTrigger>
<TooltipContent>Stop dev server</TooltipContent>
</Tooltip>
</TooltipProvider>
<>
{url && (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Button
Copy link
Contributor

@benjreinhart benjreinhart Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a link rather than a button with window.open

variant="icon"
size="icon"
onClick={() => window.open(url as string, '_blank')}
className="active:translate-y-0"
disabled={previewStatus !== 'running'}
>
<ExternalLinkIcon size={18} />
</Button>
</TooltipTrigger>
<TooltipContent>Open in new tab</TooltipContent>
</Tooltip>
</TooltipProvider>
)}

<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="icon"
size="icon"
onClick={() => {
stopPreview();
props.onChangeTab('code');
}}
className="active:translate-y-0"
disabled={previewStatus !== 'running'}
>
<StopCircleIcon size={18} />
</Button>
</TooltipTrigger>
<TooltipContent>Stop dev server</TooltipContent>
</Tooltip>
</TooltipProvider>
</>
) : null}

<div
Expand Down