import * as vscode from 'vscode' ; import { existsSync } from 'fs' ; /**
 * This class handles file changes.
 * Some entity can register for a certain file and is informed on any change of the file.
 * I.e. observed changes are:
 * - onDidCreate (file)
 * - onDidChange (file)
 * - onDidDelete (file)
 * When not needed anymore you need to call 'dispose()' on the file viewer to free it.
 * Compared to the vscode.FileSystemWatcher the FileWatcher class should be used on exact file names only.
 * Not on glob patterns.
 */ export class FileWatcher extends vscode . Disposable { protected watcher : vscode . FileSystemWatcher ; protected filePath : string ; protected fileExists : boolean ; protected funcCreate : ( fileName : string ) => void ; /**
     * Constructor.
     * Dispose if not required any more.
     */ constructor ( filePath ) { super ( ( ) => { this . watcher . dispose ( ) ; } ) ; this . filePath = filePath ; this . watcher = vscode . workspace . createFileSystemWatcher ( this . filePath ) ; this . fileExists = existsSync ( this . filePath ) ; } /**
     * When files are created this function is called.
     * And as well if the file exists at this moment.
     */ public onDidCreate ( func ) { this . funcCreate = func ; this . watcher . onDidCreate ( uri => { this . fileExists = true ; func ( this . filePath ) ; } ) ; if ( this . fileExists ) func ( this . filePath ) ; } /**
     * Calls 'func' on a change.
     * Unfortunately this is also called on a filename change.
     * Although it is better to handle this as a create.
     * Therefore it is tracked if the file exists or not.
     */ public onDidChange ( func ) { this . watcher . onDidChange ( uri => { if ( this . fileExists ) { func ( this . filePath ) ; } else { this . fileExists = true ; this . funcCreate ( this . filePath ) ; } } ) ; } /**
     * This is called on file deletion.
     * It is also called if the file is renamed.
     */ public onDidDelete ( func ) { this . watcher . onDidDelete ( uri => { this . fileExists = false ; func ( this . filePath ) ; } ) ; } }