"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("mz/fs");
const path = require("path");
const flatten = require("arr-flatten");
const pFilter = require("p-filter");
function default_1(modules) {
    return __awaiter(this, void 0, void 0, function* () {
        const dirs = yield getDirectories(modules);
        const subdirs = yield Promise.all(dirs.map((dir) => {
            return isScopedPkgsDir(dir) ? getDirectories(dir) : Promise.resolve([dir]);
        }));
        return flatten(subdirs);
    });
}
exports.default = default_1;
function getDirectories(srcPath) {
    return __awaiter(this, void 0, void 0, function* () {
        let dirs;
        try {
            dirs = yield fs.readdir(srcPath);
        }
        catch (err) {
            if (err.code !== 'ENOENT') {
                throw err;
            }
            dirs = [];
        }
        return pFilter(dirs
            .filter(relativePath => relativePath[0] !== '.') // ignore directories like .bin, .store, etc
            .map(relativePath => path.join(srcPath, relativePath)), (absolutePath) => __awaiter(this, void 0, void 0, function* () {
            const stats = yield fs.stat(absolutePath);
            return stats.isDirectory();
        }));
    });
}
function isScopedPkgsDir(dirPath) {
    return path.basename(dirPath)[0] === '@';
}
//# sourceMappingURL=getPkgDirs.js.map