diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ff94f..b9baac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Changelog +## 1.1.4 +* \#39: Cleanup old releases and UI upgrades + ## 1.1.3 * CentOS 8 support * Support remote send and receive timeouts diff --git a/src/constants.js b/src/constants.js index a2d90ab..34f4f1b 100644 --- a/src/constants.js +++ b/src/constants.js @@ -85,6 +85,8 @@ exports.IPC_Check_Installed_Reply = 'check_installed_reply'; exports.IPC_Check_Mount_Location = 'check_mount_location'; +exports.IPC_Cleanup_Releases = 'IPC_Cleanup_Releases'; + exports.IPC_Delete_File = 'delete_file'; exports.IPC_Detect_Mount = 'detect_mount'; diff --git a/src/helpers.js b/src/helpers.js index edd1ddf..e9d18bd 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -87,6 +87,22 @@ const _getRepertoryExec = version => { }; }; +const _removeDirectoryRecursively = dir => { + if (fs.existsSync(dir)) { + fs + .readdirSync(dir) + .forEach(file => { + const curPath = path.join(dir, file); + if (fs.lstatSync(curPath).isDirectory()) { + module.exports.removeDirectoryRecursively(curPath); + } else { + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(dir); + } +}; + const _resolvePath = str => { if (os.platform() === 'win32') { return str.replace(/%([^%]+)%/g, (_, n) => { @@ -130,6 +146,36 @@ module.exports.checkDaemonVersion = (version, provider) => { }); }; +module.exports.cleanupOldReleases = versionList => { + return new Promise((resolve, reject) => { + try { + if (versionList && versionList.length > 0) { + const dataDir = _getDataDirectory(); + const directoryList = fs + .readdirSync(dataDir, {withFileTypes: true}) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent); + + const removeList = directoryList + .filter(dirent => !versionList.includes(dirent.name)) + .map(dirent => dirent.name); + + for (const dir of removeList) { + try { + _removeDirectoryRecursively(path.join(dataDir, dir)); + } catch (e) { + console.log(e); + } + } + resolve(); + } + } catch (e) { + reject(e); + console.log(e); + } + }); +}; + module.exports.createSignatureFiles = (signature, publicKey) => { const fileName1 = RandomString.generate({ length: 12, @@ -653,21 +699,7 @@ module.exports.performWindowsUninstall = names => { }); }; -module.exports.removeDirectoryRecursively = (p) => { - if (fs.existsSync(p)) { - fs - .readdirSync(p) - .forEach(file => { - const curPath = path.join(p, file); - if (fs.lstatSync(curPath).isDirectory()) { - module.exports.removeDirectoryRecursively(curPath); - } else { - fs.unlinkSync(curPath); - } - }); - fs.rmdirSync(p); - } -}; +module.exports.removeDirectoryRecursively = _removeDirectoryRecursively; module.exports.resolvePath = _resolvePath; diff --git a/src/redux/actions/release_version_actions.js b/src/redux/actions/release_version_actions.js index 89e168e..7ce8b5c 100644 --- a/src/redux/actions/release_version_actions.js +++ b/src/redux/actions/release_version_actions.js @@ -13,6 +13,7 @@ import { setDismissDependencies } from './install_actions'; import {unmountAll} from './mount_actions'; +import {getIPCRenderer} from '../../utils'; export const CLEAR_UI_UPGRADE = 'relver/clearUIUpgrade'; export const clearUIUpgrade = () => { @@ -22,6 +23,17 @@ export const clearUIUpgrade = () => { }; }; +const cleanupOldReleases = versionList => { + return dispatch => { + const ipcRenderer = getIPCRenderer(); + if (ipcRenderer) { + ipcRenderer.sendSync(Constants.IPC_Cleanup_Releases + '_sync', { + version_list: versionList + }); + } + }; +}; + export const detectUIUpgrade = () => { return (dispatch, getState) => { axios @@ -77,6 +89,12 @@ export const loadReleases = () => { dispatch(setAllowDismissDependencies(versionLookup[Constants.RELEASE_TYPES[release]].length > 1)); } dispatch(checkVersionInstalled()); + + let versionList = []; + for (const key of Object.keys(locationsLookup)) { + versionList.push(key); + } + dispatch(cleanupOldReleases(versionList)) }; if ((version !== state.Version) || (release !== state.Release)) { diff --git a/src/renderer/ipc/ReleaseIPC.js b/src/renderer/ipc/ReleaseIPC.js index 5d1eb66..93a6b09 100644 --- a/src/renderer/ipc/ReleaseIPC.js +++ b/src/renderer/ipc/ReleaseIPC.js @@ -29,6 +29,12 @@ const addListeners = (ipcMain, standardIPCReply) => { }); }); + ipcMain.on(Constants.IPC_Cleanup_Releases + '_sync', (event, data) => { + helpers.cleanupOldReleases(data.version_list); + + event.returnValue = true; + }); + ipcMain.on(Constants.IPC_Extract_Release, (event, data) => { const destination = path.join(helpers.getDataDirectory(), data.Version); helpers.removeDirectoryRecursively(destination); @@ -84,4 +90,4 @@ const addListeners = (ipcMain, standardIPCReply) => { module.exports = { addListeners -}; \ No newline at end of file +};