atma-io
Version:
File / Directory Classes
477 lines (458 loc) • 22.5 kB
TypeScript
// Generated by dts-bundle v0.7.3
// Dependencies for this module:
// ../atma-utils
// ../fs
declare module 'atma-io' {
import { glob_matchPath } from 'atma-io/util/glob';
import { Io } from 'atma-io/IIo';
import { File } from 'atma-io/File';
import { setSettings } from 'atma-io/ExportsSetts';
import { class_Uri } from 'atma-utils';
import { Directory } from 'atma-io/Directory';
import { FileSafe } from 'atma-io/FileSafe';
import { LockFile } from 'atma-io/transport/filesystem/safe/LockFile';
export const watcher: {
watch(path: string, options: {
recursive?: boolean;
}, callback: (path?: string) => any): void;
unwatch(path: string, callback?: Function): void;
};
export const glob: {
matchPath: typeof glob_matchPath;
readFiles(path: string): File[];
read(path: string): (File | Directory)[];
readAsync(path: string, cb?: (error: any, arr?: (File | Directory)[], dir?: Directory) => any): Promise<(File | Directory)[]>;
};
export { FileSafe, LockFile, Directory };
const _default: Io;
export default _default;
export { File };
export const env: {
settings: any;
cwd: string;
applicationDir: class_Uri;
currentDir: class_Uri;
tmpDir: class_Uri;
newLine: string;
getTmpPath(filename: string): string;
readonly appdataDir: any;
};
export const Uri: typeof class_Uri;
export const settings: typeof setSettings;
}
declare module 'atma-io/util/glob' {
export function glob_getCalculatedPath(path: any, glob: any): any;
export function glob_matchPath(pattern: any, path: any): boolean;
export function glob_parsePatterns(mix: string | RegExp | (string | RegExp)[], out?: GlobRegExp[]): GlobRegExp[];
export function glob_parseDirs(pattern: any): [number, number, string];
export function glob_toRegExp(glob: any): GlobRegExp;
/**
* [as dir] '/dev/*.js' -> '/dev/'
*/
export function glob_getStrictPath(path: any): any;
/**
* 'c:/dev/*.js' -> '*.js'
*/
export function glob_getRelativePath(path: any): any;
export class GlobRegExp extends RegExp {
depth: number;
rootCount: number;
root: string;
}
}
declare module 'atma-io/IIo' {
import { Watcher } from 'atma-io/Watcher';
import { Directory } from 'atma-io/Directory';
import { File } from 'atma-io/File';
import { Glob } from 'atma-io/ExportsGlob';
import { setSettings } from 'atma-io/ExportsSetts';
import { FileSafe } from 'atma-io/FileSafe';
import { LockFile } from 'atma-io/transport/filesystem/safe/LockFile';
import { class_Uri } from 'atma-utils';
export interface Io {
env: {
currentDir: class_Uri;
applicationDir: class_Uri;
appdataDir: class_Uri;
tmpDir: class_Uri;
newLine: string;
settings: any;
getTmpPath: (filename: string) => string;
};
watcher: typeof Watcher;
glob: typeof Glob;
settings: typeof setSettings;
File: typeof File;
LockFile: typeof LockFile;
FileSafe: typeof FileSafe;
Uri: typeof class_Uri;
Directory: typeof Directory;
}
}
declare module 'atma-io/File' {
import { Stats } from 'fs';
import { IDeferred } from 'atma-io/IDeferred';
import { FileFactory } from 'atma-io/FileFactory';
import { FileHooks, IFileMiddleware } from 'atma-io/FileHooks';
import { ITransport } from 'atma-io/transport/custom';
import { IFileCopyOpts, IFileSettings, IOperationOptions } from 'atma-io/interfaces/IFile';
import { class_Uri } from 'atma-utils';
export class File {
opts?: IFileSettings;
uri: class_Uri;
content: Buffer | string;
sourceMap?: string;
constructor(path: string | class_Uri, opts?: IFileSettings);
read<T = string | Buffer>(mix?: IOperationOptions): T;
static read<T = string | Buffer>(path: string, mix?: IFileSettings & IOperationOptions): T;
readAsync<T = string | Buffer>(mix?: IOperationOptions): Promise<T>;
static readAsync<T = string | Buffer>(path: string, mix?: IFileSettings & IOperationOptions): Promise<T>;
readRange<T = string>(position: number, length: number, mix?: IOperationOptions): T;
static readRange<T = string>(path: string, position: number, length: number, mix?: IOperationOptions): T;
readRangeAsync<T = string>(position: number, length: number, mix?: IOperationOptions): IDeferred<T>;
static readRangeAsync<T = string>(path: string, position: number, length: number, mix?: IOperationOptions): IDeferred<T>;
write<T = string | Buffer | any>(content: T, mix?: IOperationOptions): this;
static write<T = string | Buffer | any>(path: string, content: T, mix?: IFileSettings & IOperationOptions): File;
writeAsync<T = string | Buffer | any>(content: T, mix?: IOperationOptions): Promise<this>;
static writeAsync<T = string | Buffer | any>(path: string, content: T, mix?: IFileSettings & IOperationOptions): Promise<File>;
copyTo(target: string, opts?: IFileCopyOpts): this;
static copyTo(path: string, target: string, opts?: IFileCopyOpts): File;
copyToAsync(target: string, opts?: IFileCopyOpts): IDeferred<this>;
static copyToAsync(path: string, target: string, opts?: IFileCopyOpts): IDeferred<File>;
exists(): boolean;
static exists(path: string): boolean;
existsAsync(): PromiseLike<boolean>;
static existsAsync(path: string | class_Uri): PromiseLike<boolean>;
rename(fileName: string): boolean;
static rename(path: string, fileName: string): boolean;
renameAsync(filename: any): IDeferred<boolean>;
static renameAsync(path: string, fileName: string): IDeferred<boolean>;
append(str: string): boolean;
static append(path: string, str: string): boolean;
appendAsync(str: string): IDeferred<boolean>;
static appendAsync(path: string, str: string): IDeferred<boolean>;
remove(): boolean;
static remove(path: string): boolean;
removeAsync(): Promise<boolean>;
static removeAsync(path: string): Promise<boolean>;
replace(a: string | RegExp, b: string | ((substring: string, ...args: any[]) => string), setts?: any): string;
static replace(path: string, a: string | RegExp, b: string | ((substring: string, ...args: any[]) => string), setts?: any): string;
replaceAsync(a: string | RegExp, b: string | ((substring: string, ...args: any[]) => string), setts?: any): Promise<string>;
static replaceAsync(path: string, a: string | RegExp, b: string | ((substring: string, ...args: any[]) => string), setts?: any): Promise<string>;
watch(callback: (path?: string) => void | any): void;
static watch(path: string, callback: (path?: string) => void | any): void;
unwatch(callback?: any): void;
static unwatch(path: string, callback?: any): void;
stats(): Stats;
static stats(path: string): Stats;
static clearCache(mix?: any): void;
clearCache(): void;
static disableCache(): void;
static enableCache(): void;
static registerFactory(factory: FileFactory): void;
static getFactory(): FileFactory;
static registerHookHandler(hook: FileHooks): void;
static getHookHandler(): FileHooks;
static registerTransport(protocol: string, transport: ITransport): void;
static getTransports(): {
[protocol: string]: ITransport;
};
static setTransports(repository: any): void;
static get Factory(): FileFactory;
static get Middleware(): FileHooks;
static processHooks(method: any, file: any, config: any, onComplete?: any): Promise<void>;
static middleware: {
[name: string]: IFileMiddleware;
};
static registerExtensions(extensions: any, shouldCleanPrevious?: boolean, settings?: any): void;
static setMiddlewares(extensions: any, settings?: any): void;
}
}
declare module 'atma-io/ExportsSetts' {
export function setSettings(settings: {
extensions?: any;
}): void;
}
declare module 'atma-io/Directory' {
import { dir_symlink } from 'atma-io/transport/dir_transport';
import { File } from 'atma-io/File';
import { IDeferred } from 'atma-io/IDeferred';
import { class_Uri } from 'atma-utils';
export class Directory {
uri: class_Uri;
files: File[];
constructor(directory: string | Directory | class_Uri);
exists(): boolean;
static exists(path: string): boolean;
existsAsync(): IDeferred<boolean>;
static existsAsync(path: string): IDeferred<boolean>;
ensure(): this;
static ensure(path: string): Directory;
ensureAsync(): IDeferred<Directory>;
static ensureAsync(path: string): IDeferred<Directory>;
readFiles(pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): File[];
static readFiles(path: string, pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): File[];
read(pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): (File | Directory)[];
static read(path: string, pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): (File | Directory)[];
readFilesAsync(pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): IDeferred<File[]>;
static readFilesAsync(path: string, pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): IDeferred<File[]>;
readAsync(pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): IDeferred<(File | Directory)[]>;
static readAsync(path: string, pattern?: string | RegExp | (string | RegExp)[], exclude?: string | RegExp | (string | RegExp)[]): IDeferred<(File | Directory)[]>;
copyTo(target: string, options?: {
verbose?: boolean;
}): IDeferred<void>;
static copyTo(path: string, target: string, options?: {
verbose?: boolean;
}): IDeferred<void>;
copyToAsync(target: string, options?: {
verbose?: boolean;
}): IDeferred<void>;
static copyToAsync(path: string, target: string, options?: {
verbose?: boolean;
}): IDeferred<void>;
getName(): string;
rename(name: string): void;
static rename(path: string, name: string): void;
renameAsync(name: string): IDeferred<any>;
static renameAsync(path: string, name: string): IDeferred<any>;
remove(): void;
static remove(path: string): void;
removeAsync(): IDeferred<any>;
static removeAsync(path: string): IDeferred<void>;
watch(callback: (path?: string) => void | any): void;
static watch(path: string, callback: (path?: string) => void | any): void;
unwatch(callback?: (path: string) => void | any): void;
static unwatch(path: string, callback?: (path?: string) => void | any): void;
static symlink: typeof dir_symlink;
}
}
declare module 'atma-io/FileSafe' {
import { File } from 'atma-io/File';
import { IFileSettings } from 'atma-io/interfaces/IFile';
/**
* Safe cross process file writes and reads using *.bak files as the safe-fallback
* 1. parallel-writes within one process: use sequantual queue
* 2. process-crash when writing: use *.bak files
* 3. parallel-writes for multiple processes: use locks
*/
export class FileSafe extends File {
path: string;
opts?: IFileSettings;
constructor(path: string, opts?: IFileSettings);
}
}
declare module 'atma-io/transport/filesystem/safe/LockFile' {
export class LockFile {
path: string;
constructor(path: string);
acquire(): Promise<any>;
release(): Promise<void>;
}
}
declare module 'atma-io/Watcher' {
export const Watcher: {
watch(path: string, options: {
recursive?: boolean;
}, callback: (path?: string) => void | any): void;
unwatch(path: string, callback?: Function): void;
};
}
declare module 'atma-io/ExportsGlob' {
import { glob_matchPath } from 'atma-io/util/glob';
import { Directory } from 'atma-io/Directory';
import { File } from 'atma-io/File';
export const Glob: {
matchPath: typeof glob_matchPath;
readFiles(path: string): File[];
read(path: string): (File | Directory)[];
readAsync(path: string, cb?: (error: any, arr?: (File | Directory)[], dir?: Directory) => void | any): Promise<(File | Directory)[]>;
};
}
declare module 'atma-io/IDeferred' {
export interface IDeferred<T> extends PromiseLike<T> {
done(done: (...args: any[]) => void | IDeferred<any>): this;
fail(fail: (error: any | Error) => void): this;
reject(error: any | Error): this;
resolve(...args: any[]): this;
always(always: Function): this;
defer(): this;
isResolved(): boolean;
isRejected(): boolean;
isBusy(): boolean;
resolveDelegate(): (result: any) => void | any;
rejectDelegate(): (result: Error | any) => void | any;
}
}
declare module 'atma-io/FileFactory' {
export class FileFactory {
handlers: any[];
registerHandler(regexp: RegExp, handler: any): void;
unregisterHandler(regexp: any, handler: any): void;
resolveHandler(uri: any): any;
}
}
declare module 'atma-io/FileHooks' {
import { File } from 'atma-io/File';
export interface IFileMiddleware {
name?: string;
setOptions?(opts: any): void;
setIo?(io: any): void;
register?(io: any): void;
read?(file: File, config: any): any;
readAsync?(file: File, config: any, done: Function): any;
write?(file: File, config: any): any;
writeAsync?(file: File, config: any, done: Function): any;
}
export interface IHookFunction {
(file: File, config?: any): void | any;
}
export class HookRunner {
regexp: RegExp;
method: 'read' | 'write';
handler: IFileMiddleware | IHookFunction;
zIndex: number;
constructor(regexp: RegExp, method: 'read' | 'write', handler: IFileMiddleware | IHookFunction, zIndex: number);
run(method: 'read' | 'write', file: File, config?: any): void;
runAsync(method: any, file: any, config: any, done: any): void;
canHandle(path: string, method: 'read' | 'write'): boolean;
}
type THookDefinition = {
regexp: RegExp;
method: 'read' | 'write';
handler: THookHandler;
zIndex?: number;
};
type THookHandler = string | IFileMiddleware | IHookFunction;
export class FileHooks {
hooks: HookRunner[];
register(params: THookDefinition): this;
register(extension: string, method: 'read' | 'write', handler: THookHandler, zIndex?: number): this;
register(regexp: RegExp, method: 'read' | 'write', handler: THookHandler, zIndex?: number): this;
unregister(method: 'read' | 'write', handler: IFileMiddleware | string | IHookFunction): void;
unregisterByRegexp(regexp: RegExp): void;
trigger(method: 'read' | 'write', file: File, config?: any): void;
triggerAsync(method: 'read' | 'write', file: File, config: any, cb: Function): void;
clear(): this;
getHooksForPath(path: string, method: 'read' | 'write'): HookRunner[];
}
export {};
}
declare module 'atma-io/transport/custom' {
import { TCallback } from 'atma-io/util/types';
import { IFileOptionsBase, IOperationOptions } from 'atma-io/interfaces/IFile';
export interface IFileTransportV2 {
version: 2;
save(path: string, content: any, options?: IOperationOptions & IFileOptionsBase): void;
saveAsync(path: string, content: string | Buffer, options: IOperationOptions & IFileOptionsBase): Promise<void>;
copy(from: string, to: string): void;
copyAsync(from: string, to: string): Promise<void>;
exists(path: string): boolean;
existsAsync(path: any): Promise<boolean>;
read(path: string, encoding?: 'utf8' | string, options?: IOperationOptions & IFileOptionsBase): string | Buffer;
readAsync(path: string, encoding: 'utf8' | string, options?: IOperationOptions & IFileOptionsBase): Promise<string | Buffer>;
readRange(path: string, offset: number, limit: number, encoding?: 'utf8' | string): string | Buffer;
readRangeAsync(path: string, offset: number, limit: number, encoding: 'utf8' | string): Promise<string | Buffer>;
remove(path: string): boolean;
removeAsync(path: string): Promise<void>;
rename(path: string, filename: string): void;
renameAsync(path: string, filename: string): Promise<void>;
appendAsync?(path: string, str: string): Promise<void>;
append?(path: string, str: string): void;
}
export interface IFileTransport {
version?: void;
save(path: string, content: any, options?: any): void;
saveAsync(path: any, content: any, options: any, cb: any): void;
copy(from: any, to: any): any;
copyAsync(from: any, to: any, cb: TCallback): any;
exists(path: any): boolean;
existsAsync(path: any, cb: TCallback<boolean>): any;
read(path: any, encoding?: any): string | Buffer;
readAsync(path: any, encoding: any, cb: TCallback<string | Buffer>): any;
readRange(path: any, offset: any, limit: any, encoding?: any): string | Buffer;
readRangeAsync(path: any, offset: any, limit: any, encoding: any, cb: TCallback<string | Buffer>): any;
remove(path: any): boolean;
removeAsync(path: any, cb: TCallback): any;
rename(path: any, filename: any): any;
renameAsync(path: any, filename: any, cb: any): any;
appendAsync?(path: string, str: string, cb: any): any;
append?(path: string, str: string): any;
}
export interface IDirectoryTransport {
ensure(path: any): string;
ensureAsync(path: any, cb: any): void;
ceateSymlink(source: string, target: string): any;
exists(path: any): boolean;
existsAsync(path: any, cb: TCallback<boolean>): any;
readFiles(path: any, patterns?: any, excludes?: any, data?: any): string[];
readFilesAsync(path: any, patternsOrCb?: any, excludesOrCb?: any, dataOrCb?: any, Cb?: any): any;
remove(path: any): boolean;
removeAsync(path: any, cb: TCallback): any;
rename(oldPath: any, newPath: any): any;
renameAsync(oldPath: any, newPath: any, cb: TCallback): any;
}
export interface ITransport {
File: IFileTransport | IFileTransportV2;
Directory: IDirectoryTransport;
}
export const Repository: {
[protocol: string]: ITransport;
};
export class CustomTransport {
static register(protocol: string, transport: ITransport): void;
static get(protocol: string): ITransport;
static all(): {
[protocol: string]: ITransport;
};
static set(repository: any): void;
}
}
declare module 'atma-io/interfaces/IFile' {
import { FileHooks } from 'atma-io/FileHooks';
import { FileFactory } from 'atma-io/FileFactory';
export interface IFileOptionsBase {
/** Write files via *.bak files, to prevent data los */
processSafe?: boolean;
threadSafe?: boolean;
aes256?: {
secret: string | Buffer;
};
}
export interface IFileSettings extends IFileOptionsBase {
cached?: boolean;
factory?: FileFactory;
}
export interface IFileCopyOpts {
silent?: boolean;
baseSource?: string;
}
export interface IOperationOptions extends IFileOptionsBase {
skipHooks?: boolean;
/** Default: utf8 */
encoding?: BufferEncoding;
hooks?: FileHooks;
position?: number;
length?: number;
[other: string]: any;
}
}
declare module 'atma-io/transport/dir_transport' {
export function dir_ensure(path: any): string;
export function dir_ensureAsync(path: any, cb: any): void;
export function dir_exists(path: any): boolean;
export function dir_existsAsync(path: any, cb: any): void;
export function dir_files(path: any, patterns: any, excludes: any, data?: any): string[];
export function dir_filesAsync(path: any, patternsOrCb?: any, excludesOrCb?: any, dataOrCb?: any, Cb?: any): any;
export function dir_symlink(source: string, target: string): void;
export function dir_remove(path: any): boolean;
export function dir_removeAsync(path: any, cb: any): any;
export function dir_rename(oldPath: string, newPath: string): any;
export function dir_renameAsync(oldPath: string, newPath: string, cb: any): any;
}
declare module 'atma-io/util/types' {
export type TCallback<TResult = any> = (error: Error, result?: TResult) => void;
export type TFnWithCallback<TArgs extends any[], TResult> = (...args: [...TArgs, TCallback<TResult>]) => void;
export type TFnWithCallbackArgs<T> = T extends TFnWithCallback<infer TArgs, any> ? TArgs : never;
export type THead<T extends any[]> = T extends [...infer Head, any] ? Head : any[];
export type TLast<T extends any[]> = T extends [...any[], infer Last] ? Last : never;
}