<commit_before>import * as fs from 'fs-extra';
import { isAbsolute, join, parse } from 'upath';
import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';

export * from './proxies';

export function getSubDirectory(fileName: string): string {
  return parse(fileName).dir;
}

export function getSiblingFileName(
  existingFileNameWithPath: string,
  otherFileName: string
): string {
  const subDirectory = getSubDirectory(existingFileNameWithPath);
  return join(subDirectory, otherFileName);
}

export async function readLocalFile(fileName: string): Promise<Buffer>;
export async function readLocalFile(
  fileName: string,
  encoding: 'utf8'
): Promise<string>;
export async function readLocalFile(
  fileName: string,
  encoding?: string
): Promise<string | Buffer> {
  const { localDir } = getAdminConfig();
  const localFileName = join(localDir, fileName);
  try {
    const fileContent = await fs.readFile(localFileName, encoding);
    return fileContent;
  } catch (err) {
    logger.trace({ err }, 'Error reading local file');
    return null;
  }
}

export async function writeLocalFile(
  fileName: string,
  fileContent: string
): Promise<void> {
  const { localDir } = getAdminConfig();
  const localFileName = join(localDir, fileName);
  await fs.outputFile(localFileName, fileContent);
}

export async function deleteLocalFile(fileName: string): Promise<void> {
  const { localDir } = getAdminConfig();
  if (localDir) {
    const localFileName = join(localDir, fileName);
    await fs.remove(localFileName);
  }
}

// istanbul ignore next
export async function renameLocalFile(
  fromFile: string,
  toFile: string
): Promise<void> {
  const { localDir } = getAdminConfig();
  await fs.move(join(localDir, fromFile), join(localDir, toFile));
}

// istanbul ignore next
export async function ensureDir(dirName: string): Promise<void> {
  await fs.ensureDir(dirName);
}

// istanbul ignore next
export async function ensureLocalDir(dirName: string): Promise<void> {
  const { localDir } = getAdminConfig();
  const localDirName = join(localDir, dirName);
  await fs.ensureDir(localDirName);
}

export async function ensureCacheDir(
  dirName: string,
  envPathVar?: string
): Promise<string> {
  const { cacheDir } = getAdminConfig();
  const envCacheDirName = envPathVar ? process.env[envPathVar] : null;
  const cacheDirName = envCacheDirName || join(cacheDir, dirName);
  await fs.ensureDir(cacheDirName);
  return cacheDirName;
}

/**
 * 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.
 */<commit_msg>fix(fs): only ensureDir non-empty string (#10177)<commit_after>import is from '@sindresorhus/is';
import * as fs from 'fs-extra';
import { isAbsolute, join, parse } from 'upath';
import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';

export * from './proxies';

export function getSubDirectory(fileName: string): string {
  return parse(fileName).dir;
}

export function getSiblingFileName(
  existingFileNameWithPath: string,
  otherFileName: string
): string {
  const subDirectory = getSubDirectory(existingFileNameWithPath);
  return join(subDirectory, otherFileName);
}

export async function readLocalFile(fileName: string): Promise<Buffer>;
export async function readLocalFile(
  fileName: string,
  encoding: 'utf8'
): Promise<string>;
export async function readLocalFile(
  fileName: string,
  encoding?: string
): Promise<string | Buffer> {
  const { localDir } = getAdminConfig();
  const localFileName = join(localDir, fileName);
  try {
    const fileContent = await fs.readFile(localFileName, encoding);
    return fileContent;
  } catch (err) {
    logger.trace({ err }, 'Error reading local file');
    return null;
  }
}

export async function writeLocalFile(
  fileName: string,
  fileContent: string
): Promise<void> {
  const { localDir } = getAdminConfig();
  const localFileName = join(localDir, fileName);
  await fs.outputFile(localFileName, fileContent);
}

export async function deleteLocalFile(fileName: string): Promise<void> {
  const { localDir } = getAdminConfig();
  if (localDir) {
    const localFileName = join(localDir, fileName);
    await fs.remove(localFileName);
  }
}

// istanbul ignore next
export async function renameLocalFile(
  fromFile: string,
  toFile: string
): Promise<void> {
  const { localDir } = getAdminConfig();
  await fs.move(join(localDir, fromFile), join(localDir, toFile));
}

// istanbul ignore next
export async function ensureDir(dirName: string): Promise<void> {
  if (is.nonEmptyString(dirName)) {
    await fs.ensureDir(dirName);
  }
}

// istanbul ignore next
export async function ensureLocalDir(dirName: string): Promise<void> {
  const { localDir } = getAdminConfig();
  const localDirName = join(localDir, dirName);
  await fs.ensureDir(localDirName);
}

export async function ensureCacheDir(
  dirName: string,
  envPathVar?: string
): Promise<string> {
  const { cacheDir } = getAdminConfig();
  const envCacheDirName = envPathVar ? process.env[envPathVar] : null;
  const cacheDirName = envCacheDirName || join(cacheDir, dirName);
  await fs.ensureDir(cacheDirName);
  return cacheDirName;
}

/**
 * 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.
 */
{'fraction_non_alphanumeric': 0.11250672887134398, 'fraction_numerical': 0.001256055984209582, 'mean_word_length': 4.8735511064278185, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2056515', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1627, 'n_words': 485}