/*
 * Copyright (c) 2025, Salesforce, Inc.
 * SPDX-License-Identifier: Apache-2
 * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
 */
import { globSync } from 'glob';
import path from 'node:path';
/**
 * Find cartridges recursively in a directory.
 *
 * Cartridges are identified by the presence of a `.project` file
 * (Eclipse project marker commonly used in SFCC development).
 *
 * @param directory - Directory to search for cartridges (defaults to cwd)
 * @param options - Filter options for including/excluding cartridges
 * @returns Array of discovered cartridge mappings
 *
 * @example
 * ```typescript
 * // Find all cartridges in current directory
 * const cartridges = findCartridges();
 *
 * // Find cartridges in specific directory
 * const cartridges = findCartridges('./my-project');
 *
 * // Find specific cartridges only
 * const cartridges = findCartridges('.', { include: ['app_storefront_base'] });
 *
 * // Find all except certain cartridges
 * const cartridges = findCartridges('.', { exclude: ['test_cartridge'] });
 * ```
 */
export function findCartridges(directory, options = {}) {
    const searchDir = directory ? path.resolve(directory) : process.cwd();
    // Find all .project files (Eclipse project markers)
    const projectFiles = globSync('**/.project', {
        cwd: searchDir,
        ignore: ['**/node_modules/**'],
    });
    let cartridges = projectFiles.map((f) => {
        const dirname = path.resolve(searchDir, path.dirname(f));
        const cartridgeName = path.basename(dirname);
        return {
            name: cartridgeName,
            dest: cartridgeName,
            src: dirname,
        };
    });
    // Apply include filter
    if (options.include && options.include.length > 0) {
        cartridges = cartridges.filter((c) => options.include.includes(c.name));
    }
    // Apply exclude filter
    if (options.exclude && options.exclude.length > 0) {
        cartridges = cartridges.filter((c) => !options.exclude.includes(c.name));
    }
    return cartridges;
}
//# sourceMappingURL=cartridges.js.map