UNPKG

@push.rocks/smartfile

Version:

Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.

131 lines (130 loc) 4.23 kB
import * as plugins from './plugins.js'; export interface ISmartfileConstructorOptions { path: string; contentBuffer: Buffer; base: string; } /** * an vinyl file compatible in memory file class */ export declare class SmartFile extends plugins.smartjson.Smartjson { /** * creates a Smartfile from a filePath * @param filePath */ static fromFilePath(filePath: string, baseArg?: string): Promise<SmartFile>; static fromBuffer(filePath: string, contentBufferArg: Buffer, baseArg?: string): Promise<SmartFile>; static fromString(filePath: string, contentStringArg: string, encodingArg: 'utf8' | 'binary', baseArg?: string): Promise<SmartFile>; static fromFoldedJson(foldedJsonArg: string): Promise<SmartFile>; /** * creates a Smartfile from a ReadableStream * @param stream a readable stream that provides file content * @param filePath the file path to associate with the content * @param baseArg the base path to use for the file */ static fromStream(stream: plugins.stream.Readable, filePath: string, baseArg?: string): Promise<SmartFile>; static fromUrl(urlArg: string): Promise<SmartFile>; /** * the relative path of the file */ 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 */ contentBuffer: Buffer; /** * The current working directory of the file * Note:this is similar to gulp and different from native node path base */ base: string; /** * sync the file with disk */ sync: boolean; /** * the constructor of Smartfile * @param optionsArg */ constructor(optionsArg: ISmartfileConstructorOptions); /** * 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 */ 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>; }