[skip ci] npm by nvm, shadow-cljs/node tool chain, tests successful

This commit is contained in:
Mirco 2025-01-30 22:32:37 +01:00
parent 9a80e79778
commit 5e70cad41c
7 changed files with 81 additions and 26 deletions
src
main/kotlin/org/domaindrivenarchitecture/provs
desktop
framework/ubuntu/install/base
test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure
testFixtures/kotlin/org/domaindrivenarchitecture/provs/test

View file

@ -157,6 +157,7 @@ fun Prov.provisionIdeDesktop() {
installDocker()
aptInstall(JAVA)
aptInstall(CLOJURE_TOOLS)
installNpmByNvm()
installShadowCljs()
installDevOps()
provisionPython()

View file

@ -6,28 +6,10 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackag
fun Prov.installShadowCljs(): ProvResult = task {
if (!isPackageInstalled("npm")) {
// installation of npm is too chatty even with quite install and will hang when using Java ProcessBuilder => install with output must be ignored
optional {
// may fail for some packages, but this should in general not be an issue
cmd("sudo apt-get update")
}
cmd("sudo apt-get install -qy apt-utils")
cmd("sudo DEBIAN_FRONTEND=noninteractive apt-get install -qy npm > /dev/null")
cmd("sudo npm install -g npx")
cmd("sudo npm install -g shadow-cljs")
if (!isPackageInstalled("shadow-cljs")) {
cmd(". .nvm/nvm.sh && npm install -g shadow-cljs")
cmd(". .nvm/nvm.sh && shadow-cljs --help")
} else {
val npmVersion = cmd("npm --version")
addResultToEval(ProvResult(true, out = "Package npm v$npmVersion already installed. Checking shadow-cljs now."))
if (chk("npm list -g shadow-cljs | grep empty")) {
cmd("sudo npm install -g shadow-cljs")
} else {
addResultToEval(ProvResult(true, out = "Package shadow-cljs already installed."))
}
if (chk("npm list -g npx|grep empty")) {
cmd("sudo npm install -g npx")
} else {
addResultToEval(ProvResult(true, out = "Package npx already installed."))
}
ProvResult(true, out = "shadow-cljs already installed")
}
}

View file

@ -0,0 +1,40 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.addTextToFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackageInstalledCheckCommand
import java.io.File
fun Prov.installNpmByNvm(): ProvResult = task {
if (!isPackageInstalledCheckCommand("npm")) {
//Node-Version-Manager from https://github.com/nvm-sh/nvm
val versNvm = "0.40.1"
cmd("sudo curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v$versNvm/install.sh | bash")
cmd("chmod 755 .nvm/nvm.sh")
addTextToFile("\n##### NVM #####\n", File("~/.bashrc"))
addTextToFile("""export NVM_DIR="${"\$HOME/.nvm"}" """ + "\n", File("~/.bashrc"))
addTextToFile("""[ -s "${"\$NVM_DIR/nvm.sh"}" ] && \. "${"\$NVM_DIR/nvm.sh"}" """ + "\n", File("~/.bashrc"))
addTextToFile("""[ -s "${"\$NVM_DIR/bash_completion"}" ] && \. "${"\$NVM_DIR/bash_completion"}" """ + "\n", File("~/.bashrc"))
cmd(". .nvm/nvm.sh && nvm install --lts")
//to be discussed, sourcing in docker test container, separtely?
val nvmRes = cmd(". .nvm/nvm.sh && nvm --version").toString()
if (versNvm == nvmRes) {
println("NVM version $versNvm")
addResultToEval(ProvResult(true, out = "SUCCESS: NVM version $versNvm installed !!"))
} else {
println("FAIL: NVM version $versNvm is not installed !!")
addResultToEval(ProvResult(true, out = "FAIL: NVM version $versNvm is not installed !!"))
}
cmd(". .nvm/nvm.sh && node -v")
cmd(". .nvm/nvm.sh && npm --version")
} else {
ProvResult(true, out = "npm already installed")
}
}

View file

@ -22,16 +22,17 @@ fun Prov.aptInstall(packages: String, ignoreAlreadyInstalled: Boolean = true): P
if (!aptInit) {
optional {
// may fail for some packages, but this should in general not be an issue
cmd("sudo apt-get update")
cmd("sudo apt-get update -q=2 && sudo apt-get upgrade -q=2")
}
cmd("sudo apt-get install -qy apt-utils")
cmd("sudo apt-get install -q=2 apt-utils")
aptInit = true
}
}
for (packg in packageList) {
// see https://superuser.com/questions/164553/automatically-answer-yes-when-using-apt-get-install
cmd("sudo DEBIAN_FRONTEND=noninteractive apt-get install -qy $packg")
cmd("sudo apt-get install -q=2 $packg")
//DEBIAN_FRONTEND=noninteractive
}
ProvResult(true) // dummy
} else {
@ -82,6 +83,9 @@ fun Prov.checkPackageInstalled(packageName: String): ProvResult = taskWithResult
cmd("dpkg -s $packageName")
}
fun Prov.isPackageInstalledCheckCommand(packageName: String): Boolean {
return chk("command -v $packageName")
}
/**
* Removes a package including its configuration and data file

View file

@ -1,5 +1,6 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerStartMode
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.assertTrue
@ -11,6 +12,8 @@ internal class ClojureScriptKtTest {
// given
val prov = defaultTestContainer()
prov.installNpmByNvm()
// when
val res = prov.installShadowCljs()
val res2 = prov.installShadowCljs() // check if it can be run twice successfully

View file

@ -0,0 +1,25 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerStartMode
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
internal class NVMnpmKtTest {
@Test
fun installNVMnpm() {
// given
val container = defaultTestContainer(ContainerStartMode.CREATE_NEW_KILL_EXISTING)
container.aptInstall("curl")
// when
val res01 = container.installNpmByNvm()
//test repeatability
val res02 = container.installNpmByNvm()
// then
assertTrue(res01.success)
assertTrue(res02.success)
}
}

View file

@ -41,7 +41,7 @@ private fun initDefaultTestContainer(startMode: ContainerStartMode): Prov {
containerProv.sh("""
sudo apt-get update
sudo apt-get upgrade -qq
sudo apt-get upgrade -q=2
""".trimIndent())
return containerProv