Skip to content

Commit

Permalink
Add JS support for Svelte
Browse files Browse the repository at this point in the history
  • Loading branch information
WarningImHack3r committed Mar 2, 2024
1 parent 0e59e0e commit 0320904
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.nio.file.NoSuchFileException
abstract class Source<C : Config>(val project: Project, private val serializer: KSerializer<C>) {
private val log = logger<Source<C>>()
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")
Expand Down Expand Up @@ -59,10 +59,9 @@ abstract class Source<C : Config>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +29,8 @@ class SvelteSource(project: Project) : Source<SvelteConfig>(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) {
Expand All @@ -42,7 +46,8 @@ class SvelteSource(project: Project) : Source<SvelteConfig>(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"]
Expand All @@ -55,6 +60,25 @@ class SvelteSource(project: Project) : Source<SvelteConfig>(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(
Expand Down

0 comments on commit 0320904

Please sign in to comment.