@sinclair/hammer
Version:
Build Tool for Browser and Node Applications
59 lines (58 loc) • 2.84 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import { SystemError } from './error';
import * as crypto from 'crypto';
export declare class FileError extends SystemError {
readonly operation: string;
readonly reason: string;
constructor(operation: string, reason: string);
}
export declare class File {
private readonly filePath;
constructor(filePath: string);
/** Appends this file with the given data. If the file does not exist it will be created. */
append(data: Buffer | string): Promise<void>;
/** Copies this file into the target folder. If the target folder does not exist it will be created. */
copy(folderPath: string): Promise<void>;
/** Creates an empty file if not exists. */
create(): Promise<void>;
/** Deletes this file if it exists. Otherwise no action. */
delete(): Promise<void>;
/** Replaces text content in this file that matches the given string or regular expression. */
edit(pattern: RegExp | string, replacement: string): Promise<void>;
/** Returns true if this file exists. */
exists(): Promise<boolean>;
/** Returns a hash for this file with the given algorithm (default is sha1, digest is hex) */
hash(algorithm?: string, digest?: crypto.BinaryToTextEncoding): Promise<string>;
/** Moves this file into the target folder. If the target folder does not exist it will be created. */
move(folderPath: string): Promise<void>;
/** Prepends this file with the given data. If the file does not exist it will be created. */
prepend(data: Buffer | string): Promise<void>;
/** Reads this file as a JSON object. Will throw if the content cannot be parsed. */
json<T = any>(): Promise<T>;
/** Returns the absolute path of this file. */
path(): string;
/** Reads this file as a buffer. */
read(): Promise<Buffer>;
/** Reads this file with the given encoding. */
read(encoding: BufferEncoding): Promise<string>;
/** Renames this file to the given newname. */
rename(newName: string): Promise<void>;
/** Returns the size of this file in bytes. */
size(): Promise<number>;
/** Returns the stats object for this file. */
stat(): Promise<import("fs").Stats>;
/** Truncates the contents of this file. If the file does not exist, it is created. */
truncate(length?: number): Promise<void>;
/** Writes to this file. If the file does not exist, it is created. */
write(data: string | Buffer): Promise<void>;
/** Asserts the given path exists. */
private assertExists;
/** Checks the given path exists. */
private checkExists;
/** Creates a directory if not exists. */
private createFolder;
}
/** Returns an interface to interact with a file. */
export declare function file(file: string): File;