Skip to content

Commit

Permalink
feat(manager): auto install new plugin with dep hint and market
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 9, 2022
1 parent 17fcdca commit 8830f2a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/worker/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Watcher {
}

private triggerGroupReload(neo: Dict, old: Dict, runtime: Plugin.Runtime) {
runtime.config = neo
for (const name in { ...old, ...neo }) {
if (name.startsWith('~') || name.startsWith('$')) continue
const fork = runtime[Loader.kRecord][name]
Expand Down
2 changes: 1 addition & 1 deletion plugins/frontend/manager/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default (ctx: Context) => {
icon: 'puzzle-piece',
order: 620,
authority: 4,
fields: ['market', 'packages'],
fields: ['config', 'market', 'packages'],
component: Market,
})

Expand Down
7 changes: 2 additions & 5 deletions plugins/frontend/manager/client/market/package.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<a v-if="data.links.homepage" :href="data.links.homepage" target="_blank" rel="noopener noreferrer">
<k-icon name="link"></k-icon>
</a>
<k-button v-if="store.packages[data.name]" solid type="success" class="right" @click="router.push('/settings/' + data.name)">配置</k-button>
<k-button v-if="store.packages[data.name]" solid type="success" class="right" @click="gotoSettings(data.shortname)">配置</k-button>
<k-button v-else-if="!config.override[data.name]" solid class="right" @click="addFavorite(data.name)">添加</k-button>
<k-button v-else solid type="warning" class="right" @click="removeFavorite(data.name)">取消</k-button>
</template>
Expand Down Expand Up @@ -48,10 +48,9 @@
<script lang="ts" setup>
import { computed, PropType } from 'vue'
import { useRouter } from 'vue-router'
import { MarketProvider } from '@koishijs/plugin-manager'
import { store } from '@koishijs/client'
import { config, addFavorite, removeFavorite, getMixedMeta } from '../utils'
import { config, addFavorite, removeFavorite, getMixedMeta, gotoSettings } from '../utils'
defineEmits(['query'])
Expand All @@ -63,8 +62,6 @@ const meta = computed(() => getMixedMeta(props.data.name))
const email = computed(() => props.data.author?.email)
const router = useRouter()
</script>

<style lang="scss">
Expand Down
7 changes: 2 additions & 5 deletions plugins/frontend/manager/client/settings/dep-link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import { store } from '@koishijs/client'
import { addFavorite } from '../utils'
import { addFavorite, gotoSettings } from '../utils'
defineProps<{
name: string
}>()
const router = useRouter()
function configurate(name: string) {
if (store.packages[name]) {
router.push('/settings/' + name)
gotoSettings(name.replace('koishi-plugin-', '').replace('@koishijs/plugin-', ''))
} else {
addFavorite(name)
}
Expand Down
24 changes: 7 additions & 17 deletions plugins/frontend/manager/client/settings/group.vue
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
<template>
<h1 class="config-header">
{{ current.label }}
<k-button solid @click="addPlugin">添加插件</k-button>
<k-button solid @click="addGroup">添加分组</k-button>
<k-button solid @click="execute('unload', '')">添加插件</k-button>
<k-button solid @click="execute('group', 'group')">添加分组</k-button>
</h1>
</template>

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import { send } from '@koishijs/client'
import { Tree, plugins } from './utils'
const router = useRouter()
import { send, router } from '@koishijs/client'
import { Tree } from './utils'
const props = defineProps<{
current: Tree
}>()
function addPlugin() {
const id = Math.random().toString(36).slice(2, 8)
const path = (props.current.path ? props.current.path + '/' : '') + '@' + id
send('manager/unload', path, {})
router.replace('/plugins/' + path)
}
function addGroup() {
function execute(action: 'group' | 'unload', name: string) {
const id = Math.random().toString(36).slice(2, 8)
const path = (props.current.path ? props.current.path + '/' : '') + 'group@' + id
send('manager/group', path)
const path = (props.current.path ? props.current.path + '/' : '') + name + '@' + id
send(`manager/${action}`, path)
router.replace('/plugins/' + path)
}
Expand Down
25 changes: 24 additions & 1 deletion plugins/frontend/manager/client/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dict } from 'koishi'
import { computed, watch } from 'vue'
import { createStorage, store } from '@koishijs/client'
import { createStorage, router, send, store } from '@koishijs/client'

interface ManagerConfig {
override: Dict<string>
Expand Down Expand Up @@ -44,3 +44,26 @@ export const getMixedMeta = (name: string) => ({
...store.market[name],
...store.packages[name],
})

function findPlugin(target: string, plugins: {}, prefix: string) {
for (let key in plugins) {
const config = plugins[key]
if (key.startsWith('~')) key = key.slice(1)
const request = key.split('@')[0]
if (request === target) return prefix + key
if (request === 'group') {
const result = findPlugin(target, config, prefix + key + '/')
if (result) return result
}
}
}

export function gotoSettings(name: string) {
const path = findPlugin(name, store.config.plugins, '')
if (path) {
router.push('/plugins/' + path)
} else {
send('manager/unload', name, {})
router.push('/plugins/' + name)
}
}
3 changes: 1 addition & 2 deletions plugins/frontend/manager/src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ConfigWriter extends DataService<App.Config> {
this.refresh()
}

unload(path: string, config: any, newKey?: string) {
unload(path: string, config = {}, newKey?: string) {
const [runtime, oldKey] = this.resolve(path)
this.loader.unloadPlugin(runtime, oldKey)
rename(runtime.config, oldKey, '~' + (newKey || oldKey), config)
Expand Down Expand Up @@ -149,7 +149,6 @@ class ConfigWriter extends DataService<App.Config> {
const rest = Object.keys(runtimeT.config).slice(index)
insertKey(runtimeT.config, temp, rest)
this.loader.writeConfig()
this.refresh()
}

updateBot(id: string, adapter: string, config: any) {
Expand Down

0 comments on commit 8830f2a

Please sign in to comment.