"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSystemGoalCacheArchiveStore = void 0;
const fs = require("fs-extra");
const os = require("os");
const path = require("path");
const child_process_1 = require("../../../api-helper/misc/child_process");
/**
 * Goal archive store that stores the compressed archives into the SDM cache directory.
 */
class FileSystemGoalCacheArchiveStore {
    async store(gi, classifier, archivePath) {
        const cacheDir = await FileSystemGoalCacheArchiveStore.getCacheDirectory(gi, classifier);
        const archiveName = FileSystemGoalCacheArchiveStore.archiveName;
        const archiveFileName = path.join(cacheDir, archiveName);
        await child_process_1.spawnLog("mv", [archivePath, archiveFileName], {
            log: gi.progressLog,
        });
        return archiveFileName;
    }
    async delete(gi, classifier) {
        const cacheDir = await FileSystemGoalCacheArchiveStore.getCacheDirectory(gi, classifier);
        const archiveName = FileSystemGoalCacheArchiveStore.archiveName;
        const archiveFileName = path.join(cacheDir, archiveName);
        await child_process_1.spawnLog("rm", ["-f", archiveFileName], {
            log: gi.progressLog,
        });
    }
    async retrieve(gi, classifier, targetArchivePath) {
        const cacheDir = await FileSystemGoalCacheArchiveStore.getCacheDirectory(gi, classifier);
        const archiveName = FileSystemGoalCacheArchiveStore.archiveName;
        const archiveFileName = path.join(cacheDir, archiveName);
        await child_process_1.spawnLog("cp", [archiveFileName, targetArchivePath], {
            log: gi.progressLog,
        });
    }
    static async getCacheDirectory(gi, classifier = "default") {
        const defaultCachePath = path.join(os.homedir(), ".atomist", "cache");
        const possibleCacheConfiguration = gi.configuration.sdm.cache;
        const sdmCacheDir = possibleCacheConfiguration ? (possibleCacheConfiguration.path || defaultCachePath) : defaultCachePath;
        const cacheDir = path.join(sdmCacheDir, classifier);
        await fs.mkdirs(cacheDir);
        return cacheDir;
    }
}
exports.FileSystemGoalCacheArchiveStore = FileSystemGoalCacheArchiveStore;
FileSystemGoalCacheArchiveStore.archiveName = "cache.tar.gz";
//# sourceMappingURL=FileSystemGoalCacheArchiveStore.js.map