"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const shelljs_1 = require("shelljs");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function copyFiles(source, dest) {
    shelljs_1.mkdir('-p', dest);
    shelljs_1.cp('-R', source, dest);
}
exports.copyFiles = copyFiles;
function removeFile(file) {
    if (fs_1.default.existsSync(file)) {
        shelljs_1.rm('-rf', file);
    }
}
exports.removeFile = removeFile;
function findLWCFolderPath(rootPath) {
    return findFolderWithDefaultPath(rootPath, path_1.default.join('main', 'default'), 'lwc', new Set([
        'aura',
        'classes',
        'contentassets',
        'triggers',
        'labels',
        'layouts',
        'objects',
        'staticresources'
    ]));
}
exports.findLWCFolderPath = findLWCFolderPath;
function findFileWithDefaultPath(rootPath, defaultPath, parentDir, fileName, foldersToIgnore = new Set([])) {
    let filePath = '';
    const fullDefaultPath = path_1.default.join(rootPath, defaultPath, parentDir, fileName);
    if (fs_1.default.existsSync(fullDefaultPath)) {
        filePath = fullDefaultPath;
    }
    else {
        const parentDirPath = findFolders(rootPath, parentDir, [], foldersToIgnore)[0];
        if (parentDirPath &&
            fs_1.default.existsSync(path_1.default.join(parentDirPath, fileName))) {
            filePath = path_1.default.join(parentDirPath, fileName);
        }
    }
    return filePath;
}
exports.findFileWithDefaultPath = findFileWithDefaultPath;
function findFolderWithDefaultPath(rootPath, defaultPath, folderName, foldersToIgnore = new Set([])) {
    let folderPath = '';
    const fullDefaultPath = path_1.default.join(rootPath, defaultPath, folderName);
    if (fs_1.default.existsSync(fullDefaultPath) &&
        fs_1.default.statSync(fullDefaultPath).isDirectory()) {
        folderPath = fullDefaultPath;
    }
    else {
        folderPath = findFolders(rootPath, folderName, [], foldersToIgnore)[0];
    }
    return folderPath;
}
exports.findFolderWithDefaultPath = findFolderWithDefaultPath;
function findAllFolderPaths(rootPath, directoriesToCheck, targetFolder, foldersToIgnore = new Set([])) {
    let targetFolderPaths = [];
    if (directoriesToCheck && directoriesToCheck.length > 0) {
        directoriesToCheck.forEach(directory => {
            const folderPaths = findFolders(path_1.default.join(rootPath, directory), targetFolder, [], foldersToIgnore);
            const pathIndex = targetFolderPaths.length > 0 ? targetFolderPaths.length - 1 : 0;
            targetFolderPaths.splice(pathIndex, 0, ...folderPaths);
        });
    }
    return targetFolderPaths;
}
exports.findAllFolderPaths = findAllFolderPaths;
function findFolders(rootPath, folderName, folderArray = [], foldersToIgnore = new Set([])) {
    if (!fs_1.default.existsSync(rootPath) || !fs_1.default.statSync(rootPath).isDirectory()) {
        return folderArray;
    }
    const dirChildren = fs_1.default.readdirSync(rootPath);
    const filteredDirChildren = dirChildren.filter(item => {
        return !foldersToIgnore.has(item);
    });
    for (let i = 0; i < filteredDirChildren.length; i++) {
        const file = filteredDirChildren[i];
        if (fs_1.default.statSync(path_1.default.join(rootPath, file)).isDirectory()) {
            if (file === folderName) {
                folderArray.push(path_1.default.join(rootPath, file));
                break;
            }
            else {
                folderArray = findFolders(path_1.default.join(rootPath, file), folderName, folderArray, foldersToIgnore);
            }
        }
    }
    return folderArray;
}
exports.findFolders = findFolders;
function getFileContents(filePath) {
    let contents = undefined;
    if (fs_1.default.existsSync(filePath)) {
        try {
            contents = fs_1.default.readFileSync(filePath, 'utf-8');
        }
        catch (e) {
            console.error(`Loading ${filePath} failed parsing with error ${e.message}`);
        }
    }
    return contents;
}
exports.getFileContents = getFileContents;
//# sourceMappingURL=fileUtils.js.map