Skip to content

Commit

Permalink
feat: add cleaner plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Nov 30, 2021
1 parent f26e645 commit 401adb3
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
28 changes: 28 additions & 0 deletions addons/cleaner/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {import('../../api/types').Commas} commas
*/
module.exports = function (commas) {
if (commas.app.isMainProcess()) {

const path = require('path')
const { ipcMain } = require('electron')

ipcMain.handle('get-cache-size', event => {
return event.sender.session.getCacheSize()
})

ipcMain.handle('clear-cache', event => {
event.sender.session.clearCache()
})

commas.i18n.addTranslationDirectory(path.join(__dirname, 'locales'))

} else {

commas.context.provide('preference', {
component: commas.bundler.extract('cleaner/renderer/cleaner-link.vue').default,
group: 'about',
})

}
}
3 changes: 3 additions & 0 deletions addons/cleaner/locales/zh-CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Clear Cache#!cache.1": "清理缓存"
}
9 changes: 9 additions & 0 deletions addons/cleaner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "cleaner",
"private": true,
"version": "0.1.0",
"description": "Clean up the application cache",
"main": "index.js",
"author": "commas",
"license": "ISC"
}
58 changes: 58 additions & 0 deletions addons/cleaner/renderer/cleaner-link.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<template>
<div class="form-line">
<span class="link cleaner-link" @click="clear">
<span v-i18n>Clear Cache#!cache.1</span>
</span>
<span class="cache-size" @click="check">{{ size }}</span>
</div>
</template>

<script lang="ts">
import { ipcRenderer } from 'electron'
import { computed, onMounted, ref, unref } from 'vue'
export default {
name: 'cleaner-link',
setup() {
const cacheSizeRef = ref()
const sizeRef = computed(() => {
let cacheSize = unref(cacheSizeRef)
if (typeof cacheSize !== 'number') return 'N/A'
const group = 1024
let units = ['B', 'K', 'M', 'G', 'T']
let currentIndex = 0
while (cacheSize > group && currentIndex < units.length - 1) {
cacheSize /= group
currentIndex += 1
}
return `${Math.round(cacheSize * 100) / 100}${units[currentIndex]}`
})
async function check() {
cacheSizeRef.value = await ipcRenderer.invoke('get-cache-size')
}
async function clear() {
await ipcRenderer.invoke('clear-cache')
check()
}
onMounted(() => {
check()
})
return {
size: sizeRef,
check,
clear,
}
},
}
</script>

<style lang="scss" scoped>
.cache-size {
margin-left: 1em;
}
</style>
4 changes: 2 additions & 2 deletions resources/settings.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@
"key": "terminal.addon.includes",
"type": "list",
"paradigm": ["string"],
"recommendations": ["cli", "updater", "preference", "proxy", "settings", "theme", "menu", "power-mode", "landscape"],
"recommendations": ["cli", "updater", "preference", "proxy", "settings", "theme", "menu", "power-mode", "cleaner", "landscape"],
"label": "Enabled Addons",
"comments": [
"List of enabled addon name"
],
"default": ["cli", "updater", "preference", "proxy", "settings", "theme", "menu", "power-mode"]
"default": ["cli", "updater", "preference", "proxy", "settings", "theme", "menu", "power-mode", "cleaner"]
}
]

0 comments on commit 401adb3

Please sign in to comment.