"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.job_handlers = void 0;
const job_handlers = new Map();
exports.job_handlers = job_handlers;
const fs = require('fs');
const path = require('path');
/**
 * 递归加载指定目录下所有 .js 文件
 * @param {string|string[]} dirPaths 要加载的目录路径(可以是字符串或数组)
 * @param {object} [options] 配置选项
 * @param {boolean} [options.ignoreNodeModules=true] 是否忽略 node_modules 目录
 * @param {RegExp|function} [options.filter] 自定义过滤条件
 * @returns {object} 包含所有加载模块的对象(以相对路径为键)
 */
function loadAllJSFiles(dirPaths, options = {}) {
    debugger;
    const { ignoreNodeModules = true, filter = null } = options;
    const loadedModules = {};
    // 统一处理为数组形式
    if (!Array.isArray(dirPaths)) {
        dirPaths = [dirPaths];
    }
    dirPaths.forEach(dirPath => {
        // 解析为绝对路径
        const absoluteDir = path.resolve(dirPath);
        // 递归读取目录
        function scanDirectory(currentDir, relativePath = '') {
            const files = fs.readdirSync(currentDir);
            files.forEach(file => {
                const fullPath = path.join(currentDir, file);
                const stat = fs.statSync(fullPath);
                const newRelativePath = path.join(relativePath, file);
                if (stat.isDirectory()) {
                    // 跳过 node_modules 目录(如果配置了忽略)
                    if (ignoreNodeModules && file === 'node_modules') {
                        return;
                    }
                    // 递归扫描子目录
                    scanDirectory(fullPath, newRelativePath);
                }
                else if (path.extname(file) === '.js' &&
                    (!filter ||
                        (typeof filter === 'function' && filter(fullPath)) ||
                        (filter instanceof RegExp && filter.test(fullPath)))) {
                    try {
                        // 加载模块并存储
                        const module = require(fullPath);
                        const moduleKey = newRelativePath.replace(/\.js$/, '');
                        loadedModules[moduleKey] = module;
                    }
                    catch (err) {
                        console.error(`加载模块失败: ${fullPath}`, err);
                    }
                }
            });
        }
        scanDirectory(absoluteDir);
    });
    return loadedModules;
}
// 使用示例
const modules = loadAllJSFiles(['./build/job'], {
    ignoreNodeModules: true,
    filter: (filePath) => !filePath.includes('build/job/index.js')
});
for (const key1 in modules) {
    var m = modules[key1];
    for (const key2 in m) {
        if (typeof m[key2] === 'function') {
            job_handlers.set(key2, m[key2]);
        }
    }
}
//# sourceMappingURL=index.js.map