"use strict";
const read_importer_manifest_1 = require("@pnpm/read-importer-manifest");
const fastGlob = require("fast-glob");
const pFilter = require("p-filter");
const path = require("path");
const DEFAULT_IGNORE = [
    '**/node_modules/**',
    '**/bower_components/**',
    '**/test/**',
    '**/tests/**',
];
async function findPkgs(root, opts) {
    opts = opts || {};
    const globOpts = Object.assign({}, opts, { cwd: root });
    globOpts.ignore = opts.ignore || DEFAULT_IGNORE;
    globOpts.patterns = opts.patterns
        ? normalizePatterns(opts.patterns)
        : ['**/package.{json,yaml,json5}'];
    const paths = await fastGlob(globOpts.patterns, globOpts);
    return pFilter(paths
        .sort()
        .map((manifestPath) => path.join(root, manifestPath))
        .map(async (manifestPath) => {
        try {
            return Object.assign({ path: path.dirname(manifestPath) }, await read_importer_manifest_1.readExactImporterManifest(manifestPath));
        }
        catch (err) {
            if (err.code === 'ENOENT') {
                return null;
            }
            throw err;
        }
    }), Boolean);
}
function normalizePatterns(patterns) {
    return patterns.map((pattern) => pattern.replace(/\/?$/, '/package.{json,yaml,json5}'));
}
// for backward compatibility
findPkgs['default'] = findPkgs; // tslint:disable-line
module.exports = findPkgs;
//# sourceMappingURL=index.js.map