tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
156 lines • 5.42 kB
text/typescript
/**
* Reads and parses a JSON file.
* Throws an error if the file content is not valid JSON.
* @param {string} filePath
* @returns {any}
*/
export function readJsonFile(filePath: string): any;
/**
* Saves an object as JSON to a file.
* Automatically creates the directory if it does not exist.
* @param {string} filePath
* @param {any} data
* @param {number} [spaces=2]
*/
export function writeJsonFile(filePath: string, data: any, spaces?: number): void;
/**
* Ensures that the directory exists, creating it recursively if needed.
* @param {string} dirPath
*/
export function ensureDirectory(dirPath: string): void;
/**
* Clears all contents inside a directory but keeps the directory.
* @param {string} dirPath
*/
export function clearDirectory(dirPath: string): void;
/**
* Checks whether a file exists.
* @param {string} filePath
* @returns {boolean}
*/
export function fileExists(filePath: string): boolean;
/**
* Checks whether a directory exists.
* @param {string} dirPath
* @returns {boolean}
*/
export function dirExists(dirPath: string): boolean;
/**
* Checks whether a directory is empty.
* @param {string} dirPath
* @returns {boolean}
*/
export function isDirEmpty(dirPath: string): boolean;
/**
* Copies a file to a destination.
* @param {string} src
* @param {string} dest
* @param {number} [mode]
*/
export function ensureCopyFile(src: string, dest: string, mode?: number): void;
/**
* Deletes a file (If the file exists).
* @param {string} filePath
* @returns {boolean}
*/
export function tryDeleteFile(filePath: string): boolean;
/**
* Writes text to a file (Ensures that the directory exists, creating it recursively if needed).
* @param {string} filePath
* @param {string} content
* @param {import('fs').WriteFileOptions} [ops='utf-8']
*/
export function writeTextFile(filePath: string, content: string, ops?: import("fs").WriteFileOptions): void;
/**
* Lists all files and dirs in a directory (optionally recursive).
* @param {string} dirPath
* @param {boolean} [recursive=false]
* @returns {{ files: string[]; dirs: string[] }}
*/
export function listFiles(dirPath: string, recursive?: boolean): {
files: string[];
dirs: string[];
};
/**
* Lists all directories in a directory (optionally recursive).
* @param {string} dirPath
* @param {boolean} [recursive=false]
* @returns {string[]}
*/
export function listDirs(dirPath: string, recursive?: boolean): string[];
/**
* Returns the size of a file in bytes.
* @param {string} filePath
* @returns {number}
*/
export function fileSize(filePath: string): number;
/**
* Returns the total size of a directory in bytes.
* @param {string} dirPath
* @returns {number}
*/
export function dirSize(dirPath: string): number;
/**
* Creates a backup copy of a file with .bak timestamp suffix.
* @param {string} filePath
* @param {string} [ext='bak']
*/
export function backupFile(filePath: string, ext?: string): void;
/**
* Returns the most recent backup file path for a given file.
* @param {string} filePath
* @param {string} [ext='bak']
* @returns {string} Full path to the most recent backup
*/
export function getLatestBackupPath(filePath: string, ext?: string): string;
/**
* Restores the most recent backup of a file.
* @param {string} filePath
* @param {string} [ext='bak']
*/
export function restoreLatestBackup(filePath: string, ext?: string): void;
/**
* Renames multiple files in a directory using a rename function.
* @param {string} dirPath - The target directory.
* @param {(original: string, index: number) => string} renameFn - Function that returns new filename.
* @param {string[]} [extensions] - Optional: Only rename files with these extensions.
*
* @throws {TypeError} If any argument has an invalid type.
* @throws {Error} If the directory does not exist or contains invalid files.
*/
export function renameFileBatch(dirPath: string, renameFn: (original: string, index: number) => string, extensions?: string[]): void;
/**
* Renames files using regex replacement.
* @param {string} dirPath
* @param {RegExp} pattern - Regex to match in the filename.
* @param {string} replacement - Replacement string.
* @param {string[]} [extensions]
*/
export function renameFileRegex(dirPath: string, pattern: RegExp, replacement: string, extensions?: string[]): void;
/**
* Adds a prefix or suffix to filenames.
* @param {string} dirPath
* @param {{ prefix?: string, suffix?: string }} options
* @param {string[]} [extensions]
*/
export function renameFileAddPrefixSuffix(dirPath: string, { prefix, suffix }: {
prefix?: string;
suffix?: string;
}, extensions?: string[]): void;
/**
* Normalizes all filenames to lowercase (or uppercase).
* @param {string} dirPath
* @param {'lower' | 'upper' | 'title'} mode
* @param {string[]} [extensions]
* @param {boolean} [normalizeExt=false] - Whether to apply case change to file extensions too.
* @throws {Error} If mode is invalid.
*/
export function renameFileNormalizeCase(dirPath: string, mode?: "lower" | "upper" | "title", extensions?: string[], normalizeExt?: boolean): void;
/**
* Pads numbers in filenames (e.g., "img1.jpg" -> "img001.jpg").
* @param {string} dirPath
* @param {number} totalDigits
* @param {string[]} [extensions]
*/
export function renameFilePadNumbers(dirPath: string, totalDigits?: number, extensions?: string[]): void;
//# sourceMappingURL=normalFuncs.d.mts.map