Skip to content

Commit

Permalink
feat(manager): check non-local plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 11, 2022
1 parent 30aff4e commit e97c414
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/market/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/market",
"description": "Scan Package Manager for Koishi Plugins",
"version": "2.0.0",
"version": "2.0.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
12 changes: 6 additions & 6 deletions packages/market/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ export interface ScanConfig {
onFailure?(name: string, reason: any): void
}

function conclude(remote: RemotePackage) {
const manifest = {
export function conclude(meta: PackageJson) {
const manifest: Manifest = {
description: {
en: remote.description,
en: meta.description,
},
locales: [],
recommends: [],
...remote.koishi,
...meta.koishi,
service: {
required: [],
optional: [],
implements: [],
...remote.koishi?.service,
...meta.koishi?.service,
},
}

for (const keyword of remote.keywords ?? []) {
for (const keyword of meta.keywords ?? []) {
if (keyword === 'market:hidden') {
manifest.hidden = true
} else if (keyword.startsWith('required:')) {
Expand Down
14 changes: 7 additions & 7 deletions plugins/frontend/manager/client/market/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@

<script setup lang="ts">
import { store } from '@koishijs/client'
import { router, store } from '@koishijs/client'
import { computed, reactive, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
import { config } from '../utils'
import { validate } from './utils'
import type { AnalyzedPackage } from '@koishijs/market'
import PackageView from './package.vue'
const route = useRoute()
const router = useRouter()
const { keyword } = route.query
const words = reactive(Array.isArray(keyword) ? keyword : (keyword || '').split(' '))
const words = reactive([])
if (words[words.length - 1]) words.push('')
watch(() => route.query.keyword, (keyword) => {
words.splice(0, Infinity, ...Array.isArray(keyword) ? keyword : (keyword || '').split(' '))
if (words[words.length - 1]) words.push('')
}, { immediate: true })
const realWords = computed(() => words.filter(w => w))
Expand Down
9 changes: 1 addition & 8 deletions plugins/frontend/manager/client/settings/dep-link.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<span class="k-dep-link" @click.stop="configurate(name)">
<span class="link" @click.stop="configurate(name)">
{{ name }}
</span>
</template>
Expand All @@ -25,11 +25,4 @@ function configurate(name: string) {

<style scoped lang="scss">
.k-dep-link {
&:hover {
cursor: pointer;
text-decoration: underline;
}
}
</style>
21 changes: 18 additions & 3 deletions plugins/frontend/manager/client/settings/plugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</el-select>
</template>
<template v-else>
<span class="label">{{ data.shortname }}</span>
<span class="label">{{ current.label }}</span>
<k-alias :current="current"></k-alias>
</template>
<template v-if="!current.disabled">
<k-button solid type="error" @click="execute('unload')">停用插件</k-button>
<k-button solid :disabled="env.invalid" @click="execute('reload')">重载配置</k-button>
</template>
<template v-else-if="env">
<template v-else-if="name">
<k-button solid :disabled="env.invalid" @click="execute('reload')">启用插件</k-button>
<k-button solid @click="execute('unload')">保存配置</k-button>
</template>
Expand Down Expand Up @@ -74,6 +74,10 @@
<template #hint>{{ hint }}</template>
</k-form>
</template>

<k-comment v-else-if="current.label" type="error">
此插件尚未安装,<span class="link" @click.stop="gotoMarket">点击前往插件市场</span>。
</k-comment>
</template>

<script lang="ts" setup>
Expand All @@ -100,7 +104,7 @@ const name = computed(() => {
const shortname = temporary || label
if (shortname.includes('/')) {
const [left, right] = shortname.split('/')
return `${left}/koishi-plugin-${right}`
return [`${left}/koishi-plugin-${right}`].find(name => name in store.packages)
}
return [
`@koishijs/plugin-${shortname}`,
Expand All @@ -126,6 +130,10 @@ function execute(event: 'unload' | 'reload') {
}
}
function gotoMarket() {
router.push('/market?keyword=' + props.current.label)
}
</script>

<style lang="scss">
Expand All @@ -142,6 +150,13 @@ function execute(event: 'unload' | 'reload') {
font-size: 1.15rem;
color: var(--fg3);
}
span.link {
&:hover {
cursor: pointer;
text-decoration: underline;
}
}
}
</style>
31 changes: 13 additions & 18 deletions plugins/frontend/manager/client/settings/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Dict } from 'koishi'
import { computed } from 'vue'
import { PackageJson } from '@koishijs/market'
import { MarketProvider } from '@koishijs/plugin-manager'
import { router, store } from '@koishijs/client'
import { getMixedMeta } from '../utils'
Expand Down Expand Up @@ -28,18 +27,11 @@ export interface EnvInfo {
console?: boolean
}

function getKeywords(prefix: string, meta: Partial<PackageJson>) {
prefix += ':'
return (meta.keywords || [])
.filter(name => name.startsWith(prefix))
.map(name => name.slice(prefix.length))
}

function isAvailable(name: string, remote: MarketProvider.Data) {
return getKeywords('impl', {
return {
...remote.versions[0],
...store.packages[remote.name],
}).includes(name)
}.manifest?.service.implements.includes(name)
}

function getEnvInfo(name: string) {
Expand All @@ -59,32 +51,32 @@ function getEnvInfo(name: string) {
}
}

const data = getMixedMeta(name)
const local = store.packages[name]
const result: EnvInfo = { impl: [], using: {}, deps: {} }

// check implementations
for (const name of getKeywords('impl', data)) {
for (const name of local.manifest.service.implements) {
if (name === 'adapter') continue
result.impl.push(name)
}

// check services
for (const name of getKeywords('required', data)) {
for (const name of local.manifest.service.required) {
setService(name, true)
}
for (const name of getKeywords('optional', data)) {
for (const name of local.manifest.service.optional) {
setService(name, false)
}

// check dependencies
for (const name in data.peerDependencies) {
if (!name.startsWith('koishi-plugin-') || !name.startsWith('@koishijs/plugin-')) continue
for (const name in local.peerDependencies) {
if (!name.includes('koishi-plugin-') || !name.startsWith('@koishijs/plugin-')) continue
if (name === '@koishijs/plugin-console') continue
const available = name in store.packages
const fulfilled = !!store.packages[name]?.id
if (!fulfilled) result.invalid = true
result.deps[name] = { name, required: true, fulfilled, local: available }
for (const impl of getKeywords('impl', getMixedMeta(name))) {
for (const impl of getMixedMeta(name).manifest.service.implements) {
delete result.using[impl]
}
}
Expand All @@ -93,7 +85,10 @@ function getEnvInfo(name: string) {
}

export const envMap = computed(() => {
return Object.fromEntries(Object.keys(store.packages).map(name => [name, getEnvInfo(name)]))
return Object.fromEntries(Object
.keys(store.packages)
.filter(x => x)
.map(name => [name, getEnvInfo(name)]))
})

export interface Tree {
Expand Down
2 changes: 1 addition & 1 deletion plugins/frontend/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@types/which-pm-runs": "^1.0.0"
},
"dependencies": {
"@koishijs/market": "^2.0.0",
"@koishijs/market": "^2.0.1",
"cross-spawn": "^7.0.3",
"ns-require": "^1.1.2",
"semver": "^7.3.7",
Expand Down
6 changes: 4 additions & 2 deletions plugins/frontend/manager/src/packages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Adapter, App, Context, Dict, Logger, pick, Plugin, remove, Schema } from 'koishi'
import { DataService } from '@koishijs/plugin-console'
import { PackageJson } from '@koishijs/market'
import { conclude, Manifest, PackageJson } from '@koishijs/market'
import { promises as fsp } from 'fs'
import { dirname } from 'path'
import ns from 'ns-require'
Expand Down Expand Up @@ -122,12 +122,13 @@ class PackageProvider extends DataService<Dict<PackageProvider.Data>> {
'name',
'version',
'description',
'peerDependencies',
]) as PackageProvider.Data

// workspace packages are followed by symlinks
result.workspace = data.$workspace
result.shortname = data.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
result.manifest = conclude(data)
result.peerDependencies = { ...data.peerDependencies }

// check adapter
const oldLength = Object.keys(Adapter.library).length
Expand Down Expand Up @@ -157,6 +158,7 @@ namespace PackageProvider {
shortname?: string
schema?: Schema
workspace?: boolean
manifest?: Manifest
}
}

Expand Down

0 comments on commit e97c414

Please sign in to comment.