fix: install and upgrade in store for jsr packages

This commit is contained in:
Huakun Shen 2025-01-12 03:11:55 -05:00
parent 130608cd92
commit 7e811440b8
3 changed files with 30 additions and 11 deletions

View File

@ -22,7 +22,11 @@ function createExtensionsStore(): Writable<ExtPackageJsonExtra[]> & {
registerNewExtensionByPath: (extPath: string) => Promise<ExtPackageJsonExtra>
uninstallStoreExtensionByIdentifier: (identifier: string) => Promise<ExtPackageJsonExtra>
uninstallDevExtensionByIdentifier: (identifier: string) => Promise<ExtPackageJsonExtra>
upgradeStoreExtension: (identifier: string, tarballUrl: string) => Promise<ExtPackageJsonExtra>
upgradeStoreExtension: (
identifier: string,
tarballUrl: string,
extras?: { overwritePackageJson?: string }
) => Promise<ExtPackageJsonExtra>
} {
const store = writable<ExtPackageJsonExtra[]>([])
@ -176,12 +180,13 @@ function createExtensionsStore(): Writable<ExtPackageJsonExtra[]> & {
async function upgradeStoreExtension(
identifier: string,
tarballUrl: string
tarballUrl: string,
extras?: { overwritePackageJson?: string }
): Promise<ExtPackageJsonExtra> {
const extsDir = get(appConfig).extensionsInstallDir
if (!extsDir) throw new Error("Extension path not set")
return uninstallStoreExtensionByIdentifier(identifier).then(() =>
installFromTarballUrl(tarballUrl, extsDir)
installFromTarballUrl(tarballUrl, extsDir, extras)
)
}

View File

@ -4,8 +4,9 @@
import { supabaseAPI } from "@/supabase"
import { goBackOnEscapeClearSearchTerm, goHomeOnEscapeClearSearchTerm } from "@/utils/key"
import { goBack, goHome } from "@/utils/route"
import { SBExt } from "@kksh/api/supabase"
import { SBExt, type Tables } from "@kksh/api/supabase"
import { isUpgradable } from "@kksh/extension"
import type { ExtPublishMetadata } from "@kksh/supabase/models"
import { Button, Command } from "@kksh/svelte5"
import { Constants } from "@kksh/ui"
import { ExtListItem } from "@kksh/ui/extension"
@ -14,6 +15,7 @@
import { ArrowLeft } from "lucide-svelte"
import type { Snippet } from "svelte"
import { toast } from "svelte-sonner"
import { getInstallExtras } from "./[identifier]/helper.js"
let { data } = $props()
const { storeExtList, installedStoreExts, installedExtsMap, upgradableExpsMap } = data
@ -38,10 +40,17 @@
return toast.error("Fail to get latest extension", {
description: res.error.message
})
const tarballUrl = supabaseAPI.translateExtensionFilePathToUrl(res.data.tarball_path)
return extensions.upgradeStoreExtension(ext.identifier, tarballUrl).then((newExt) => {
toast.success(`${ext.name} Upgraded to ${newExt.version}`)
})
const tarballUrl = res.data.tarball_path.startsWith("http")
? res.data.tarball_path
: supabaseAPI.translateExtensionFilePathToUrl(res.data.tarball_path)
const installExtras = await getInstallExtras(
res.data as Tables<"ext_publish"> & { metadata: ExtPublishMetadata }
)
return extensions
.upgradeStoreExtension(ext.identifier, tarballUrl, installExtras)
.then((newExt) => {
toast.success(`${ext.name} Upgraded to ${newExt.version}`)
})
}
async function onExtItemInstall(ext: SBExt) {
@ -51,10 +60,15 @@
description: res.error.message
})
const tarballUrl = supabaseAPI.translateExtensionFilePathToUrl(res.data.tarball_path)
const tarballUrl = res.data.tarball_path.startsWith("http")
? res.data.tarball_path
: supabaseAPI.translateExtensionFilePathToUrl(res.data.tarball_path)
const installExtras = await getInstallExtras(
res.data as Tables<"ext_publish"> & { metadata: ExtPublishMetadata }
)
const installDir = await getExtensionsFolder()
return extensions
.installFromTarballUrl(tarballUrl, installDir)
.installFromTarballUrl(tarballUrl, installDir, installExtras)
.then(() => toast.success(`Plugin ${ext.name} Installed`))
.then(() =>
supabaseAPI.incrementDownloads({

View File

@ -8,7 +8,7 @@ export async function getInstallExtras(
if (ext.metadata.sourceType) {
if (ext.metadata.sourceType === "jsr") {
if (ext.metadata.source) {
try {
try {
const res = await fetch(`${ext.metadata.source}/package.json`)
const pkgJsonContent = await res.text()
extras.overwritePackageJson = pkgJsonContent