import { existsSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
function directoryHasAtLeastOneFile(dirPath) {
    if (!existsSync(dirPath))
        return false;
    const entries = readdirSync(dirPath, { withFileTypes: true });
    for (const entry of entries) {
        const candidate = join(dirPath, entry.name);
        if (entry.isFile())
            return true;
        if (entry.isDirectory() && directoryHasAtLeastOneFile(candidate))
            return true;
    }
    return false;
}
function isWorkspacePackageBuilt(srcDir) {
    return directoryHasAtLeastOneFile(join(srcDir, 'dist'));
}
export async function ensureBundledWorkspacePackagesBuilt(_params) {
    const params = _params;
    const missing = new Map();
    for (const bundle of params.bundles) {
        if (!isWorkspacePackageBuilt(bundle.srcDir)) {
            missing.set(bundle.packageName, bundle);
        }
    }
    for (const bundle of missing.values()) {
        params.runCommand(params.yarn.cmd, [...params.yarn.args, 'workspace', bundle.packageName, 'build'], { cwd: params.repoRoot });
        if (!isWorkspacePackageBuilt(bundle.srcDir)) {
            throw new Error(`[component-artifacts] bundled workspace package build did not produce dist output: ${bundle.packageName} (${join(bundle.srcDir, 'dist')})`);
        }
    }
}
//# sourceMappingURL=ensureBundledWorkspacePackagesBuilt.js.map