index.ts 1.63 KB
import fs from 'node:fs';
import path from 'node:path';
import { logger } from '../../utils/logger';
import { GraphState } from '../../state';
import { writeScaffold, getScaffoldEssentialFiles } from './scaffold';
import { getProjectProfile } from '../../utils/project-profile';

/**
 * '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 });
    const profile = getProjectProfile();
    writeScaffold(appPath, appName, profile);

    // Validate that essential files/directories were created (framework-specific).
    const essentialFiles = getScaffoldEssentialFiles(profile);
    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 {};
};