@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
55 lines (54 loc) • 1.99 kB
TypeScript
import { type Dirent } from 'node:fs';
import { type Promisable } from 'type-fest';
/**
* Test if the error is a NodeJS errno exception.
*
* @param error The value to check
* @param code If specified, check if the code matches
* @returns Whether or not the error is a NodeJS errno exception.
*/
export declare function isErrno(error: unknown, code?: string): error is NodeJS.ErrnoException;
/**
* Read the content of a JSON or YAML file.
*
* @param path The path to the file to read.
* @returns A tuple of the parsed content and the content as a string.
*/
export declare function readData<R>(path: URL | string): Promise<[R, string]>;
interface WriteDataOptions {
/**
* If false, don’t sort the object keys.
*/
readonly sort?: boolean;
/**
* A comparison function to use for sorting keys.
*
* By default natural sorting will be used.
*/
readonly compare?: ((a: string, b: string) => number) | null;
}
/**
* Write data to a file serialized as JSON or YAML.
*
* If `prettier` is available, the content is also formatted.
*
* @param path The file path to write the data to.
* @param data The data to write to the file.
* @param options Additional options for processing the data.
* @returns The formatted content.
*/
export declare function writeData(path: string, data: unknown, { compare, sort }?: WriteDataOptions): Promise<string>;
interface OpenDirSafeOptions {
allowMissing?: boolean;
recursive?: boolean;
}
/**
* Read the contents of a directory.
*
* @param directory The path of the directory to open.
* @param onFile A callback which will get called for every file. This will be called with the full
* file path as its first argument and its `Dirent` object as the second argument.
* @param options Additional options.
*/
export declare function opendirSafe(directory: string, onFile: (fullpath: string, stat: Dirent) => Promisable<void>, options?: OpenDirSafeOptions): Promise<void>;
export {};