"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.moveFile = exports.cloneDirectory = exports.checkDistDirectory = exports.checkWorkingDirectory = exports.createRecursiveDirectory = exports.createDirectory = exports.auditFile = exports.auditDirectoryFiles = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const _directorys_1 = __importDefault(require("./_directorys"));
const _sleep_1 = __importDefault(require("./_sleep"));
/**
 * Creates a directory from a path as a string.
 * @param directoryToMake
 */
const createDirectory = (directoryToMake) => __awaiter(void 0, void 0, void 0, function* () {
    yield (0, _sleep_1.default)(250);
    if ((yield fs_extra_1.default.pathExists(directoryToMake)) === true) {
        return false;
    }
    yield fs_extra_1.default.mkdir(directoryToMake);
    return true;
});
exports.createDirectory = createDirectory;
/**
 * Creates the directories from an array recursively.
 * @param directoriesToMake
 */
const createRecursiveDirectory = (directoriesToMake) => { var directoriesToMake_1, directoriesToMake_1_1; return __awaiter(void 0, void 0, void 0, function* () {
    var e_1, _a;
    try {
        yield (0, _sleep_1.default)(250);
        try {
            for (directoriesToMake_1 = __asyncValues(directoriesToMake); directoriesToMake_1_1 = yield directoriesToMake_1.next(), !directoriesToMake_1_1.done;) {
                const dir = directoriesToMake_1_1.value;
                yield createDirectory(dir);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (directoriesToMake_1_1 && !directoriesToMake_1_1.done && (_a = directoriesToMake_1.return)) yield _a.call(directoriesToMake_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    catch (err) {
        return console.error(err);
    }
}); };
exports.createRecursiveDirectory = createRecursiveDirectory;
// TODO: Refactor checks for directories into one function that takes an array of directories..
/**
 * Checks the working directory, and returns true if all directories exist.
 */
const checkWorkingDirectory = () => __awaiter(void 0, void 0, void 0, function* () {
    yield (0, _sleep_1.default)(250);
    const directories = [
        yield fs_extra_1.default.pathExists(_directorys_1.default.shopRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.productionRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.developmentRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.devRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.scriptsRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.scriptsModuleRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.stylesRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.fontsRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.imagesRoot),
    ];
    return !directories.includes(false);
});
exports.checkWorkingDirectory = checkWorkingDirectory;
/**
 * Checks the production directory and returns true if all directories exist.
 */
const checkDistDirectory = () => __awaiter(void 0, void 0, void 0, function* () {
    yield (0, _sleep_1.default)(250);
    const directories = [
        yield fs_extra_1.default.pathExists(_directorys_1.default.shopRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.productionRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distAssetsRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distConfigRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distLayoutRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distLocalesRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distSectionsRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distSnippetsRoot),
        yield fs_extra_1.default.pathExists(_directorys_1.default.distTemplatesRoot),
    ];
    return !directories.includes(false);
});
exports.checkDistDirectory = checkDistDirectory;
const cloneDirectory = (directoryToCopy = _directorys_1.default.productionRoot, directoryDestination = _directorys_1.default.developmentRoot) => __awaiter(void 0, void 0, void 0, function* () {
    yield (0, _sleep_1.default)(250);
    yield fs_extra_1.default.copy(directoryToCopy, directoryDestination);
});
exports.cloneDirectory = cloneDirectory;
const moveFile = (fileToMove, fileDestination) => __awaiter(void 0, void 0, void 0, function* () {
    yield (0, _sleep_1.default)(250);
    yield fs_extra_1.default.move(fileToMove, `${fileDestination}/${path_1.default.basename(fileToMove)}`);
});
exports.moveFile = moveFile;
const auditFile = (file, auditSettings) => { var auditSettings_1, auditSettings_1_1; return __awaiter(void 0, void 0, void 0, function* () {
    var e_2, _a;
    const settings = [];
    if (file.includes('.liquid')) {
        const fileContent = yield fs_extra_1.default.readFile(file);
        try {
            for (auditSettings_1 = __asyncValues(auditSettings); auditSettings_1_1 = yield auditSettings_1.next(), !auditSettings_1_1.done;) {
                const setting = auditSettings_1_1.value;
                const settingString = `settings.${setting}`;
                if (fileContent.toString().includes(settingString)) {
                    settings.push(settingString);
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (auditSettings_1_1 && !auditSettings_1_1.done && (_a = auditSettings_1.return)) yield _a.call(auditSettings_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
    }
    return settings;
}); };
exports.auditFile = auditFile;
const auditDirectoryFiles = () => __awaiter(void 0, void 0, void 0, function* () {
    var e_3, _a, e_4, _b;
    const files = [];
    const auditDirectories = [
        _directorys_1.default.layoutRoot,
        _directorys_1.default.templatesRoot,
        _directorys_1.default.customersRoot,
        _directorys_1.default.snippetsRoot,
        _directorys_1.default.sectionsRoot,
    ];
    try {
        for (var auditDirectories_1 = __asyncValues(auditDirectories), auditDirectories_1_1; auditDirectories_1_1 = yield auditDirectories_1.next(), !auditDirectories_1_1.done;) {
            const directory = auditDirectories_1_1.value;
            const filesToAudit = yield fs_extra_1.default.readdir(directory);
            try {
                for (var filesToAudit_1 = (e_4 = void 0, __asyncValues(filesToAudit)), filesToAudit_1_1; filesToAudit_1_1 = yield filesToAudit_1.next(), !filesToAudit_1_1.done;) {
                    const file = filesToAudit_1_1.value;
                    yield Promise.resolve(files.push(`${directory}/${file}`));
                }
            }
            catch (e_4_1) { e_4 = { error: e_4_1 }; }
            finally {
                try {
                    if (filesToAudit_1_1 && !filesToAudit_1_1.done && (_b = filesToAudit_1.return)) yield _b.call(filesToAudit_1);
                }
                finally { if (e_4) throw e_4.error; }
            }
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (auditDirectories_1_1 && !auditDirectories_1_1.done && (_a = auditDirectories_1.return)) yield _a.call(auditDirectories_1);
        }
        finally { if (e_3) throw e_3.error; }
    }
    return files;
});
exports.auditDirectoryFiles = auditDirectoryFiles;
