#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const init_js_1 = require("./commands/init.js");
const run_js_1 = require("./commands/run.js");
const doctor_js_1 = require("./commands/doctor.js");
const list_js_1 = require("./commands/list.js");
const status_js_1 = require("./commands/status.js");
const serve_js_1 = require("./commands/serve.js");
const mcp_js_1 = require("./commands/mcp.js");
const install_js_1 = require("./commands/install.js");
const skill_js_1 = require("./commands/skill.js");
const fs_1 = require("fs");
const path_1 = require("path");
const cleanup_js_1 = require("./commands/cleanup.js");
const program = new commander_1.Command();
program
    .name('vsaf')
    .description('DAG-based workflow engine for AI coding agents')
    .version('0.1.0');
program
    .command('init')
    .description('Initialize .vsaf/ in current project')
    .action(async () => {
    try {
        await (0, init_js_1.execInit)(process.cwd());
        console.log('[vsaf] Initialized .vsaf/ successfully');
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
program
    .command('run <workflow>')
    .description('Execute a workflow')
    .option('--args <args>', 'Arguments to pass to the workflow', '')
    .option('--resume <runId>', 'Resume an interrupted workflow run')
    .action(async (workflow, opts) => {
    try {
        await (0, run_js_1.execRun)(workflow, process.cwd(), { args: opts.args, resume: opts.resume });
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
program
    .command('doctor')
    .description('Check environment and dependencies')
    .action(async () => {
    const results = await (0, doctor_js_1.runChecks)(process.cwd());
    console.log((0, doctor_js_1.formatChecks)(results));
});
program
    .command('list')
    .description('List available workflows')
    .action(() => {
    const cwd = process.cwd();
    const workflows = (0, list_js_1.listWorkflows)(cwd);
    if (workflows.length === 0) {
        const hasVsaf = (0, fs_1.existsSync)((0, path_1.join)(cwd, '.vsaf'));
        console.log(hasVsaf
            ? 'No workflows found in .vsaf/workflows/.'
            : 'No workflows found. Run "vsaf init" first.');
        return;
    }
    for (const wf of workflows) {
        console.log(`  ${wf.name.padEnd(25)} ${wf.description}`);
    }
});
program
    .command('status')
    .description('Show workflow run statuses')
    .action(() => {
    const runs = (0, status_js_1.getRunStatuses)(process.cwd());
    console.log((0, status_js_1.formatStatuses)(runs));
});
program
    .command('cleanup')
    .description('Delete stored run data from .vsaf/storage/')
    .option('--all', 'Delete all runs (not just failed)', false)
    .option('--dry-run', 'Show what would be deleted without deleting', false)
    .action(async (opts) => {
    try {
        await (0, cleanup_js_1.execCleanup)(process.cwd(), { all: opts.all, dryRun: opts.dryRun });
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
program
    .command('serve')
    .description('Start API server and web dashboard')
    .option('--port <port>', 'Port number', '3000')
    .option('--open', 'Auto-open browser', false)
    .action(async (opts) => {
    try {
        await (0, serve_js_1.execServe)(process.cwd(), { port: parseInt(opts.port, 10), open: opts.open });
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
program
    .command('mcp')
    .description('Start MCP server for Claude integration')
    .action(async () => {
    try {
        await (0, mcp_js_1.execMCP)();
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
program
    .command('install <pack>')
    .description('Install a skill pack (deploys to .claude/skills/ in current project)')
    .option('--force', 'Overwrite existing skills', false)
    .option('--skip-deps', 'Skip npm dependency installation', false)
    .action((pack, opts) => {
    try {
        (0, install_js_1.execInstall)(pack, process.cwd(), { force: opts.force, skipDeps: opts.skipDeps });
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
const skillCmd = program
    .command('skill')
    .description('Manage skills');
skillCmd
    .command('add <source>')
    .description('Install skill from local path, git URL, or built-in pack')
    .option('--only <names>', 'Only install specific skills (comma-separated)', (val) => val.split(','))
    .option('--force', 'Overwrite existing skills', false)
    .action((source, opts) => {
    try {
        const result = (0, skill_js_1.skillAdd)(source, process.cwd(), { only: opts.only, force: opts.force });
        for (const name of result.added)
            console.log(`[vsaf] Added: ${name}`);
        for (const name of result.skipped)
            console.log(`[vsaf] Skipped: ${name} (already exists)`);
        console.log(`[vsaf] ${result.added.length} skills installed`);
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
skillCmd
    .command('list')
    .description('List installed skills')
    .action(() => {
    const skills = (0, skill_js_1.skillList)(process.cwd());
    if (skills.length === 0) {
        console.log('No skills installed. Run "vsaf skill add <source>" to install.');
        return;
    }
    console.log('NAME'.padEnd(20) + 'DESCRIPTION'.padEnd(35) + 'FILES  LINKED');
    for (const s of skills) {
        const linked = s.linkedCommand ? `✓ ${s.linkedCommand}` : '✗';
        console.log(`${s.name.padEnd(20)}${s.description.padEnd(35)}${String(s.fileCount).padEnd(7)}${linked}`);
    }
});
skillCmd
    .command('info <name>')
    .description('Show skill details')
    .action((name) => {
    try {
        const info = (0, skill_js_1.skillInfo)(name, process.cwd());
        console.log(`Name:         ${info.name}`);
        console.log(`Description:  ${info.description}`);
        if (info.version)
            console.log(`Version:      ${info.version}`);
        if (info.author)
            console.log(`Author:       ${info.author}`);
        console.log(`\nFiles:`);
        for (const f of info.files)
            console.log(`  ${f}`);
        if (info.linkedCommands.length > 0) {
            console.log(`\nUsed by:`);
            for (const c of info.linkedCommands)
                console.log(`  ${c}`);
        }
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
skillCmd
    .command('remove <name>')
    .description('Remove an installed skill')
    .action((name) => {
    try {
        const result = (0, skill_js_1.skillRemove)(name, process.cwd());
        if (result.warnings.length > 0) {
            console.log(`[vsaf] Warning: these commands reference the removed skill:`);
            for (const w of result.warnings)
                console.log(`  ${w}`);
        }
        console.log(`[vsaf] Removed skill: ${result.removed}`);
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
skillCmd
    .command('init <pack>')
    .description('Scaffold commands and workflows for a skill pack')
    .action((pack) => {
    try {
        const packInfo = (0, skill_js_1.resolvePackInfo)(pack, process.cwd());
        const result = (0, skill_js_1.skillInit)(pack, process.cwd(), packInfo.skillNames, { workflows: packInfo.workflows });
        for (const c of result.commandsCreated)
            console.log(`[vsaf] Created command: commands/${c}.md (→ skill: ${c})`);
        for (const c of result.commandsSkipped)
            console.log(`[vsaf] Skipped command: commands/${c}.md (already exists)`);
        for (const w of result.workflowsCreated)
            console.log(`[vsaf] Created workflow: workflows/${w}`);
        console.log(`[vsaf] ${result.commandsCreated.length} commands + ${result.workflowsCreated.length} workflows created`);
        if (result.commandsCreated.length > 0) {
            console.log(`[vsaf] → Edit commands/ files to add project-specific context`);
        }
    }
    catch (err) {
        console.error(`[vsaf] Error: ${err.message}`);
        process.exit(1);
    }
});
program.parse();
//# sourceMappingURL=index.js.map