Skip to content

Commit

Permalink
feat(config): support manifest.exports
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 3, 2024
1 parent 0990f20 commit ae18578
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/registry/src/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class LocalScanner {
return results.flat(1).filter((x): x is string => !!x)
}

checkEcosystem(meta: PackageJson, eco: Ecosystem) {
private checkEcosystem(meta: PackageJson, eco: Ecosystem) {
for (const peer in eco.peerDependencies) {
if (!meta.peerDependencies?.[peer]) return
}
Expand All @@ -142,7 +142,7 @@ export class LocalScanner {
if (eco.manifest in meta) return meta.name
}

loadEcosystem(eco: Ecosystem) {
private loadEcosystem(eco: Ecosystem) {
for (const [name, { meta, workspace }] of Object.entries(this.candidates)) {
const shortname = this.checkEcosystem(meta, eco)
if (!shortname) continue
Expand Down
6 changes: 3 additions & 3 deletions packages/registry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ export interface IconSvg {
pathData: string
}

export interface Manifest extends Manifest.Base {
export interface Manifest extends Manifest.Export {
icon?: IconSvg
hidden?: boolean
preview?: boolean
insecure?: boolean
category?: string
public?: string[]
exports?: Dict<Manifest.Base | null | undefined>
exports?: Dict<Manifest.Export | null>
ecosystem?: Partial<Ecosystem>
}

export namespace Manifest {
export interface Base {
export interface Export {
browser?: boolean
description?: string | Dict<string>
service?: Manifest.Service
Expand Down
11 changes: 6 additions & 5 deletions packages/registry/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export namespace Ensure {
export const string = primitive<string>('string')
}

function concludeBase(base?: Manifest.Base | null, description?: string) {
if (typeof base !== 'object') return
function concludeExport(base?: Manifest.Export | null, description?: string) {
// undefined values are dropped during serialization
if (typeof base !== 'object') return undefined!
if (!base) return null

const result: Manifest.Base = {
const result: Manifest.Export = {
browser: Ensure.boolean(base.browser),
description: Ensure.dict(base.description) ?? Ensure.string(description),
service: Ensure.object(base.service, (service) => ({
Expand All @@ -64,7 +65,7 @@ function concludeBase(base?: Manifest.Base | null, description?: string) {

export function conclude(meta: PackageJson, prop = 'cordis') {
const result: Manifest = {
...concludeBase(meta[prop], meta.description),
...concludeExport(meta[prop], meta.description),
hidden: Ensure.boolean(meta[prop]?.hidden),
preview: Ensure.boolean(meta[prop]?.preview),
insecure: Ensure.boolean(meta[prop]?.insecure),
Expand All @@ -75,7 +76,7 @@ export function conclude(meta: PackageJson, prop = 'cordis') {
pattern: Ensure.array(ecosystem.pattern),
keywords: Ensure.array(ecosystem.keywords),
})),
exports: Ensure.dict(meta[prop]?.exports, concludeBase),
exports: Ensure.dict(meta[prop]?.exports, concludeExport),
}

return result
Expand Down
40 changes: 28 additions & 12 deletions plugins/config/client/components/select.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<el-dialog
:modelValue="!!dialogSelect"
@update:modelValue="dialogSelect = null"
@update:modelValue="dialogSelect = undefined"
class="plugin-select"
>
<template #header>
Expand All @@ -15,20 +15,31 @@
<slot name="tabs" :packages="packages"></slot>
<div class="content">
<el-scrollbar>
<div class="package" v-for="({ name, manifest }) in packages" :key="name" @click.stop="configure(name)">
<h3>{{ name }}</h3>
<k-markdown inline class="desc" :source="tt(manifest.description)"></k-markdown>
</div>
<template v-for="local in packages" :key="local.package.name">
<div
class="package"
v-if="local.manifest.exports?.['.'] !== null"
@click.stop="configure(local.package.name)">
<h3>{{ local.package.name }}</h3>
<k-markdown inline class="desc" :source="tt(local.manifest.description)"></k-markdown>
</div>
<template v-for="(manifest, name) in local.manifest.exports">
<div class="package" v-if="manifest">
<h3>{{ joinName(name, local.package.name) }}</h3>
<k-markdown inline class="desc" :source="tt(manifest.description)"></k-markdown>
</div>
</template>
</template>
</el-scrollbar>
</div>
</el-dialog>
</template>

<script lang="ts" setup>
import { router, send, useContext, useI18nText } from '@cordisjs/client'
import { router, Dict, send, useContext, useI18nText } from '@cordisjs/client'
import { computed, inject, nextTick, ref, watch } from 'vue'
import type { PackageData } from '../../src'
import type { LocalObject } from '@cordisjs/registry'
const ctx = useContext()
const tt = useI18nText()
Expand All @@ -41,15 +52,20 @@ const dialogSelect = computed({
const keyword = ref('')
const input = ref()
const filter = inject('plugin-select-filter', (data: PackageData) => true)
const filter = inject('plugin-select-filter', (local: LocalObject) => true)
const packages = computed(() => Object.values(ctx.manager.data.value.packages).filter(({ name, shortname }) => {
return name && shortname.includes(keyword.value.toLowerCase()) && filter(ctx.manager.data.value.packages[name])
const packages = computed(() => Object.values(ctx.manager.data.value.packages).filter((local) => {
return local.package.name.includes(keyword.value.toLowerCase()) && filter(local)
}))
function joinName(name: string, base: string) {
if (name.startsWith('./')) name = name.slice(2)
return base + '/' + name
}
async function configure(name: string) {
const parent = dialogSelect.value.path
dialogSelect.value = null
const parent = dialogSelect.value!.path
dialogSelect.value = undefined
keyword.value = ''
const id = await send('manager.config.create', {
name,
Expand Down
4 changes: 2 additions & 2 deletions plugins/config/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {} from '@cordisjs/plugin-hmr'
declare module '@cordisjs/loader' {
namespace Entry {
interface Options {
label?: string | Dict<string>
collapse?: boolean
label?: string | Dict<string> | null
collapse?: boolean | null
}
}
}
Expand Down

0 comments on commit ae18578

Please sign in to comment.