index.ts 1.54 KB
import fs from 'node:fs';
import path from 'node:path';
import { logger } from '../../utils/logger';
import { GraphState } from '../../state';
import { writeScaffold } from './scaffold';

/**
 * 'initial' node: initialize the empty project scaffold.
 *
 * The scaffold is deterministic (React V18 + TS + Vite + TailwindCSS V4 + Less),
 * so we generate it directly instead of driving an LLM agent. This is instant,
 * reproducible, and keeps the model out of the critical path — the previous
 * agent-based approach could hang indefinitely against some OpenAI-compatible
 * endpoints (e.g. Aliyun MAAS Qwen) due to an unbounded agent loop.
 */
export const initialProject = async (state: GraphState) => {
    logger.printInfoLog('Initializing project...');

    const appPath = state.workspace.app;
    const appName = state.urlInfo.name || 'my-app';
    if (!appPath) {
        throw new Error('Workspace application path is not defined.');
    }

    fs.mkdirSync(appPath, { recursive: true });
    writeScaffold(appPath, appName);

    // Validate that essential files/directories were created.
    const essentialFiles = ['package.json', 'src', 'vite.config.ts', 'tsconfig.json', 'index.html', 'src/main.tsx', 'src/App.tsx'];
    for (const file of essentialFiles) {
        const fullPath = path.join(appPath, file);
        if (!fs.existsSync(fullPath)) {
            throw new Error(`Critical file/directory missing: "${file}". The project scaffolding may have failed.`);
        }
    }

    logger.printSuccessLog(`Scaffold created at ${appPath}`);

    return {};
};