"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalFiles = exports.readSystemFile = exports.outputCacheFile = exports.readCacheFile = exports.cachePathExists = exports.rmCache = exports.listCacheDir = exports.statLocalFile = exports.chmodLocalFile = exports.findUpLocal = exports.localPathIsSymbolicLink = exports.localPathIsFile = exports.createCacheWriteStream = exports.readLocalDirectory = exports.findLocalSiblingOrParent = exports.isValidLocalPath = exports.localPathExists = exports.privateCacheDir = exports.ensureCacheDir = exports.ensureLocalDir = exports.ensureDir = exports.renameLocalFile = exports.deleteLocalFile = exports.writeLocalFile = exports.readLocalSymlink = exports.readLocalFile = exports.getSiblingFileName = exports.getParentDir = exports.pipeline = void 0;
const tslib_1 = require("tslib");
const node_stream_1 = tslib_1.__importDefault(require("node:stream"));
const node_util_1 = tslib_1.__importDefault(require("node:util"));
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const find_up_1 = tslib_1.__importDefault(require("find-up"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const upath_1 = tslib_1.__importDefault(require("upath"));
const global_1 = require("../../config/global");
const logger_1 = require("../../logger");
const util_1 = require("./util");
exports.pipeline = node_util_1.default.promisify(node_stream_1.default.pipeline);
function getParentDir(fileName) {
    return upath_1.default.parse(fileName).dir;
}
exports.getParentDir = getParentDir;
function getSiblingFileName(fileName, siblingName) {
    const subDirectory = getParentDir(fileName);
    return upath_1.default.join(subDirectory, siblingName);
}
exports.getSiblingFileName = getSiblingFileName;
async function readLocalFile(fileName, encoding) {
    const localFileName = (0, util_1.ensureLocalPath)(fileName);
    try {
        const fileContent = encoding
            ? await fs_extra_1.default.readFile(localFileName, encoding)
            : await fs_extra_1.default.readFile(localFileName);
        return fileContent;
    }
    catch (err) {
        logger_1.logger.trace({ err }, 'Error reading local file');
        return null;
    }
}
exports.readLocalFile = readLocalFile;
async function readLocalSymlink(fileName) {
    const localFileName = (0, util_1.ensureLocalPath)(fileName);
    try {
        const linkContent = await fs_extra_1.default.readlink(localFileName);
        return linkContent;
    }
    catch (err) {
        logger_1.logger.trace({ err }, 'Error reading local symlink');
        return null;
    }
}
exports.readLocalSymlink = readLocalSymlink;
async function writeLocalFile(fileName, fileContent) {
    const localFileName = (0, util_1.ensureLocalPath)(fileName);
    await fs_extra_1.default.outputFile(localFileName, fileContent);
}
exports.writeLocalFile = writeLocalFile;
async function deleteLocalFile(fileName) {
    // This a failsafe and hopefully will never be triggered
    if (global_1.GlobalConfig.get('platform') === 'local') {
        throw new Error('Cannot delete file when platform=local');
    }
    const localDir = global_1.GlobalConfig.get('localDir');
    if (localDir) {
        const localFileName = (0, util_1.ensureLocalPath)(fileName);
        await fs_extra_1.default.remove(localFileName);
    }
}
exports.deleteLocalFile = deleteLocalFile;
async function renameLocalFile(fromFile, toFile) {
    const fromPath = (0, util_1.ensureLocalPath)(fromFile);
    const toPath = (0, util_1.ensureLocalPath)(toFile);
    await fs_extra_1.default.move(fromPath, toPath);
}
exports.renameLocalFile = renameLocalFile;
async function ensureDir(dirName) {
    if (is_1.default.nonEmptyString(dirName)) {
        await fs_extra_1.default.ensureDir(dirName);
    }
}
exports.ensureDir = ensureDir;
async function ensureLocalDir(dirName) {
    const fullPath = (0, util_1.ensureLocalPath)(dirName);
    await fs_extra_1.default.ensureDir(fullPath);
    return fullPath;
}
exports.ensureLocalDir = ensureLocalDir;
async function ensureCacheDir(name) {
    const cacheDirName = (0, util_1.ensureCachePath)(`others/${name}`);
    await fs_extra_1.default.ensureDir(cacheDirName);
    return cacheDirName;
}
exports.ensureCacheDir = ensureCacheDir;
/**
 * Return the path of the private cache directory. This directory is wiped
 * between repositories, so they can be used to store private registries' index
 * without risk of that information leaking to other repositories/users.
 */
function privateCacheDir() {
    const cacheDir = global_1.GlobalConfig.get('cacheDir');
    return upath_1.default.join(cacheDir, '__renovate-private-cache');
}
exports.privateCacheDir = privateCacheDir;
async function localPathExists(pathName) {
    // Works for both files as well as directories
    const path = (0, util_1.ensureLocalPath)(pathName);
    try {
        const s = await fs_extra_1.default.stat(path);
        return !!s;
    }
    catch (_) {
        return false;
    }
}
exports.localPathExists = localPathExists;
/**
 * Validate local path without throwing.
 * @param path Path to check
 * @returns `true` if given `path` is a valid local path, otherwise `false`.
 */
function isValidLocalPath(path) {
    return (0, util_1.isValidPath)(path, 'localDir');
}
exports.isValidLocalPath = isValidLocalPath;
/**
 * Tries to find `otherFileName` in the directory where
 * `existingFileNameWithPath` is, then in its parent directory, then in the
 * grandparent, until we reach the top-level directory. All paths
 * must be relative to `localDir`.
 */
async function findLocalSiblingOrParent(existingFileNameWithPath, otherFileName) {
    if (upath_1.default.isAbsolute(existingFileNameWithPath)) {
        return null;
    }
    if (upath_1.default.isAbsolute(otherFileName)) {
        return null;
    }
    let current = existingFileNameWithPath;
    while (current !== '') {
        current = getParentDir(current);
        const candidate = upath_1.default.join(current, otherFileName);
        if (await localPathExists(candidate)) {
            return candidate;
        }
    }
    return null;
}
exports.findLocalSiblingOrParent = findLocalSiblingOrParent;
/**
 * Get files by name from directory
 */
async function readLocalDirectory(path) {
    const localPath = (0, util_1.ensureLocalPath)(path);
    const fileList = await fs_extra_1.default.readdir(localPath);
    return fileList;
}
exports.readLocalDirectory = readLocalDirectory;
function createCacheWriteStream(path) {
    const fullPath = (0, util_1.ensureCachePath)(path);
    return fs_extra_1.default.createWriteStream(fullPath);
}
exports.createCacheWriteStream = createCacheWriteStream;
async function localPathIsFile(pathName) {
    const path = (0, util_1.ensureLocalPath)(pathName);
    try {
        const s = await fs_extra_1.default.stat(path);
        return s.isFile();
    }
    catch (_) {
        return false;
    }
}
exports.localPathIsFile = localPathIsFile;
async function localPathIsSymbolicLink(pathName) {
    const path = (0, util_1.ensureLocalPath)(pathName);
    try {
        const s = await fs_extra_1.default.lstat(path);
        return s.isSymbolicLink();
    }
    catch (_) {
        return false;
    }
}
exports.localPathIsSymbolicLink = localPathIsSymbolicLink;
/**
 * Find a file or directory by walking up parent directories within localDir
 */
async function findUpLocal(fileName, cwd) {
    const { localDir } = global_1.GlobalConfig.get();
    const absoluteCwd = upath_1.default.join(localDir, cwd);
    const normalizedAbsoluteCwd = upath_1.default.normalizeSafe(absoluteCwd);
    const res = await (0, find_up_1.default)(fileName, {
        cwd: normalizedAbsoluteCwd,
        type: 'file',
    });
    // Return null if nothing found
    if (!is_1.default.nonEmptyString(res) || !is_1.default.nonEmptyString(localDir)) {
        return null;
    }
    const safePath = upath_1.default.normalizeSafe(res);
    // Return relative path if file is inside of local dir
    if (safePath.startsWith(localDir)) {
        let relativePath = safePath.replace(localDir, '');
        if (relativePath.startsWith('/')) {
            relativePath = relativePath.substring(1);
        }
        return relativePath;
    }
    // Return null if found file is outside of localDir
    return null;
}
exports.findUpLocal = findUpLocal;
function chmodLocalFile(fileName, mode) {
    const fullFileName = (0, util_1.ensureLocalPath)(fileName);
    return fs_extra_1.default.chmod(fullFileName, mode);
}
exports.chmodLocalFile = chmodLocalFile;
async function statLocalFile(fileName) {
    const fullFileName = (0, util_1.ensureLocalPath)(fileName);
    try {
        return await fs_extra_1.default.stat(fullFileName);
    }
    catch (_) {
        return null;
    }
}
exports.statLocalFile = statLocalFile;
function listCacheDir(path) {
    const fullPath = (0, util_1.ensureCachePath)(path);
    return fs_extra_1.default.readdir(fullPath);
}
exports.listCacheDir = listCacheDir;
async function rmCache(path) {
    const fullPath = (0, util_1.ensureCachePath)(path);
    await fs_extra_1.default.rm(fullPath, { recursive: true });
}
exports.rmCache = rmCache;
async function cachePathExists(pathName) {
    const path = (0, util_1.ensureCachePath)(pathName);
    try {
        const s = await fs_extra_1.default.stat(path);
        return !!s;
    }
    catch (_) {
        return false;
    }
}
exports.cachePathExists = cachePathExists;
function readCacheFile(fileName, encoding) {
    const fullPath = (0, util_1.ensureCachePath)(fileName);
    return encoding ? fs_extra_1.default.readFile(fullPath, encoding) : fs_extra_1.default.readFile(fullPath);
}
exports.readCacheFile = readCacheFile;
function outputCacheFile(file, data) {
    const filePath = (0, util_1.ensureCachePath)(file);
    return fs_extra_1.default.outputFile(filePath, data);
}
exports.outputCacheFile = outputCacheFile;
function readSystemFile(fileName, encoding) {
    return encoding ? fs_extra_1.default.readFile(fileName, encoding) : fs_extra_1.default.readFile(fileName);
}
exports.readSystemFile = readSystemFile;
async function getLocalFiles(fileNames) {
    const fileContentMap = {};
    for (const fileName of fileNames) {
        fileContentMap[fileName] = await readLocalFile(fileName, 'utf8');
    }
    return fileContentMap;
}
exports.getLocalFiles = getLocalFiles;
//# sourceMappingURL=index.js.map