UNPKG

@push.rocks/smartfile

Version:

High-level file representation classes (SmartFile, StreamFile, VirtualDirectory) for efficient in-memory file management in Node.js using TypeScript. Works seamlessly with @push.rocks/smartfs for filesystem operations.

130 lines (129 loc) 3.91 kB
import * as plugins from './plugins.js'; export interface ISmartfileConstructorOptions { path: string; contentBuffer: Buffer; base: string; } /** * an vinyl file compatible in memory file class * Use SmartFileFactory to create instances of this class */ export declare class SmartFile extends plugins.smartjson.Smartjson { /** * Reference to the SmartFs instance for filesystem operations */ private smartFs?; /** * the relative path of the file */ accessor path: string; /** * a parsed path */ get parsedPath(): plugins.path.ParsedPath; get absolutePath(): string; get absoluteParsedPath(): plugins.path.ParsedPath; /** * the content of the file as Buffer */ accessor contentBuffer: Buffer; /** * The current working directory of the file * Note:this is similar to gulp and different from native node path base */ accessor base: string; /** * sync the file with disk */ accessor sync: boolean; /** * the constructor of Smartfile * @param optionsArg * @param smartFs optional SmartFs instance for filesystem operations */ constructor(optionsArg?: ISmartfileConstructorOptions, smartFs?: any); /** * set contents from string * @param contentString */ setContentsFromString(contentString: string, encodingArg?: 'utf8' | 'binary'): void; /** * write file to disk at its original location * Behaviours: * - no argument write to exactly where the file was picked up * - Requires SmartFs instance (create via SmartFileFactory) */ write(): Promise<void>; /** * writes the file to path given as argument * note: if the path is not absolute, takes process.cwd() as base * @param filePathArg */ writeToDiskAtPath(filePathArg: string): Promise<void>; /** * writes the file to a directory combined with the relative path portion * @param dirPathArg * @returns */ writeToDir(dirPathArg: string): Promise<string>; /** * read file from disk */ read(): Promise<void>; /** * deletes the file from disk at its original location */ delete(): Promise<void>; /** * Renames the file to the specified new name. * - Updates the `path` property with the new name. * - Writes the file to the new location if it exists on disk. * @param newName The new name of the file (including extension if applicable). * @param writeToDisk (optional) If true, also renames the file on the disk. * @returns The updated file path after renaming. */ rename(newName: string, writeToDisk?: boolean): Promise<string>; /** * vinyl-compatibility: alias of this.contentBuffer */ get contents(): Buffer; set contents(contentsArg: Buffer); /** * vinyl-compatibility */ get cwd(): string; /** * return relative path of file */ get relative(): string; /** * return truw when the file has content */ isNull(): boolean; /** * return true if contents are Buffer */ isBuffer(): boolean; isDirectory(): boolean; isStream(): boolean; isSymbolic(): boolean; getHash(typeArg?: 'path' | 'content' | 'all'): Promise<string>; updateFileName(fileNameArg: string): void; editContentAsString(editFuncArg: (fileStringArg: string) => Promise<string>): Promise<void>; /** * Returns a ReadableStream from the file's content buffer */ getStream(): plugins.stream.Readable; /** * Returns the size of the file in bytes */ getSize(): Promise<number>; /** * Parse content as string with specified encoding */ parseContentAsString(encodingArg?: BufferEncoding): string; /** * Parse content as buffer */ parseContentAsBuffer(): Buffer; }