"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cache = void 0;
var crypto = require("crypto");
var gluegun_1 = require("gluegun");
var lockFile = {
    yarn: "yarn.lock",
    pnpm: "pnpm-lock.yaml",
    npm: "package-lock.json",
    bun: "bun.lockb",
};
var MAC = "darwin";
var WINDOWS = "win32";
var LINUX = "linux";
var cachePath = (_a = {},
    _a[MAC] = "Library/Caches",
    _a[WINDOWS] = "AppData/Local/Temp",
    _a[LINUX] = ".cache",
    _a);
var path = gluegun_1.filesystem.path, dir = gluegun_1.filesystem.dir, homedir = gluegun_1.filesystem.homedir;
function hash(str) {
    return crypto.createHash("md5").update(str).digest("hex");
}
var targets = function (_a) {
    var rootDir = _a.rootDir, packagerName = _a.packagerName, platform = _a.platform;
    var cachePaths = [
        { type: "dir", path: path(rootDir, "node_modules") },
        { type: "file", path: path(rootDir, lockFile[packagerName]) },
    ];
    return cachePaths.filter(function (target) {
        return (target === null || target === void 0 ? void 0 : target.platform) ? target.platform.includes(platform) : true;
    });
};
function copy(options) {
    var fromRootDir = options.fromRootDir, toRootDir = options.toRootDir, packagerName = options.packagerName, _a = options.platform, platform = _a === void 0 ? process.platform : _a;
    var fromTargets = targets({ rootDir: fromRootDir, packagerName: packagerName, platform: platform });
    var toTargets = targets({ rootDir: toRootDir, packagerName: packagerName, platform: platform });
    return Promise.all(fromTargets.map(function (from, index) {
        var to = toTargets[index];
        if (from.type === "dir") {
            dir(from.path);
        }
        return gluegun_1.filesystem.copyAsync(from.path, to.path, { overwrite: true });
    }));
}
/**
 * Root directory path of ignite dependency cache
 */
function rootdir(platform) {
    var _a;
    if (platform === void 0) { platform = process.platform; }
    var folder = (_a = cachePath[platform]) !== null && _a !== void 0 ? _a : cachePath[LINUX];
    return path(homedir(), folder, "ignite");
}
function clear() {
    gluegun_1.filesystem.remove(rootdir());
}
/** Tool for managing cache of dependencies related to the ignite boilerplate */
exports.cache = {
    copy: copy,
    targets: targets,
    hash: hash,
    rootdir: rootdir,
    clear: clear,
};
//# sourceMappingURL=cache.js.map