import { shell , contextBridge , remote } from "electron" ; import path from "path" ; import fs from "fs" ; import process from "process" ; import { invokeBackend , listenBackend } from "./preload-backend" ; import { copyDirectory } from "./preload-porting" ; import { IniLoader } from "aws-sdk/global" ; import { writeProfile } from "./setup" ; import jsZip from "jszip" ; import { localStore , nugetPackageStore , portingStore , reducerCacheStore , } from "./preload-localStore" ; import { Project , VersionPair , } from "@porting-assistant/react/src/models/project" ; import axios from "axios" ; import isDev from "electron-is-dev" ; contextBridge . exposeInMainWorld ( "electron" , { openExternalUrl : ( url ) => shell . openExternal ( url ) , openPath : ( path ) => shell . openPath ( path ) , saveState : ( key , value ) => localStore . set ( key , value ) , getState : ( key , defaultValue ) => localStore . get ( key , defaultValue ) , saveCache : ( value ) => reducerCacheStore . set ( "reducerCache" , value ) , getCache : ( defaultValue ) => reducerCacheStore . get ( "reducerCache" , defaultValue ) , saveNugetPackages : ( value ) => nugetPackageStore . set ( "nugetPackages" , value ) , getNugetPackages : ( defaultValue ) => nugetPackageStore . get ( "nugetPackages" , defaultValue ) , dialog : { showOpenDialog : ( options ) => invokeBackend ( "dialogShowOpenDialog" , options ) , showSaveDialog : ( options ) => invokeBackend ( "dialogShowSaveDialog" , options ) , } , getFilename : ( filePath ) => { return path . basename ( filePath ) ; } , getDirectory : ( filePath ) => { return path . dirname ( filePath ) ; } , getRelativePath : ( sourcePath , targetPath ) => { return path . relative ( path . dirname ( sourcePath ) , targetPath ) ; } , getPathSeparator : ( ) => { return path . sep ; } , joinPaths : ( ... paths ) => { return path . join ( ... paths ) ; } , pathExists : ( path ) => { return fs . existsSync ( path ) ; } , getProfiles : ( ) => { try { const iniLoder = new IniLoader ( ) ; return iniLoder . loadFrom ( { } ) ; } catch ( err ) { return [ ] ; } } , writeProfile , writeZipFile : ( zipFilename , contents ) => { return new Promise < void > ( async ( resolve , reject ) => { const zip = new jsZip ( ) ; contents . map ( ( c ) => zip . file ( c . filename , c . contents ) ) ; zip . generateNodeStream ( { type : "nodebuffer" , streamFiles : true } ) . pipe ( fs . createWriteStream ( zipFilename ) ) . on ( "error" , ( err ) => reject ( err ) ) . on ( "finish" , ( ) => resolve ( ) ) ; } ) ; } , verifyUser : ( profile ) => invokeBackend ( "verifyProfile" , profile ) , getVersion : ( ) => invokeBackend ( "getVersion" ) , getLatestVersion : ( ) => invokeBackend ( "getLatestVersion" ) , getOutdatedVersionFlag : ( ) => invokeBackend ( "getOutdatedVersionFlag" ) , telemetry : ( message ) => invokeBackend ( "telemetry" , message ) , writeReactErrLog : ( source , message , response ) => invokeBackend ( "writeReactErrLog" , source , message , response ) , getAssessmentLog : ( ) => { const dateString = new Date ( ) . toLocaleDateString ( "en-CA" ) . slice ( 0 , 10 ) . replace ( /-/g , "" ) ; return path . join ( remote . app . getPath ( "userData" ) , "logs" , `portingAssistant-assessment-${ dateString }.log` ) ; } , } ) ; contextBridge . exposeInMainWorld ( "backend" , { ping : ( ) => invokeBackend ( "ping" ) , analyzeSolution : ( solutionFilePath , runId , triggerType , settings ) => invokeBackend ( "analyzeSolution" , solutionFilePath , runId , triggerType , settings ) , openSolutionInIDE : ( solutionFilePath ) => invokeBackend ( "openSolutionInIDE" , solutionFilePath ) , getFileContents : ( sourceFilePath ) => invokeBackend ( "getFileContents" , sourceFilePath ) , listenNugetPackageUpdate : ( callback ) => listenBackend ( "onNugetPackageUpdate" , callback ) , listenApiAnalysisUpdate : ( callback ) => listenBackend ( "onApiAnalysisUpdate" , callback ) , checkInternetAccess : ( ) => invokeBackend ( "checkInternetAccess" ) , sendCustomerFeedback : ( upload ) => invokeBackend ( "sendCustomerFeedback" , upload ) , uploadRuleContribution : ( upload ) => invokeBackend ( "uploadRuleContribution" , upload ) } ) ; contextBridge . exposeInMainWorld ( "porting" , { portingStores : { } , copyDirectory : ( solutionPath , destinationPath ) => copyDirectory ( solutionPath , destinationPath ) , getConfig : ( ) => portingStore . get ( "solutions" ) , setConfig : ( data ) => portingStore . set ( "solutions" , data ) , applyPortingProjectFileChanges : ( projects , solutionPath , targetFramework , upgradeVersions ) => invokeBackend ( "applyPortingProjectFileChanges" , projects , solutionPath , targetFramework , upgradeVersions ) , } ) ; if ( process . env [ "NODE_ENV" ] === "test" ) { contextBridge . exposeInMainWorld ( "test" , { clearState : ( ) => localStore . clear ( ) , clearSolutions : ( ) => localStore . set ( "solutions" , { } ) , addSolution : ( solutionPath ) => { const localStoreObj = localStore . get ( "solutions" ) ; localStoreObj [ solutionPath ] = { solutionPath : solutionPath } ; return localStore . set ( "solutions" , localStoreObj ) ; } , } ) ; }