/**
 * Copyright (c) Microsoft Corporation.
 *
 * 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.
 */
import fs from 'fs';
import os from 'os';
import path from 'path';
import { calculateSha1 } from './crypto';
export const existsAsync = (path) => new Promise(resolve => fs.stat(path, err => resolve(!err)));
export async function mkdirIfNeeded(filePath) {
    // This will harmlessly throw on windows if the dirname is the root directory.
    await fs.promises.mkdir(path.dirname(filePath), { recursive: true }).catch(() => { });
}
export async function removeFolders(dirs) {
    return await Promise.all(dirs.map((dir) => fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 10 }).catch(e => e)));
}
export function canAccessFile(file) {
    if (!file)
        return false;
    try {
        fs.accessSync(file);
        return true;
    }
    catch (e) {
        return false;
    }
}
export function isWritable(file) {
    try {
        fs.accessSync(file, fs.constants.W_OK);
        return true;
    }
    catch {
        return false;
    }
}
export function isSystemDirectory(dir) {
    const resolved = path.resolve(dir);
    if (process.platform === 'win32') {
        const systemRoot = path.resolve(process.env.SystemRoot || 'C:\\Windows');
        return isPathInside(systemRoot.toLowerCase(), resolved.toLowerCase());
    }
    return resolved === '/';
}
export async function copyFileAndMakeWritable(from, to) {
    await fs.promises.copyFile(from, to);
    await fs.promises.chmod(to, 0o664);
}
export function addSuffixToFilePath(filePath, suffix) {
    const ext = path.extname(filePath);
    const base = filePath.substring(0, filePath.length - ext.length);
    return base + suffix + ext;
}
export function sanitizeForFilePath(s) {
    return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
}
export function isPathInside(root, candidate) {
    const resolvedRoot = path.resolve(root);
    const resolvedCandidate = path.resolve(candidate);
    if (resolvedCandidate === resolvedRoot)
        return true;
    return resolvedCandidate.startsWith(resolvedRoot + path.sep);
}
export function resolveWithinRoot(root, fileName) {
    if (path.isAbsolute(fileName))
        return null;
    const resolvedFile = path.resolve(root, fileName);
    return isPathInside(root, resolvedFile) ? resolvedFile : null;
}
export function toPosixPath(aPath) {
    return aPath.split(path.sep).join(path.posix.sep);
}
export function makeSocketPath(domain, name) {
    const userNameHash = calculateSha1(process.env.USERNAME || process.env.USER || 'default').slice(0, 8);
    if (process.platform === 'win32') {
        const socketsDir = process.env.PLAYWRIGHT_SOCKETS_DIR;
        const suffix = socketsDir ? `-${calculateSha1(socketsDir).slice(0, 8)}` : '';
        return `\\\\.\\pipe\\pw-${userNameHash}-${domain}-${name}${suffix}`;
    }
    const baseDir = process.env.PLAYWRIGHT_SOCKETS_DIR || path.join(os.tmpdir(), `pw-${userNameHash}`);
    const dir = path.join(baseDir, domain);
    const result = path.join(dir, `${name}.sock`);
    fs.mkdirSync(dir, { recursive: true });
    return result;
}
//# sourceMappingURL=fileUtils.js.map