"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const flatten = require("arr-flatten");
const fs = require("mz/fs");
const pFilter = require("p-filter");
const path = require("path");
async function default_1(modules) {
    const dirs = await getDirectories(modules);
    const subdirs = await Promise.all(dirs.map((dir) => {
        return isScopedPkgsDir(dir) ? getDirectories(dir) : Promise.resolve([dir]);
    }));
    return flatten(subdirs);
}
exports.default = default_1;
async function getDirectories(srcPath) {
    let dirs;
    try {
        dirs = await fs.readdir(srcPath);
    }
    catch (err) {
        if (err.code !== 'ENOENT') {
            throw err;
        }
        dirs = [];
    }
    return pFilter(dirs
        .filter((relativePath) => !relativePath.startsWith('.')) // ignore directories like .bin, .store, etc
        .map((relativePath) => path.join(srcPath, relativePath)), async (absolutePath) => {
        try {
            const stats = await fs.stat(absolutePath);
            return stats.isDirectory();
        }
        catch (err) {
            if (err.code !== 'ENOENT')
                throw err;
            // Cannot find file at ${absolutePath} although it was listed by readdir.
            // This probably means that the target of the symlink does not exist (broken symlink).
            // This used to be a warning but it didn't really cause any issues.
            return false;
        }
    });
}
function isScopedPkgsDir(dirPath) {
    return path.basename(dirPath).startsWith('@');
}
//# sourceMappingURL=getPkgDirs.js.map