All files / src/util fs.ts

75.6% Statements 31/41
100% Branches 7/7
75% Functions 3/4
75.6% Lines 31/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 421x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 3x 3x 3x 3x 2x 2x 2x 3x 1x 1x                     1x 1x 4x 4x 4x 4x 4x  
import { Logger } from "./log.js";
import fs from "node:fs";
import extract from "extract-zip";
import path from "node:path";
import { Maybe } from "purify-ts";
import { ensureError } from "./misc.js";
 
export function cleanDir(dir: string, log: Logger) {
  log.info(`Cleaning directory ${dir}`);
  deleteQuietly(dir, true, log);
  fs.mkdirSync(dir, { recursive: true });
}
 
export function deleteQuietly(path: string, recursive: boolean, log: Logger) {
  try {
    fs.rmSync(path, { force: true, recursive: recursive });
    log.info(`Deleted "${path}"`);
  } catch (e) {
    const error = ensureError(e);
    log.warn(`Could not delete "${path}". Reason: ${error}`);
  }
}
 
export async function unzipFileIntoDirectory(
  zipFile: string,
  destDir: string,
  deleteZip: boolean,
  log: Logger,
) {
  await extract(zipFile, { dir: destDir });
  if (deleteZip) {
    deleteQuietly(zipFile, false, log);
  }
}
 
export function resolveFile(...paths: string[]) {
  const file = path.resolve(...paths);
  return Maybe.encase(() => fs.statSync(file))
    .filter(stat => stat.isFile())
    .map(() => path.resolve(file));
}