"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 = normalizePatterns(opts.patterns ? opts.patterns : ['.', '**']);
    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) {
    const normalizedPatterns = [];
    for (const pattern of patterns) {
        // We should add separate pattern for each extension
        // for some reason, fast-glob is buggy with /package.{json,yaml,json5} pattern
        normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json'));
        normalizedPatterns.push(pattern.replace(/\/?$/, '/package.json5'));
        normalizedPatterns.push(pattern.replace(/\/?$/, '/package.yaml'));
    }
    return normalizedPatterns;
}
// for backward compatibility
findPkgs['default'] = findPkgs; // tslint:disable-line
module.exports = findPkgs;
