import { PackageInfo } from '@/types/base' ; import { INodeVersions } from '@/types/node' ; import { ipcRenderer } from 'electron' ; const DEFAULT_INSTALL_RESULT = { nodeVersion : '' , npmVersion : '' } ; const DEFAULT_NODE_INSTALL_FORM_VALUE = { } ; const DEFAULT_NODE_INSTALL_STATUS = { installNode : 'wait' , reinstallPackages : 'wait' , } ; const DEFAULT_NODE_INSTALL_ERR_MSG = { installNode : '' , reinstallPackages : '' , } ; const DEFAULT_NODE_VERSIONS = { versions : [ ] , majors : [ ] , } ; export default { state : { nodeInfo : { } , nodeVersions : DEFAULT_NODE_VERSIONS , currentStep : 0 , nodeInstallStatus : DEFAULT_NODE_INSTALL_STATUS , nodeInstallErrMsg : DEFAULT_NODE_INSTALL_ERR_MSG , installResult : DEFAULT_INSTALL_RESULT , nodeInstallFormValue : DEFAULT_NODE_INSTALL_FORM_VALUE , nodeInstallVisible : false , nodeInstallChannel : 'install-node' , nodeInstallProcessStatusChannel : 'install-node-process-status' , } , reducers : { updateNodeInfo ( prevState , payload ) { prevState . nodeInfo = payload ; } , updateNodeVersions ( prevState , payload ) { prevState . nodeVersions = payload ; } , updateStep ( prevState , currentStep ) { prevState . currentStep = currentStep ; } , initStep ( prevState ) { prevState . currentStep = 0 ; } , updateNodeInstallStatus ( prevState , { status , stepName } ) { prevState . nodeInstallStatus [ stepName ] = status ; } , updateNodeInstallErrMsg ( prevState , { errMsg , stepName } ) { prevState . nodeInstallErrMsg [ stepName ] = errMsg ; } , initNodeInstall ( prevState ) { prevState . nodeInstallStatus = DEFAULT_NODE_INSTALL_STATUS ; prevState . nodeInstallErrMsg = DEFAULT_NODE_INSTALL_ERR_MSG ; prevState . installResult = DEFAULT_INSTALL_RESULT ; prevState . installNodeFormValue = DEFAULT_NODE_INSTALL_FORM_VALUE ; } , updateInstallResult ( prevState , installResult ) { prevState . installResult = { ... prevState . installResult , ... installResult } ; } , updateNodeInstallFormValue ( prevState , formValue ) { prevState . nodeInstallFormValue = formValue ; } , setNodeInstallVisible ( prevState , visible ) { prevState . nodeInstallVisible = visible ; } , } , effects : ( dispatch ) => ( { async getNodeInfo ( ) { const nodeInfo = await ipcRenderer . invoke ( 'get-node-info' ) ; dispatch . nodeVersion . updateNodeInfo ( nodeInfo ) ; } , async getNodeVersions ( ) { const nodeVersions = await ipcRenderer . invoke ( 'get-node-versions' ) ; dispatch . nodeVersion . updateNodeVersions ( nodeVersions ) ; } , async clearCaches ( { processChannel , installChannel } ) { await ipcRenderer . invoke ( 'clear-node-install-cache' , { processChannel , installChannel } ) ; } , async getCaches ( { processChannel , installChannel } ) { const { processCaches } = await ipcRenderer . invoke ( 'get-node-install-cache' , { processChannel , installChannel } ) ; if ( Array . isArray ( processCaches ) ) { processCaches . forEach ( ( { task , status , result , errMsg } ) => { dispatch . nodeVersion . updateNodeInstallStatus ( { status , stepName : task } ) ; if ( status === 'success' ) { if ( result ) { dispatch . nodeVersion . updateInstallResult ( result ) ; } } else if ( status === 'error' ) { dispatch . nodeVersion . updateNodeInstallErrMsg ( { errMsg , stepName : task } ) ; } if ( status === 'success' || status === 'error' ) { if ( task === 'installNode' ) { dispatch . nodeVersion . updateStep ( 2 ) ; } else if ( task === 'reinstallPackages' ) { dispatch . nodeVersion . updateStep ( 3 ) ; } } else if ( status === 'done' ) { dispatch . nodeVersion . updateStep ( 3 ) ; } } ) ; } } , } ) , } ;