-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manager): auto detect invalid schema
- Loading branch information
Showing
5 changed files
with
133 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Dict } from 'koishi' | ||
import { computed } from 'vue' | ||
import { MarketProvider, Package } from '@koishijs/plugin-manager' | ||
import { store } from '~/client' | ||
import { getMixedMeta } from '../utils' | ||
|
||
interface DepInfo { | ||
name: string | ||
required: boolean | ||
fulfilled: boolean | ||
} | ||
|
||
interface ServiceDepInfo extends DepInfo { | ||
available?: string[] | ||
} | ||
|
||
interface PluginDepInfo extends DepInfo { | ||
local?: boolean | ||
} | ||
|
||
export interface EnvInfo { | ||
impl: string[] | ||
deps: Dict<PluginDepInfo> | ||
using: Dict<ServiceDepInfo> | ||
invalid?: boolean | ||
console?: boolean | ||
} | ||
|
||
function getKeywords(prefix: string, meta: Package.Meta) { | ||
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', { | ||
...remote.versions[0], | ||
...store.packages[remote.name], | ||
}).includes(name) | ||
} | ||
|
||
function getEnvInfo(name: string) { | ||
function setService(name: string, required: boolean) { | ||
if (name === 'console') { | ||
result.console = true | ||
return | ||
} | ||
|
||
const fulfilled = name in store.services | ||
if (required && !fulfilled) result.invalid = true | ||
result.using[name] = { name, required, fulfilled } | ||
if (!fulfilled) { | ||
result.using[name].available = Object.values(store.market || {}) | ||
.filter(data => isAvailable(name, data)) | ||
.map(data => data.name) | ||
} | ||
} | ||
|
||
// check implementations | ||
const data = getMixedMeta(name) | ||
const result: EnvInfo = { impl: [], using: {}, deps: {} } | ||
for (const name of getKeywords('impl', data)) { | ||
if (name === 'adapter') continue | ||
result.impl.push(name) | ||
if (name in store.services && !data.id) { | ||
result.invalid = true | ||
} | ||
} | ||
|
||
// check services | ||
for (const name of getKeywords('required', data)) { | ||
setService(name, true) | ||
} | ||
for (const name of getKeywords('optional', data)) { | ||
setService(name, false) | ||
} | ||
|
||
// check dependencies | ||
for (const name of data.peerDeps) { | ||
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))) { | ||
delete result.using[impl] | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
export const envMap = computed(() => { | ||
return Object.fromEntries(Object.keys(store.packages).map(name => [name, getEnvInfo(name)])) | ||
}) |