import { JsonMap } from '@salesforce/ts-types' ; import * as fs from 'fs' ; import { QueryResult } from 'jsforce' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { getDocumentName , getRootWorkspacePath } from '../commonUtils' ; import { nls } from '../messages' ; import { CsvDataProvider , DataProvider , JsonDataProvider } from './dataProviders' ; export enum FileFormat { JSON = 'json' , CSV = 'csv' } export class QueryDataFileService { private dataProvider : DataProvider ; private documentName : string ; constructor ( private queryText , private queryData , private format , private document ) { this . documentName = getDocumentName ( document ) ; this . dataProvider = this . getDataProvider ( ) ; } protected getDataProvider ( ) { switch ( this . format ) { case FileFormat . CSV : return new CsvDataProvider ( this . documentName ) ; case FileFormat . JSON : return new JsonDataProvider ( this . documentName ) ; default : throw new Error ( 'No DataProvider Found' ) ; } } public async save ( ) { let selectedFileSavePath = '' ; const fileContent = this . dataProvider . getFileContent ( this . queryText , this . queryData . records ) ; const defaultFileName = this . dataProvider . getFileName ( ) ; const queryDataDefaultFilePath = path . join ( path . parse ( this . document . uri . path ) . dir , defaultFileName ) ; const fileInfo = await vscode . window . showSaveDialog ( { defaultUri : vscode . Uri . file ( queryDataDefaultFilePath ) } ) ; if ( fileInfo && fileInfo . fsPath ) { selectedFileSavePath = fileInfo . fsPath ; fs . writeFileSync ( selectedFileSavePath , fileContent ) ; this . showFileInExplorer ( selectedFileSavePath ) ; this . showSaveSuccessMessage ( path . basename ( selectedFileSavePath ) ) ; } return selectedFileSavePath ; } private showFileInExplorer ( targetPath ) { if ( targetPath . startsWith ( getRootWorkspacePath ( ) ) ) { vscode . commands . executeCommand ( 'revealInExplorer' , vscode . Uri . file ( targetPath ) ) ; } } private showSaveSuccessMessage ( savedFileName ) { vscode . window . showInformationMessage ( nls . localize ( 'info_file_save_success' , savedFileName ) ) ; } }