From 38bc0f25d9fab730aa399c6cb025fc4c14ee857c Mon Sep 17 00:00:00 2001 From: Shimizu Izumi Date: Sat, 22 Jun 2024 21:15:38 +0200 Subject: [PATCH] Fix package installation for yarn, add support for `bun.lock` (#51) Co-authored-by: Antoine Lethimonnier <43064022+WarningImHack3r@users.noreply.github.com> --- .../backend/helpers/DependencyManager.kt | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 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 ade9544..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 @@ -23,24 +23,40 @@ class DependencyManager(private val project: Project) { PROD } - private fun getPackageManager(): String? { + enum class PackageManager(val command: String) { + NPM("npm"), + PNPM("pnpm"), + YARN("yarn"), + BUN("bun"); + + fun getLockFileName() = when (this) { + NPM -> listOf("package-lock.json") + PNPM -> listOf("pnpm-lock.yaml") + YARN -> listOf("yarn.lock") + BUN -> listOf("bun.lockb", "bun.lock") + } + + fun getInstallCommand() = when (this) { + YARN -> "add" + else -> "i" + } + } + + private fun getPackageManager(): PackageManager? { 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() + return enumValues().firstOrNull { packageManager -> + packageManager.getLockFileName().any { lockFile -> + fileManager.getVirtualFilesByName(lockFile).isNotEmpty() + } + } } fun installDependencies(dependencyNames: List, installationType: InstallationType = InstallationType.PROD) { getPackageManager()?.let { packageManager -> // install the dependency val command = listOfNotNull( - packageManager, - "i", + packageManager.command, + packageManager.getInstallCommand(), if (installationType == InstallationType.DEV) "-D" else null, *dependencyNames.toTypedArray() ).toTypedArray() @@ -59,7 +75,7 @@ class DependencyManager(private val project: Project) { getPackageManager()?.let { packageManager -> // uninstall the dependencies val command = listOf( - packageManager, + packageManager.command, "remove", *dependencyNames.toTypedArray() ).toTypedArray()