import fs from 'node:fs';
import path from 'node:path';
import { detectJsPackageManager, hasDependency, installCommandFor, readPackageJson, runScriptCommandFor, } from './packageManager.js';
function fileExists(root, names) {
    return names.some((name) => fs.existsSync(path.join(root, name)));
}
/**
 * Vite + Vue SFC projects (non-Nuxt).
 */
export class VueAdapter {
    root;
    kind = 'vue';
    label;
    packageManager;
    pkg;
    constructor(root) {
        this.root = root;
        this.pkg = readPackageJson(root);
        this.packageManager = detectJsPackageManager(root);
        this.label = 'Vue';
    }
    installCommand() {
        const { command, args } = installCommandFor(this.packageManager);
        return { command, args, cwd: this.root };
    }
    devCommand() {
        return this.requireScript(['dev', 'start']);
    }
    buildCommand() {
        return this.scriptOrNull(['build']);
    }
    testCommand(extraArgs = []) {
        return this.scriptOrNull(['test'], extraArgs);
    }
    lintCommand(fix = false) {
        return this.scriptOrNull(fix ? ['format', 'lint:fix'] : ['lint']);
    }
    readyPattern() {
        return /Local:\s+(https?:\/\/\S+)/i;
    }
    describe() {
        return {
            kind: this.kind,
            label: this.label,
            root: this.root,
            packageManager: this.packageManager,
            vueVersion: this.pkg?.dependencies?.vue ?? this.pkg?.devDependencies?.vue ?? null,
            typescript: hasDependency(this.pkg, 'typescript') || fileExists(this.root, ['tsconfig.json']),
            vite: fileExists(this.root, ['vite.config.js', 'vite.config.ts', 'vite.config.mjs']),
            scripts: this.pkg?.scripts ?? {},
        };
    }
    requireScript(candidates) {
        const cmd = this.scriptOrNull(candidates);
        if (!cmd) {
            throw new Error(`No "${candidates.join('"/"')}" script found in package.json at "${this.root}".`);
        }
        return cmd;
    }
    scriptOrNull(candidates, extraArgs = []) {
        const script = candidates.find((name) => this.pkg?.scripts?.[name]);
        if (!script)
            return null;
        const { command, args } = runScriptCommandFor(this.packageManager, script, extraArgs);
        return { command, args, cwd: this.root };
    }
}
/**
 * Nuxt (Vue meta-framework) projects.
 */
export class NuxtAdapter {
    root;
    kind = 'nuxt';
    label = 'Nuxt';
    packageManager;
    pkg;
    constructor(root) {
        this.root = root;
        this.pkg = readPackageJson(root);
        this.packageManager = detectJsPackageManager(root);
    }
    installCommand() {
        const { command, args } = installCommandFor(this.packageManager);
        return { command, args, cwd: this.root };
    }
    devCommand() {
        return this.requireScript(['dev', 'start']);
    }
    buildCommand() {
        return this.scriptOrNull(['build', 'generate']);
    }
    testCommand(extraArgs = []) {
        return this.scriptOrNull(['test'], extraArgs);
    }
    lintCommand(fix = false) {
        return this.scriptOrNull(fix ? ['format', 'lint:fix'] : ['lint']);
    }
    readyPattern() {
        return /Local:\s+(https?:\/\/\S+)|Nuxt\s+.+ready/i;
    }
    describe() {
        return {
            kind: this.kind,
            label: this.label,
            root: this.root,
            packageManager: this.packageManager,
            nuxtVersion: this.pkg?.dependencies?.nuxt ?? this.pkg?.devDependencies?.nuxt ?? null,
            typescript: hasDependency(this.pkg, 'typescript') || fileExists(this.root, ['tsconfig.json']),
            scripts: this.pkg?.scripts ?? {},
        };
    }
    requireScript(candidates) {
        const cmd = this.scriptOrNull(candidates);
        if (!cmd) {
            throw new Error(`No "${candidates.join('"/"')}" script found in package.json at "${this.root}".`);
        }
        return cmd;
    }
    scriptOrNull(candidates, extraArgs = []) {
        const script = candidates.find((name) => this.pkg?.scripts?.[name]);
        if (!script)
            return null;
        const { command, args } = runScriptCommandFor(this.packageManager, script, extraArgs);
        return { command, args, cwd: this.root };
    }
}
//# sourceMappingURL=vue.js.map