Skip to content

Commit 07e5292

Browse files
committed
Fix auto-install of pnpm and node on mode init
1 parent 55f8746 commit 07e5292

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

p5js/src/main/kotlin/p5jsEditor.kt

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,55 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?) :
6363

6464
createIndexHtml()
6565

66-
// TODO: refactor into functions, pick up crucial information from stdout
67-
statusNotice("Looking for pnpm…")
66+
val stackedStatusNotice = createStackedStatusNotice()
67+
stackedStatusNotice("Looking for pnpm…")
6868
try {
69-
runCommand("pnpm -v")
69+
val pnpmVersion = probeCommand("pnpm -v")
70+
stackedStatusNotice("Found $pnpmVersion.")
7071
} catch (e: Exception) {
71-
statusNotice("pnpm not found. Installing pnpm…")
72+
stackedStatusNotice("Not found. Installing pnpm…")
7273
if (isWindows) {
7374
runCommand("powershell -command \"Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression\"")
7475
} else {
7576
runCommand("chmod u+x ${mode?.folder}/install.sh")
7677
runCommand("${mode?.folder}/install.sh")
7778
}
79+
stackedStatusNotice("Done.")
80+
}
81+
82+
stackedStatusNotice("Looking for node…")
83+
try {
84+
val nodeVersion = probeCommand("node -v")
85+
stackedStatusNotice("Found $nodeVersion.")
86+
} catch (e: Exception) {
87+
stackedStatusNotice("Not found. Installing node via pnpm…")
7888

79-
statusNotice("Installing Node via pnpm…")
8089
runCommand("pnpm env use --global lts")
90+
91+
stackedStatusNotice("Done.")
8192
}
8293

83-
statusNotice("All done! Enjoy p5.js mode.")
94+
stackedStatusNotice("Enjoy p5.js mode!")
95+
}
96+
}
97+
98+
fun probeCommand(command: String): String {
99+
val process = builder(command).start()
100+
val exitCode = process.waitFor()
101+
102+
if (exitCode != 0) {
103+
throw RuntimeException("Command failed with non-zero exit code $exitCode.")
104+
}
105+
106+
val output = process.inputStream.bufferedReader().use { it.readText() }
107+
return output
108+
}
109+
110+
fun createStackedStatusNotice(): (String) -> Unit {
111+
var statusText = mutableListOf<String>()
112+
return fun(text: String) {
113+
statusText.add(text)
114+
statusNotice(statusText.joinToString(" "))
84115
}
85116
}
86117

@@ -102,8 +133,7 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?) :
102133
if (sketch.isUntitled || sketch.isReadOnly) {
103134
Messages.showMessage("Save First", "Please first save the sketch.");
104135
} else {
105-
// TODO: I’m sure this is not the best way to ensure that this runs async, so the ActionListener can return
106-
// but works for now
136+
// TODO: I’m sure this is not the best way to ensure that this runs async, so the ActionListener can return but works for now
107137
scope.launch {
108138
handleExport()
109139
}
@@ -336,7 +366,6 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?) :
336366
}
337367
}
338368

339-
340369
fun runCommand(action: String, directory: File = sketch.folder) {
341370
try {
342371
val processBuilder = builder(action, directory)
@@ -358,15 +387,14 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?) :
358387
}
359388
}
360389

361-
private fun builder(action: String, directory: File): ProcessBuilder {
390+
private fun builder(action: String, directory: File = sketch.folder): ProcessBuilder {
362391
val processBuilder = ProcessBuilder()
363392

364393
// Set the command based on the operating system
365-
val shell = System.getenv("SHELL")
366394
val command = if (isWindows) {
367395
listOf("cmd", "/c", action)
368396
} else {
369-
listOf(shell, "-ci", action)
397+
listOf(System.getenv("SHELL"), "-ci", action)
370398
}
371399

372400
processBuilder.command(command)

0 commit comments

Comments
 (0)