import * as fs from 'fs';
import * as path from 'path';
export function regulateSlash(s) {
    return s.replace(/\\/g, '/');
}
export function listFilesRecursive(dir, includeFilters, excludeFilters) {
    const ret = [];
    const walk = function (subDirs) {
        const entries = fs.readdirSync(path.join(dir, subDirs));
        for (const e of entries) {
            const f = path.join(subDirs, e);
            const name = regulateSlash(f);
            if (!applyFiltersToStr(name, includeFilters, excludeFilters))
                continue;
            const fullName = path.join(dir, f);
            const stat = fs.lstatSync(fullName);
            if (stat.isFile()) {
                ret.push(name);
            }
            else if (stat.isDirectory()) {
                walk(f);
            }
        }
    };
    walk('');
    return ret;
}
export function applyFiltersToStr(str, includeFilters, excludeFilters) {
    if (includeFilters.length > 0) {
        if (!includeFilters.some(v => str.match(v) != null)) {
            return false;
        }
    }
    if (excludeFilters.some(v => str.match(v) != null)) {
        return false;
    }
    return true;
}
export function buildHierarchy(arr, separator, hierarchy = {}) {
    for (const s of arr) {
        const parts = s.split(separator);
        let o = hierarchy;
        for (let i = 0; i < parts.length; i++) {
            const key = parts.slice(0, i + 1).join(separator);
            if (!(key in o)) {
                if (i === parts.length - 1) {
                    o[key] = null;
                }
                else {
                    const subObject = {};
                    o[key] = subObject;
                }
            }
            const subObject = o[key];
            if (subObject === null)
                break;
            o = subObject;
        }
    }
    return hierarchy;
}
export function walkHierarchy(hierarchy, visitor, myName) {
    for (const key of Object.keys(hierarchy)) {
        if (myName !== undefined) {
            visitor(myName, key);
        }
        const child = hierarchy[key];
        if (child) {
            walkHierarchy(child, visitor, key);
        }
    }
}
//# sourceMappingURL=util.js.map