import {Directory, DirectoryContent, File, ShallowDirectory, SimpleStats} from './model'; export interface FileSystemEvent { type: keyof Events; fullPath: string; correlation?: Correlation; } export interface UnexpectedErrorEvent extends FileSystemEvent { type: 'unexpectedError'; stack?: string; } export interface FileCreatedEvent extends FileSystemEvent { type: 'fileCreated'; newContent: string; } export interface FileChangedEvent extends FileSystemEvent { type: 'fileChanged'; newContent: string; } export interface FileDeletedEvent extends FileSystemEvent { type: 'fileDeleted'; } export interface DirectoryCreatedEvent extends FileSystemEvent { type: 'directoryCreated'; } export interface DirectoryDeletedEvent extends FileSystemEvent { type: 'directoryDeleted'; } export interface Disposable { dispose(): void; } export function isDisposable(fs: any): fs is Disposable { return !!fs && typeof fs.dispose === 'function'; } export type ListenerFn = (event: T) => any; export type Events = { unexpectedError: UnexpectedErrorEvent; fileCreated: FileCreatedEvent; fileChanged: FileChangedEvent; fileDeleted: FileDeletedEvent; directoryCreated: DirectoryCreatedEvent; directoryDeleted: DirectoryDeletedEvent; }; export const fileSystemEventNames: Array = ['unexpectedError', 'fileCreated', 'fileChanged', 'fileDeleted', 'directoryCreated', 'directoryDeleted']; export const fileSystemAsyncMethods: Array = ['saveFile', 'deleteFile', 'deleteDirectory', 'loadTextFile', 'loadDirectoryTree', 'ensureDirectory', 'loadDirectoryChildren', 'stat']; export type Correlation = string; export interface EventEmitter { listeners(event: S, exists: boolean): Array> | boolean; listeners(event: S): Array>; on(event: S, fn: ListenerFn, context?: any): this; addListener(event: S, fn: ListenerFn, context?: any): this; once(event: S, fn: ListenerFn, context?: any): this; removeListener(event: S, fn?: ListenerFn, context?: any, once?: boolean): this; removeAllListeners(event: S): this; off(event: S, fn?: ListenerFn, context?: any, once?: boolean): this; eventNames(): Array; } export interface FileSystem { readonly events: EventEmitter; readonly baseUrl: string; saveFile(fullPath: string, newContent: string, correlation?: Correlation): Promise; deleteFile(fullPath: string, correlation?: Correlation): Promise; deleteDirectory(fullPath: string, recursive?: boolean, correlation?: Correlation): Promise; ensureDirectory(fullPath: string, correlation?: Correlation): Promise; loadTextFile(fullPath: string): Promise; loadDirectoryTree(fullPath?: string): Promise; loadDirectoryChildren(fullPath: string): Promise<(File | ShallowDirectory)[]>; stat(fullPath: string): Promise; } export interface FileSystemReadSync extends FileSystem { loadTextFileSync(fullPath: string): string; loadDirectoryTreeSync(fullPath?: string): Directory; loadDirectoryContentSync(fullPath?: string): DirectoryContent; loadDirectoryChildrenSync(fullPath: string): Array; statSync(fullPath: string): SimpleStats; } export function isFileSystemReadSync(fs: FileSystem): fs is FileSystemReadSync { return typeof (fs as any).loadTextFileSync === 'function' && typeof (fs as any).loadDirectoryTreeSync === 'function' && typeof (fs as any).loadDirectoryContentSync === 'function' && typeof (fs as any).loadDirectoryChildrenSync === 'function' && typeof (fs as any).statSync === 'function'; }