@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
78 lines (76 loc) • 2.92 kB
JavaScript
import { exists, existsSync as existsSync$1 } from "./exists.mjs";
import { createWriteStream, mkdirSync, rmSync } from "node:fs";
import { mkdir, readFile, rm } from "node:fs/promises";
import { parseTar, parseTarGzip } from "nanotar";
//#region src/helpers.ts
/**
* Create a directory if it does not exist.
*
* @param path - The directory path to check
* @returns An indicator specifying if the directory exists
*/
function createDirectorySync(path) {
if (existsSync$1(path)) return;
return mkdirSync(path, { recursive: true });
}
/**
* Create a directory if it does not exist.
*
* @param path - The directory path to check
* @returns An indicator specifying if the directory exists
*/
async function createDirectory(path) {
if (await exists(path)) return;
return mkdir(path, { recursive: true });
}
/**
* Remove a directory if it exists.
*
* @param path - The directory path to check
* @returns An indicator specifying if the directory exists
*/
function removeDirectorySync(path) {
if (!existsSync$1(path)) return;
return rmSync(path, { recursive: true });
}
/**
* Remove a directory if it exists.
*
* @param path - The directory path to check
* @returns An indicator specifying if the directory exists
*/
async function removeDirectory(path) {
if (!existsSync$1(path)) return;
return rm(path, { recursive: true });
}
/**
* Extracts a file from a given tarball to the specified destination.
*
* @param tarballPath - The path to the tarball from where the file should be extracted.
* @param file - The path to the file inside the tarball.
* @param destinationFilePath - The destination file path.
* @returns True if the file was extracted successfully, false otherwise.
*/
async function extractFileFromTar(tarballPath, file, destinationFilePath) {
const entry = parseTar(await readFile(tarballPath)).find((e) => e.name === file);
if (!entry?.data) return;
if (!await exists(destinationFilePath)) await mkdir(destinationFilePath, { recursive: true });
createWriteStream(destinationFilePath).write(entry.data);
}
/**
* Extracts a file from a given TarGzip to the specified destination.
*
* @param tarballPath - The path to the tarball from where the file should be extracted.
* @param file - The path to the file inside the tarball.
* @param destinationFilePath - The destination file path.
* @returns True if the file was extracted successfully, false otherwise.
*/
async function extractFileFromTarGzip(tarballPath, file, destinationFilePath) {
const entry = (await parseTarGzip(await readFile(tarballPath))).find((e) => e.name === file);
if (!entry?.data) return;
if (!await exists(destinationFilePath)) await mkdir(destinationFilePath, { recursive: true });
createWriteStream(destinationFilePath).write(entry.data);
}
//#endregion
export { createDirectory, createDirectorySync, extractFileFromTar, extractFileFromTarGzip, removeDirectory, removeDirectorySync };
//# sourceMappingURL=helpers.mjs.map