From 032090476f6cbbda1c7db54134a9ad62d75ab6c8 Mon Sep 17 00:00:00 2001 From: WarningImHack3r <43064022+WarningImHack3r@users.noreply.github.com> Date: Sat, 2 Mar 2024 20:54:10 +0100 Subject: [PATCH] Add JS support for Svelte --- CHANGELOG.md | 1 + .../backend/sources/Source.kt | 9 +++--- .../backend/sources/config/SvelteConfig.kt | 1 + .../backend/sources/impl/SvelteSource.kt | 28 +++++++++++++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07750ed..5111e08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ ### Added - [Svelte/Vue] Notify when an updated component does no longer uses some installed files, allowing to remove them +- [Svelte] Add support for the TypeScript option - Allow to remove installed dependencies when they are no longer used by any component ### Fixed diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt index 9559298..9faa069 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/Source.kt @@ -24,7 +24,7 @@ import java.nio.file.NoSuchFileException abstract class Source(val project: Project, private val serializer: KSerializer) { private val log = logger>() abstract var framework: String - private val domain: String + protected val domain: String get() = URI(getLocalConfig().`$schema`).let { uri -> "${uri.scheme}://${uri.host}".also { log.debug("Parsed domain: $it") @@ -59,10 +59,9 @@ abstract class Source(val project: Project, private val serializer: protected abstract fun adaptFileToConfig(contents: String): String - private fun fetchComponent(componentName: String): ComponentWithContents { - val style = getLocalConfig().style - val response = RequestSender.sendRequest("$domain/registry/styles/$style/$componentName.json") - return response.ok { Json.decodeFromString(it.body) } ?: throw Exception("Component $componentName not found") + protected open fun fetchComponent(componentName: String): ComponentWithContents { + return RequestSender.sendRequest("$domain/registry/styles/${getLocalConfig().style}/$componentName.json") + .ok { Json.decodeFromString(it.body) } ?: throw Exception("Component $componentName not found") } protected fun fetchColors(): JsonElement { diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/config/SvelteConfig.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/config/SvelteConfig.kt index 45175d2..092033e 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/config/SvelteConfig.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/config/SvelteConfig.kt @@ -8,6 +8,7 @@ class SvelteConfig( override val `$schema`: String, override val style: String, override val tailwind: Tailwind, + val typescript: Boolean = true, override val aliases: Aliases ) : Config() { diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteSource.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteSource.kt index 15c7053..787b4bd 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteSource.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/sources/impl/SvelteSource.kt @@ -3,12 +3,15 @@ package com.github.warningimhack3r.intellijshadcnplugin.backend.sources.impl import com.github.warningimhack3r.intellijshadcnplugin.backend.helpers.DependencyManager import com.github.warningimhack3r.intellijshadcnplugin.backend.helpers.FileManager import com.github.warningimhack3r.intellijshadcnplugin.backend.helpers.ShellRunner +import com.github.warningimhack3r.intellijshadcnplugin.backend.http.RequestSender import com.github.warningimhack3r.intellijshadcnplugin.backend.sources.Source import com.github.warningimhack3r.intellijshadcnplugin.backend.sources.config.SvelteConfig +import com.github.warningimhack3r.intellijshadcnplugin.backend.sources.remote.ComponentWithContents import com.github.warningimhack3r.intellijshadcnplugin.notifications.NotificationManager import com.intellij.notification.NotificationType import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.project.Project +import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject @@ -26,7 +29,8 @@ class SvelteSource(project: Project) : Source(project, SvelteConfi log.debug("Alias $alias does not start with $ or @, returning it as-is") } val usesKit = DependencyManager(project).isDependencyInstalled("@sveltejs/kit") - val configFile = if (usesKit) ".svelte-kit/tsconfig.json" else "tsconfig.json" + val tsConfigName = if (getLocalConfig().typescript) "tsconfig.json" else "jsconfig.json" + val configFile = if (usesKit) ".svelte-kit/$tsConfigName" else tsConfigName val fileManager = FileManager(project) var tsConfig = fileManager.getFileContentsAtPath(configFile) if (tsConfig == null) { @@ -42,7 +46,8 @@ class SvelteSource(project: Project) : Source(project, SvelteConfi throw NoSuchFileException("Cannot get or generate $configFile") } Thread.sleep(250) // wait for the sync to create the files - tsConfig = fileManager.getFileContentsAtPath(configFile) ?: throw NoSuchFileException("Cannot get $configFile") + tsConfig = + fileManager.getFileContentsAtPath(configFile) ?: throw NoSuchFileException("Cannot get $configFile") } val aliasPath = Json.parseToJsonElement(tsConfig) .jsonObject["compilerOptions"] @@ -55,6 +60,25 @@ class SvelteSource(project: Project) : Source(project, SvelteConfi } } + override fun fetchComponent(componentName: String): ComponentWithContents { + val config = getLocalConfig() + return if (config.typescript) { + super.fetchComponent(componentName) + } else { + RequestSender.sendRequest("$domain/registry/styles/${config.style}-js/$componentName.json") + .ok { Json.decodeFromString(it.body) } ?: throw Exception("Component $componentName not found") + } + } + + override fun adaptFileExtensionToConfig(extension: String): String { + return if (!getLocalConfig().typescript) { + extension.replace( + Regex("\\.ts$"), + ".js" + ) + } else extension + } + override fun adaptFileToConfig(contents: String): String { val config = getLocalConfig() return contents.replace(