mono/packages/fs/src/interfaces.ts
2025-01-22 20:49:04 +01:00

528 lines
11 KiB
TypeScript

/////////////////////////////////////////////////////////
//
// Enums
//
export enum ENodeType {
FILE = 'file',
DIR = 'dir',
SYMLINK = 'symlink',
OTHER = 'other',
BLOCK = 'block'
}
/**
* Native errors.
* @todo : replace with errno.
*/
export let EError: any = {
NONE: 'None',
EXISTS: 'EEXIST',
PERMISSION: 'EACCES',
NOEXISTS: 'ENOENT',
CROSS_DEVICE: 'EXDEV'
};
/////////////////////////////////////////////////////////
//
// Data structures
//
export interface INode {
name: string
type: ENodeType | string
size: number
accessTime?: Date
modifyTime?: Date
changeTime?: Date
birthTime?: Date
absolutePath?: string
mode?: number
pointsAt?: string
relativePath?: string
children: INode[]
total?: number
checksum?: string
mime?: string
}
/**
* The options for "inspect".
*
* @export
* @interface IInspectOptions
*/
export interface IInspectOptions {
checksum?: string
mode?: boolean
times?: boolean
absolutePath?: boolean
symlinks?: boolean | string
size?: boolean
mime?: boolean
}
export interface INodeReport {
node: IProcessingNode
error: string
resolved: IConflictSettings
}
/**
* The accepted types for write and read as union.
*/
export type ReadWriteDataType = string | Buffer | object;
/**
* An extended version of Error to make typescript happy. This has been copied from
* the official Node typings.
*
* @export
* @class ErrnoException
* @extends {Error}
*/
export class ErrnoException extends Error {
public errno?: number
public code?: string
public path?: string
public syscall?: string
public stack?: string
}
/**
* Structure for file operations.
*/
export interface IProcessingNode {
path: string
item: INode
status?: ENodeOperationStatus
dst?: string
}
/**
* Basic flags during a file operation.
*
* @export
* @enum {number}
*/
export enum EBaseFlags {
/**
* When copying, don't copy symlinks but resolve them instead.
*/
FOLLOW_SYMLINKS = 8
}
/**
* Flags to determine certain properties during inspection.
*
* @export
* @enum {number}
*/
export enum EInspectFlags {
MODE = 2,
TIMES = 4,
SYMLINKS = 8,
FILE_SIZE = 16,
DIRECTORY_SIZE = 32,
CHECKSUM = 64,
MIME = 128
}
/**
* Basic options for file operations: used by cp, mv, rename and rm.
*
* @export
* @interface IBaseOptions
*/
export interface IBaseOptions {
/**
* Array of glob minimatch patterns
*
* @type {string[]}
* @memberOf IBaseOptions
*/
matching?: string[]
/**
* A function called to reject or accept nodes. This is used only when matching
* has been left empty.
* @memberOf IBaseOptions
*/
filter?: (from: string) => boolean
/**
* Flags to determine properties per node
*
* @type {EInspectFlags}
* @memberOf IBaseOptions
*/
flags?: EInspectFlags
}
/////////////////////////////////////////////////////////
//
// File operations : copy
//
/**
* Callback prototype signature when an item has been copied.
* This is used to abort the copy process when returning false.
*
* @param {string} path The path of the item
* @param {number} current The current index of the item
* @param {number} total The total of all items
* @param {INode} [item] The node data for the item
* @param {string} [item] The destination path
* @returns {boolean}
*/
export type ItemProgressCallback = (path: string, current: number, total: number, item?: INode, dst?:string) => boolean
/**
* Callback prototype signature when an item conflict occurs.
* It's async since the conflict might be resolved in an client application and hence
* we have to wait til the user decided.
*
* This is not being called if:
* - a previous callback returned with IConflictSettings#mode == ALWAYS
* - the options object already contains pre-defined conflict settings.
*
* @param {string} path The path of the item.
* @param {INode} item The node data.
* @param {string} err The native error code of the conflict (EEXIST,...)
* @returns {Promise<IConflictSettings>}
*/
export type ResolveConflictCallback = (path: string, item: INode, err: string) => Promise<IConflictSettings>
/**
* Callback prototype signature when an item is written during a copy.
**/
export type ContentCallback = (path: string, data: Buffer, item: INode) => Buffer
/**
* Callback prototype signature to rename an item when copied
*
* @param {string} from The source path.
* @param {string} from The target path.
* @returns {string|null}
*/
export type RenameCallback = (from: string, to: string) => string | null
/**
* Callback prototype signature when a file with at least 5MB size is being copied.
*
* @param {string} path The path of the item.
* @param {number} current The current copied bytes.
* @param {number} total The total size in bytes.
* @returns {Promise<IConflictSettings>}
*/
export type WriteProgressCallback = (path: string, current: number, total: number) => void
/**
* Status of a node operation.
*
* @export
* @enum {number}
*/
export enum ENodeOperationStatus {
// Node has been collected
COLLECTED,
// Node has been checked for existence
CHECKED,
// Node is in progress, before copy
PROCESSING,
// Node is in process
PROCESS,
// Node is in conflict, and user is being asked what to do
ASKING,
// Node conflict has been resolved by user
ANSWERED,
// Node has been copied
DONE
}
/**
* The possible modes to resolve a conflict during copy and move.
*
* @export
* @enum {number}
*/
export enum EResolveMode {
SKIP = 0,
OVERWRITE,
IF_NEWER,
IF_SIZE_DIFFERS,
APPEND,
THROW,
RETRY,
ABORT
}
/**
* Additional flags for copy
*
* @export
* @enum {number}
*/
export enum ECopyFlags {
/**
* Transfer atime and mtime of source to target
*/
NONE = 0,
/**
* Transfer atime and mtime of source to target
*/
PRESERVE_TIMES = 2,
/**
* Empty the target folder
*/
EMPTY = 4,
/**
* When copying, don't copy symlinks but resolve them instead.
*/
FOLLOW_SYMLINKS = 8,
/**
* Collect errors & success
*/
REPORT = 16
}
export interface IStatOptions {
/**
* Array of glob minimatch patterns
*
* @type {string[]}
* @memberOf ICopyOptions
*/
matching?: string[]
/**
* A function called to reject or accept nodes to be copied. This is used only when matching
* has been left empty.
* @memberOf ICopyOptions
*/
filter?: (from: string) => boolean
/**
* A progress callback for any copied item. Only executed in async.
*/
progress?: ItemProgressCallback
}
/**
* Copy options
*
* @export
* @interface ICopyOptions
*/
export interface ICopyOptions {
/**
* @type {boolean}
* @memberOf ICopyOptions
*/
overwrite?: boolean
/**
* Array of glob minimatch patterns
*
* @type {string[]}
* @memberOf ICopyOptions
*/
matching?: string[]
/**
* A function called to reject or accept nodes to be copied. This is used only when matching
* has been left empty.
* @memberOf ICopyOptions
*/
filter?: (from: string) => boolean
/**
* A progress callback for any copied item. Only executed in async.
*/
progress?: ItemProgressCallback
/**
* A progress function called for async and larger files only.
*
* @type {WriteProgressCallback}
* @memberOf ICopyOptions
*/
writeProgress?: WriteProgressCallback
/**
* Callback function when writing source content
*/
content?: ContentCallback
/**
* A callback when a conflict or error occurs. This is being called only if the user
* didn't provide conflictSettings.
*
* @type {ResolveConflictCallback}
* @memberOf ICopyOptions
*/
conflictCallback?: ResolveConflictCallback
/**
* A callback to rename nodes during copy
* @type {ResolveConflictCallback}
* @memberOf ICopyOptions
*/
renameCallback?: RenameCallback
/**
* Ability to set conflict resolver settings in advance, so that no callback will be called.
*
* @type {IConflictSettings}
* @memberOf ICopyOptions
*/
conflictSettings?: IConflictSettings
/**
* Throttel copy for larger files. This will be only used when writeProgress is set and the file is at least 5MB.
*
* @type {number}
* @memberOf ICopyOptions
*/
throttel?: number
/**
* Print some debug messages.
*
* @type {boolean}
* @memberOf ICopyOptions
*/
debug?: boolean
/**
* The copy flags.
*
* @type {ECopyFlags}
* @memberOf ICopyOptions
*/
flags?: ECopyFlags
}
/**
* An enumeration to narrow a conflict resolve to a single item or for all following conflicts.
*
* @export
* @enum {number}
*/
export enum EResolve {
/**
* Always will use the chose conflict settings for all following conflicts.
*/
ALWAYS,
/**
* 'This' will use the conflict settings for a single conflict so the conflict callback will be triggered again for the next conflict.
*/
THIS
}
/**
* A composite conflict settings and it's scope. This is the result type
* for the conflict callback.
*
* @export
* @interface IConflictSettings
*/
export interface IConflictSettings {
/**
* How to resolve this conflict/error.
*
* @type {EResolveMode}
* @memberOf IConflictSettings
*/
overwrite: EResolveMode;
/**
* The scope of this conflict resolver: always or this.
*
* @type {EResolve}
* @memberOf IConflictSettings
*/
mode: EResolve;
/**
* Track the origin error type for this settings.
*
* @type {string}
* @memberOf IConflictSettings
*/
error?: string;
}
export type TCopyResult = void | INodeReport[];
/////////////////////////////////////////////////////////
//
// File operations : write
//
/**
* fs/write options.
*
* @export
* @interface IWriteOptions
*/
export interface IWriteOptions {
atomic?: boolean;
jsonIndent?: number;
mode?: string;
}
/////////////////////////////////////////////////////////
//
// File operations : remove
//
export type TDeleteResult = void | INodeReport[];
/**
* Additional flags for delete
*
* @export
* @enum {number}
*/
export enum EDeleteFlags {
REPORT = 16
}
/**
* Delete options
*
* @export
* @interface IDeleteOptions
*/
export interface IDeleteOptions {
/**
* Array of glob minimatch patterns
*
* @type {string[]}
* @memberOf IDeleteOptions
*/
matching?: string[];
/**
* A callback when a conflict or error occurs. This is being called only if the user
* didn't provide conflictSettings.
*
* @type {ResolveConflictCallback}
* @memberOf IDeleteOptions
*/
conflictCallback?: ResolveConflictCallback;
/**
* Ability to set conflict resolver settings in advance, so that no callback will be called.
*
* @type {IConflictSettings}
* @memberOf IDeleteOptions
*/
conflictSettings?: IConflictSettings;
/**
*
* A progress callback for any deleted item. Only excecuted in async.
* @type {ItemProgressCallback}
* @memberOf IDeleteOptions
*/
progress?: ItemProgressCallback;
/**
* Print some messages.
*
* @type {boolean}
* @memberOf IDeleteOptions
*/
debug?: boolean;
/**
* A function called to reject or accept nodes to be copied. This is used only when matching
* has been left empty.
* @memberOf IDeleteOptions
*/
filter?: (from: string) => boolean;
flags?: EDeleteFlags;
}