From fb564364dda4700998dec4765c301c9a4aef5570 Mon Sep 17 00:00:00 2001 From: Izumi Date: Sat, 22 Jun 2024 16:11:05 +0200 Subject: [PATCH 1/4] Depdency install fix: Update DependencyManager.kt to use package manager specific install command --- .../backend/helpers/DependencyManager.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt index ade9544..1b21d99 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt @@ -35,12 +35,22 @@ class DependencyManager(private val project: Project) { }.values.firstOrNull() } + private fun getInstallCommand(packageManager: String): String { + return when (packageManager) { + "npm" -> "i" + "pnpm" -> "add" + "yarn" -> "add" + "bun" -> "add" + else -> throw IllegalArgumentException("Unknown package manager: $packageManager") + } + } + fun installDependencies(dependencyNames: List, installationType: InstallationType = InstallationType.PROD) { getPackageManager()?.let { packageManager -> // install the dependency val command = listOfNotNull( packageManager, - "i", + getInstallCommand(packageManager), if (installationType == InstallationType.DEV) "-D" else null, *dependencyNames.toTypedArray() ).toTypedArray() From e4a69541e62268722c0b485e1f6e8b6151593b05 Mon Sep 17 00:00:00 2001 From: Antoine Lethimonnier <43064022+WarningImHack3r@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:48:28 +0200 Subject: [PATCH 2/4] Use enum --- .../backend/helpers/DependencyManager.kt | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt index 1b21d99..a7c3b5a 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt @@ -23,25 +23,29 @@ class DependencyManager(private val project: Project) { PROD } - private fun getPackageManager(): String? { - val fileManager = FileManager.getInstance(project) - return mapOf( - "package-lock.json" to "npm", - "pnpm-lock.yaml" to "pnpm", - "yarn.lock" to "yarn", - "bun.lockb" to "bun" - ).filter { - fileManager.getVirtualFilesByName(it.key).isNotEmpty() - }.values.firstOrNull() + enum class PackageManager(val command: String) { + NPM("npm"), + PNPM("pnpm"), + YARN("yarn"), + BUN("bun"); + + fun getLockFileName() = when (this) { + NPM -> "package-lock.json" + PNPM -> "pnpm-lock.yaml" + YARN -> "yarn.lock" + BUN -> "bun.lockb" + } + + fun getInstallCommand() = when (this) { + YARN -> "add" + else -> "i" + } } - private fun getInstallCommand(packageManager: String): String { - return when (packageManager) { - "npm" -> "i" - "pnpm" -> "add" - "yarn" -> "add" - "bun" -> "add" - else -> throw IllegalArgumentException("Unknown package manager: $packageManager") + private fun getPackageManager(): PackageManager? { + val fileManager = FileManager.getInstance(project) + return PackageManager.entries.firstOrNull { + fileManager.getVirtualFilesByName(it.getLockFileName()).isNotEmpty() } } @@ -49,8 +53,8 @@ class DependencyManager(private val project: Project) { getPackageManager()?.let { packageManager -> // install the dependency val command = listOfNotNull( - packageManager, - getInstallCommand(packageManager), + packageManager.command, + packageManager.getInstallCommand(), if (installationType == InstallationType.DEV) "-D" else null, *dependencyNames.toTypedArray() ).toTypedArray() @@ -69,7 +73,7 @@ class DependencyManager(private val project: Project) { getPackageManager()?.let { packageManager -> // uninstall the dependencies val command = listOf( - packageManager, + packageManager.command, "remove", *dependencyNames.toTypedArray() ).toTypedArray() From 8ca880dcec8a7304d7d0abbcc32914f5489e9fe0 Mon Sep 17 00:00:00 2001 From: Antoine Lethimonnier <43064022+WarningImHack3r@users.noreply.github.com> Date: Sat, 22 Jun 2024 20:52:59 +0200 Subject: [PATCH 3/4] Fix enum values --- .../intellijshadcnplugin/backend/helpers/DependencyManager.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt index a7c3b5a..30146ec 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt @@ -44,7 +44,7 @@ class DependencyManager(private val project: Project) { private fun getPackageManager(): PackageManager? { val fileManager = FileManager.getInstance(project) - return PackageManager.entries.firstOrNull { + return enumValues().firstOrNull { fileManager.getVirtualFilesByName(it.getLockFileName()).isNotEmpty() } } From 34de02124168f09b939e3807dbfa7fb3881dd341 Mon Sep 17 00:00:00 2001 From: Antoine Lethimonnier <43064022+WarningImHack3r@users.noreply.github.com> Date: Sat, 22 Jun 2024 21:05:45 +0200 Subject: [PATCH 4/4] Add support for upcoming `bun.lock` lockfile --- .../backend/helpers/DependencyManager.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt index 30146ec..76f58a8 100644 --- a/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt +++ b/src/main/kotlin/com/github/warningimhack3r/intellijshadcnplugin/backend/helpers/DependencyManager.kt @@ -30,10 +30,10 @@ class DependencyManager(private val project: Project) { BUN("bun"); fun getLockFileName() = when (this) { - NPM -> "package-lock.json" - PNPM -> "pnpm-lock.yaml" - YARN -> "yarn.lock" - BUN -> "bun.lockb" + NPM -> listOf("package-lock.json") + PNPM -> listOf("pnpm-lock.yaml") + YARN -> listOf("yarn.lock") + BUN -> listOf("bun.lockb", "bun.lock") } fun getInstallCommand() = when (this) { @@ -44,8 +44,10 @@ class DependencyManager(private val project: Project) { private fun getPackageManager(): PackageManager? { val fileManager = FileManager.getInstance(project) - return enumValues().firstOrNull { - fileManager.getVirtualFilesByName(it.getLockFileName()).isNotEmpty() + return enumValues().firstOrNull { packageManager -> + packageManager.getLockFileName().any { lockFile -> + fileManager.getVirtualFilesByName(lockFile).isNotEmpty() + } } }