/**
 * Pure helper functions for `olam refresh`.
 *
 * Kept in a separate file so unit tests can import them without
 * pulling in @olam/adapters (which requires a built package).
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
/**
 * Collect the CP source files to copy — mirrors build-devbox.sh:38-77.
 *
 * Includes:
 *   - standalone/*.mjs  (excluding *.test.mjs)
 *   - standalone/lib/*.mjs  (excluding *.test.mjs) when lib/ exists
 */
export function collectCpSourceFiles(standaloneDir) {
    if (!fs.existsSync(standaloneDir)) {
        throw new Error(`CP standalone dir not found: ${standaloneDir}`);
    }
    const entries = [];
    // Top-level *.mjs (except *.test.mjs) — mirrors build-devbox.sh:39-44
    const topLevel = fs.readdirSync(standaloneDir)
        .filter(f => {
        const stat = fs.statSync(path.join(standaloneDir, f));
        return stat.isFile() && f.endsWith('.mjs') && !f.endsWith('.test.mjs');
    })
        .sort();
    for (const f of topLevel) {
        entries.push({ srcPath: path.join(standaloneDir, f), destRelPath: f });
    }
    // lib/*.mjs (except *.test.mjs) — mirrors build-devbox.sh:53-61
    const libDir = path.join(standaloneDir, 'lib');
    if (fs.existsSync(libDir) && fs.statSync(libDir).isDirectory()) {
        const libFiles = fs.readdirSync(libDir)
            .filter(f => {
            const stat = fs.statSync(path.join(libDir, f));
            return stat.isFile() && f.endsWith('.mjs') && !f.endsWith('.test.mjs');
        })
            .sort();
        for (const f of libFiles) {
            entries.push({ srcPath: path.join(libDir, f), destRelPath: `lib/${f}` });
        }
    }
    return entries;
}
/**
 * Validate server.mjs is present — mirrors build-devbox.sh:62-66.
 * Throws if missing so the caller gets a clear, actionable error.
 */
export function validateServerMjsPresent(entries) {
    if (!entries.some(e => e.destRelPath === 'server.mjs')) {
        throw new Error('server.mjs not found in the source list — standalone/ may not be built. ' +
            'Use --skip-build to bypass this check.');
    }
}
//# sourceMappingURL=refresh-helpers.js.map