import * as os from 'os';
import * as path from 'path';

export function expandPath(filePath: string): string {
  if (!filePath) return filePath;
  // Resolve all instances of [home] and [tmp] using global regex
  return filePath
    .replace(/\[home\]/g, os.homedir())
    .replace(/\[tmp\]/g, os.tmpdir());
}

export function getHashedPath(cacheRoot: string, hash: string): string {
  const expandedRoot = expandPath(cacheRoot);
  const subdirectory = hash.substring(0, 2);
  return path.join(expandedRoot, subdirectory, `${hash}.file`);
}
