// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import { FileSystem } from '@rushstack/node-core-library';
const DEFAULT_BUILD_CACHE_FOLDER_NAME = 'build-cache';
/**
 * A build cache provider using the local file system.
 * Required by all cloud cache providers.
 * @beta
 */
export class FileSystemBuildCacheProvider {
    constructor(options) {
        this._cacheFolderPath =
            options.rushUserConfiguration.buildCacheFolder ||
                path.join(options.rushConfiguration.commonTempFolder, DEFAULT_BUILD_CACHE_FOLDER_NAME);
    }
    /**
     * Returns the absolute disk path for the specified cache id.
     */
    getCacheEntryPath(cacheId) {
        return path.join(this._cacheFolderPath, cacheId);
    }
    /**
     * Validates that the specified cache id exists on disk, and returns the path if it does.
     */
    async tryGetCacheEntryPathByIdAsync(terminal, cacheId) {
        const cacheEntryFilePath = this.getCacheEntryPath(cacheId);
        if (await FileSystem.existsAsync(cacheEntryFilePath)) {
            return cacheEntryFilePath;
        }
        else {
            return undefined;
        }
    }
    /**
     * Writes the specified buffer to the corresponding file system path for the cache id.
     */
    async trySetCacheEntryBufferAsync(terminal, cacheId, entryBuffer) {
        const cacheEntryFilePath = this.getCacheEntryPath(cacheId);
        await FileSystem.writeFileAsync(cacheEntryFilePath, entryBuffer, { ensureFolderExists: true });
        terminal.writeVerboseLine(`Wrote cache entry to "${cacheEntryFilePath}".`);
        return cacheEntryFilePath;
    }
}
//# sourceMappingURL=FileSystemBuildCacheProvider.js.map