Skip to content

Commit

Permalink
fix(market): handle install failure, fix #271
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 19, 2023
1 parent 6b88267 commit f14f01e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
10 changes: 7 additions & 3 deletions plugins/market/client/components/install.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<p>正在加载版本数据……</p>
</div>

<k-comment v-if="store.dependencies?.[active] && !current" type="danger">
<p>该依赖的安装发生了错误,你可以尝试修复或移除它。</p>
</k-comment>
<el-scrollbar v-if="data?.[version] && Object.keys(data[version].peers).length">
<table>
<tr>
Expand Down Expand Up @@ -63,9 +67,9 @@
<el-button v-else @click="installDep(version)" :disabled="unchanged">添加</el-button>
</template>
<template v-else-if="data">
<el-button v-if="current || config.bulk && config.override[active]" @click="installDep('')" type="danger">移除</el-button>
<el-button v-if="current || store.dependencies[active] || config.bulk && config.override[active]" @click="installDep('')" type="danger">移除</el-button>
<el-button :type="result" @click="installDep(version)" :disabled="unchanged">
{{ current ? '更新' : '安装' }}
{{ current ? '更新' : store.dependencies?.[active] ? '修复' : '安装' }}
</el-button>
</template>
</div>
Expand Down Expand Up @@ -114,7 +118,7 @@ const selectVersion = computed({
const unchanged = computed(() => {
return !data.value?.[version.value]
|| version.value === store.dependencies?.[active.value]?.request
|| version.value === store.dependencies?.[active.value]?.request && !!store.dependencies?.[active.value]?.resolved
})
const current = computed(() => store.dependencies?.[active.value]?.resolved)
Expand Down
4 changes: 2 additions & 2 deletions plugins/market/client/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const manualDeps = reactive<Dict<Dict<AnalyzeResult>>>({})
export const showManual = ref(false)
export const showConfirm = ref(false)

export async function install(override: Dict<string>, callback?: () => Awaitable<void>) {
export async function install(override: Dict<string>, callback?: () => Awaitable<void>, forced?: boolean) {
const instance = loading({
text: '正在更新依赖……',
})
Expand All @@ -54,7 +54,7 @@ export async function install(override: Dict<string>, callback?: () => Awaitable
})
try {
active.value = ''
const code = await send('market/install', override)
const code = await send('market/install', override, forced)
if (code) {
message.error('安装失败!')
} else {
Expand Down
6 changes: 3 additions & 3 deletions plugins/market/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare module '@koishijs/console' {
}

interface Events {
'market/install'(deps: Dict<string>): Promise<number>
'market/install'(deps: Dict<string>, forced?: boolean): Promise<number>
'market/registry'(name: string): Promise<Dict<Pick<RemotePackage, DependencyMetaKey>>>
}
}
Expand Down Expand Up @@ -155,8 +155,8 @@ export function apply(ctx: Context, config: Config) {
prod: resolve(__dirname, '../../dist'),
})

ctx.console.addListener('market/install', async (deps) => {
const code = await ctx.installer.install(deps)
ctx.console.addListener('market/install', async (deps, forced) => {
const code = await ctx.installer.install(deps, forced)
ctx.console.dependencies?.refresh()
ctx.console.registry?.refresh()
ctx.console.packages?.refresh()
Expand Down
14 changes: 9 additions & 5 deletions plugins/market/src/node/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,18 @@ class Installer extends Service {
return this.exec(this.agent, args)
}

async install(deps: Dict<string>) {
async install(deps: Dict<string>, forced?: boolean) {
const oldDeps = await this.getDeps()
await this.override(deps)

let shouldInstall = false
for (const name in deps) {
const { resolved } = oldDeps[name] || {}
if (deps[name] && resolved && satisfies(resolved, deps[name], { includePrerelease: true })) continue
shouldInstall = true
forced = true
break
}

if (shouldInstall) {
if (forced) {
const code = await this._install()
if (code) return code
}
Expand All @@ -224,7 +223,12 @@ class Installer extends Service {
const { resolved, workspace } = oldDeps[name]
if (workspace || !newDeps[name]) continue
if (newDeps[name].resolved === resolved) continue
if (!(require.resolve(name) in require.cache)) continue
try {
if (!(require.resolve(name) in require.cache)) continue
} catch (error) {
logger.warn(error)
continue
}
this.ctx.loader.fullReload()
}

Expand Down

0 comments on commit f14f01e

Please sign in to comment.