import { getTestResultsFolder } from '@salesforce/salesforcedx-utils-vscode/out/src/helpers' ; import * as fs from 'fs' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { lwcTestIndexer } from '../testIndexer' ; import { TestExecutionInfo } from '../types' ; /**
 * Test result watcher to watch for creating/updating test results,
 * and update test indexer.
 */ class TestResultsWatcher implements vscode . Disposable { private fileSystemWatchers = new Map < string , vscode . FileSystemWatcher > ( ) ; private disposables : vscode . Disposable [ ] = [ ] ; param context public register ( context ) { context . subscriptions . push ( this ) ; } param workspaceFolder param testExecutionInfo public getTempFolder ( workspaceFolder , testExecutionInfo ) { const { testType } = testExecutionInfo ; const workspaceFsPath = workspaceFolder . uri . fsPath ; return getTestResultsFolder ( workspaceFsPath , testType ) ; } param outputFilePath public watchTestResults ( outputFilePath ) { const outputFileFolder = path . dirname ( outputFilePath ) ; let fileSystemWatcher = this . fileSystemWatchers . get ( outputFileFolder ) ; if ( ! fileSystemWatcher ) { const outputFileExtname = path . extname ( outputFilePath ) ; const testResultsGlobPattern = path . join ( outputFileFolder , `*${ outputFileExtname }` ) . replace ( /\\/g , '/' ) ; fileSystemWatcher = vscode . workspace . createFileSystemWatcher ( testResultsGlobPattern ) ; fileSystemWatcher . onDidCreate ( testResultsUri => { this . updateTestResultsFromTestResultsJson ( testResultsUri ) ; } ) ; fileSystemWatcher . onDidChange ( testResultsUri => { this . updateTestResultsFromTestResultsJson ( testResultsUri ) ; } ) ; this . fileSystemWatchers . set ( outputFileFolder , fileSystemWatcher ) ; this . disposables . push ( fileSystemWatcher ) ; } } private updateTestResultsFromTestResultsJson ( testResultsUri ) { try { const { fsPath : testResultsFsPath } = testResultsUri ; const testResultsJSON = fs . readFileSync ( testResultsFsPath , { encoding : 'utf8' } ) ; const testResults = JSON . parse ( testResultsJSON ) ; lwcTestIndexer . updateTestResults ( testResults ) ; } catch ( error ) { console . error ( error ) ; } } public dispose ( ) { while ( this . disposables . length ) { const disposable = this . disposables . pop ( ) ; if ( disposable ) { disposable . dispose ( ) ; } } } } export const testResultsWatcher = new TestResultsWatcher ( ) ;