fs-extender
Version:
Extras suite for node fs module
198 lines (197 loc) • 5.89 kB
TypeScript
/// <reference types="node" />
import * as fs from "./patch";
/**
* Test whether or not the given path exists by checking with the file system.
* Then call the `callback` argument with either true or false:
*
* ```js
* import { exists } from 'fs-extender';
*
* exists('/etc/passwd', (err, e) => {
* console.log(e ? 'it exists' : 'no passwd!');
* });
* ```
* This method normalizes the functioning of the exists function of Node.js, returning an error as the first argument of callback
*
* Using `fs.exists()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
* so introduces a race condition, since other processes may change the file's
* state between the two calls. Instead, user code should open/read/write the
* file directly and handle the error raised if the file does not exist.
*
* **write (NOT RECOMMENDED)**
*
* ```js
* import { exists, open, close } from 'fs-extender';
*
* exists('myfile', (err, e) => {
* if (e) {
* console.error('myfile already exists');
* } else {
* open('myfile', 'wx', (err, fd) => {
* if (err) throw err;
*
* try {
* writeMyData(fd);
* } finally {
* close(fd, (err) => {
* if (err) throw err;
* });
* }
* });
* }
* });
* ```
*
* **write (RECOMMENDED)**
*
* ```js
* import { open, close, lstatSync } from 'fs-extender';
* open('myfile', 'wx', (err, fd) => {
* if (err) {
* if (err.code === 'EEXIST') {
* console.error('myfile already exists');
* return;
* }
*
* throw err;
* }
*
* try {
* writeMyData(fd);
* } finally {
* close(fd, (err) => {
* if (err) throw err;
* });
* }
* });
* ```
*
* **read (NOT RECOMMENDED)**
*
* ```js
* import { open, close, exists } from 'fs-extender';
*
* exists('myfile', (err, e) => {
* if (e) {
* open('myfile', 'r', (err, fd) => {
* if (err) throw err;
*
* try {
* readMyData(fd);
* } finally {
* close(fd, (err) => {
* if (err) throw err;
* });
* }
* });
* } else {
* console.error('myfile does not exist');
* }
* });
* ```
*
* **read (RECOMMENDED)**
*
* ```js
* import { open, close } from 'fs-extender';
*
* open('myfile', 'r', (err, fd) => {
* if (err) {
* if (err.code === 'ENOENT') {
* console.error('myfile does not exist');
* return;
* }
*
* throw err;
* }
*
* try {
* readMyData(fd);
* } finally {
* close(fd, (err) => {
* if (err) throw err;
* });
* }
* });
* ```
*
* The "not recommended" examples above check for existence and then use the
* file; the "recommended" examples are better because they use the file directly
* and handle the error, if any.
*
* In general, check for the existence of a file only if the file won’t be
* used directly, for example when its existence is a signal from another
* process.
*/
export declare function exists(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, exists: boolean) => void): void;
/**
* Check if it is possible to access the `path` object
* `callback` gets 2 arguments `(err, exist:boolean)`
* @param path fs.PathLike
* @param mode
* @param callback
*/
export declare function existAccess(path: fs.PathLike, mode: number | undefined, callback: (err: NodeJS.ErrnoException | null, result: boolean) => void): void;
export declare function existAccess(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, result: boolean) => void): void;
/**
* Check if it is possible to access the `path` object
* @param path
* @param mode
* @returns `boolean`
*/
export declare function existAccessSync(path: fs.PathLike, mode?: number | undefined): boolean;
/**
* Check if the item is a directory
* @param path
* @param callback
*/
export declare function statIsDirectory(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, isType: boolean, stats: fs.Stats | fs.BigIntStats) => void): void;
/**
* Check if path is a directory
* @param path
* @returns
*/
export declare function statIsDirectorySync(path: fs.PathLike): boolean;
/**
* Check if path is a file
* @param path
* @param callback
*/
export declare function statIsFile(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, isType: boolean, stats: fs.Stats | fs.BigIntStats) => void): void;
/**
* Check if path is a file
* @param path
* @returns
*/
export declare function statIsFileSync(path: fs.PathLike): boolean;
/**
* Check if pat his a symbolik link
* @param path
* @param callback
*/
export declare function statIsSymbolicLink(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, isType: boolean, stats: fs.Stats | fs.BigIntStats) => void): void;
/**
* Check if path is a symbolik link
* @param path
* @returns
*/
export declare function statIsSymbolicLinkSync(path: fs.PathLike): boolean;
/**
* Check if given path is empty, if it's a folder it will use
* `readdir` and check the number of returing items,
* if it's another thing it will return the `size === 0`.
* Will throw any error that happens while checking
*/
export declare function isEmpty(path: fs.PathLike, options: {
dereference?: boolean;
}, callback: (err: NodeJS.ErrnoException | null, empty: boolean) => void): void;
export declare function isEmpty(path: fs.PathLike, callback: (err: NodeJS.ErrnoException | null, empty: boolean) => void): void;
/**
* Check if given path is empty, if it's a folder it will use
* `readdir` and check the number of returing items,
* if it's another thing it will return the `size === 0`.
* Will throw any error that happens while checking
*/
export declare function isEmptySync(path: fs.PathLike, options?: {
dereference?: boolean;
}): boolean;