import fs from 'node:fs'; import path from 'node:path'; import { buildPlan } from './core/plan.js'; import { createContext } from './core/context.js'; import { writePlan } from './core/generator.js'; import { installDependencies, manualInstallInstructions } from './core/installer.js'; import { collectOptions, confirmOverwrite, prompts, showError, showInfo, showIntro, showOutro, showStep, showSuccess, showSummary, showWarning, } from './prompts/index.js'; import { parseArgs, TEMPLATE_IDS, UsageError } from './utils/args.js'; import { emptyDirectory, isDirectoryEmpty, pathExists } from './utils/fs.js'; import { colors } from './utils/colors.js'; import { pmCommand } from './utils/strings.js'; function packageVersion() { try { const file = new URL('../package.json', import.meta.url); return JSON.parse(fs.readFileSync(file, 'utf8')).version ?? '0.0.0'; } catch { return '0.0.0'; } } function helpText() { return `${colors.bold('create-stackwise')} — scaffold a production-ready stack. ${colors.bold('Usage')} create-stackwise [directory] [options] ${colors.bold('Templates')} ${TEMPLATE_IDS.join(', ')} ${colors.bold('Options')} -t, --template Start from a template --pm npm, pnpm, yarn, or bun --install Install dependencies after scaffolding --no-install Skip dependency installation -y, --yes Use defaults without interactive prompts -f, --force Replace an existing non-empty directory -h, --help Show this help -v, --version Show the CLI version ${colors.bold('Examples')} create-stackwise my-app create-stackwise my-api --template express --pm pnpm --install create-stackwise my-app --template mern --yes --install `; } function logLine(interactive, method, message) { if (interactive) { method(message); } else { console.log(message); } } function targetDisplay(target) { const relative = path.relative(process.cwd(), target); return relative && !relative.startsWith('..') ? relative : target; } function nextSteps(context, target) { const cd = `cd ${targetDisplay(target)}`; if (context.isMonorepo) { const frontend = `${cd}${path.sep}${context.frontendPath()}\n${pmCommand(context.packageManager, 'dev')}`; const backend = context.hasPythonBackend ? `${cd}${path.sep}${context.backendPath()}\n${process.platform === 'win32' ? '.venv\\Scripts\\python' : '.venv/bin/python'} -m uvicorn app.main:app --reload --port ${context.backend.port}` : `${cd}${path.sep}${context.backendPath()}\n${pmCommand(context.packageManager, 'dev')}`; return `Run these in separate terminals:\n${frontend}\n\n${backend}`; } if (context.hasPythonBackend) { const python = process.platform === 'win32' ? '.venv\\Scripts\\python' : '.venv/bin/python'; return `${cd}\n${python} -m uvicorn app.main:app --reload --port ${context.backend.port}`; } return `${cd}\n${pmCommand(context.packageManager, 'dev')}`; } function summaryLines(context, plan, target, fileCount) { const stack = [ context.frontend?.label, context.backend?.label, context.database?.label, ].filter(Boolean); return [ `Directory: ${targetDisplay(target)}`, `Stack: ${stack.join(' + ') || 'Custom'}`, `Tools: ${context.toolModules.map((tool) => tool.label).join(', ') || 'None'}`, `Files: ${fileCount}`, '', 'Next:', nextSteps(context, target), ]; } export async function run(argv = process.argv.slice(2)) { const options = parseArgs(argv); if (options.help) { console.log(helpText()); return; } if (options.version) { console.log(packageVersion()); return; } const interactive = Boolean(process.stdout.isTTY) && !options.yes; if (interactive) showIntro(); let answers; try { answers = await collectOptions(options, { interactive }); } catch (error) { if (error instanceof UsageError) throw error; throw error; } const target = path.resolve(process.cwd(), answers.target); if (pathExists(target) && !isDirectoryEmpty(target)) { if (!options.force) { if (!interactive) { throw new Error( `Target directory "${target}" is not empty. Use --force to replace it or choose another directory.`, ); } const overwrite = await confirmOverwrite(target); if (prompts.isCancel(overwrite) || !overwrite) { prompts.cancel('Setup cancelled.'); return; } } if (!fs.statSync(target).isDirectory()) { throw new Error(`Target "${target}" is not a directory.`); } emptyDirectory(target); } const context = createContext({ targetDir: target, name: answers.target, presetId: answers.presetId, projectTypeLabel: answers.projectTypeLabel, frontend: answers.frontend, backend: answers.backend, database: answers.database, tools: answers.tools, packageManager: answers.packageManager, install: answers.install, }); const plan = buildPlan(context); logLine(interactive, showStep, `Writing ${plan.files.size} files to ${targetDisplay(target)}`); const fileCount = writePlan(target, plan); if (context.install) { logLine(interactive, showStep, 'Installing dependencies'); await installDependencies(plan, { onStep: (message) => logLine(interactive, showStep, message), }); logLine(interactive, showSuccess, 'Dependencies installed'); } else { const manual = manualInstallInstructions(plan); if (manual) { logLine(interactive, showInfo, `Dependencies were not installed. Install them manually:\n${manual}`); } } for (const note of plan.notes) { logLine(interactive, showInfo, note); } const summary = summaryLines(context, plan, target, fileCount); if (interactive) { showSummary(summary); showOutro('Happy building.'); } else { console.log(summary.join('\n')); } }