Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix package installation for yarn, add support for bun.lock #51

Merged
merged 4 commits into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageManager>().firstOrNull { packageManager ->
packageManager.getLockFileName().any { lockFile ->
fileManager.getVirtualFilesByName(lockFile).isNotEmpty()
}
}
}

fun installDependencies(dependencyNames: List<String>, 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()
Expand All @@ -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()
Expand Down
Loading