@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
84 lines (82 loc) • 3.42 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
const require_exists = require('./exists.cjs');
let node_fs = require("node:fs");
let node_fs_promises = require("node:fs/promises");
let nanotar = require("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 (require_exists.existsSync(path)) return;
return (0, node_fs.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 require_exists.exists(path)) return;
return (0, node_fs_promises.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 (!require_exists.existsSync(path)) return;
return (0, node_fs.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 (!require_exists.existsSync(path)) return;
return (0, node_fs_promises.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 = (0, nanotar.parseTar)(await (0, node_fs_promises.readFile)(tarballPath)).find((e) => e.name === file);
if (!entry?.data) return;
if (!await require_exists.exists(destinationFilePath)) await (0, node_fs_promises.mkdir)(destinationFilePath, { recursive: true });
(0, node_fs.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 (0, nanotar.parseTarGzip)(await (0, node_fs_promises.readFile)(tarballPath))).find((e) => e.name === file);
if (!entry?.data) return;
if (!await require_exists.exists(destinationFilePath)) await (0, node_fs_promises.mkdir)(destinationFilePath, { recursive: true });
(0, node_fs.createWriteStream)(destinationFilePath).write(entry.data);
}
//#endregion
exports.createDirectory = createDirectory;
exports.createDirectorySync = createDirectorySync;
exports.extractFileFromTar = extractFileFromTar;
exports.extractFileFromTarGzip = extractFileFromTarGzip;
exports.removeDirectory = removeDirectory;
exports.removeDirectorySync = removeDirectorySync;