@@ -43,7 +43,8 @@ import javax.swing.JMenu
4343class p5jsEditor (base : Base , path : String? , state : EditorState ? , mode : Mode ? ): Editor(base, path, state, mode) {
4444
4545 val scope = CoroutineScope (Dispatchers .Default )
46- val SHELL = System .getenv(" SHELL" )
46+ val isWindows = System .getProperty(" os.name" ).lowercase().contains(" windows" )
47+ val shell = System .getenv(" SHELL" )
4748
4849 init {
4950 scope.launch {
@@ -88,21 +89,26 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?): E
8889 statusNotice(" Looking for pnpm…" )
8990 try {
9091 // TODO: Only an interactive shell allows me access to pnpm
91- runCommand(SHELL , listOf ( " -ci " , " pnpm -v" ) )
92+ runCommand(" pnpm -v" )
9293 }
9394 catch (e: Exception ) {
9495 statusNotice(" pnpm not found. Installing pnpm…" )
95- runCommand(" chmod" , listOf (" u+x" , " ${mode?.folder} /install.sh" ))
96- runCommand(SHELL , listOf (" -ci" , " ${mode?.folder} /install.sh" ))
96+ if (isWindows) {
97+ runCommand(" powershell -command \" Invoke-WebRequest https://get.pnpm.io/install.ps1 -UseBasicParsing | Invoke-Expression\" " )
98+ }
99+ else {
100+ runCommand(" chmod u+x ${mode?.folder} /install.sh" )
101+ runCommand(" ${mode?.folder} /install.sh" )
102+ }
97103
98104 statusNotice(" Installing Node via pnpm…" )
99- runCommand(SHELL , listOf ( " -ci " , " pnpm env use --global lts" ) , onFinished = {
105+ runCommand(" pnpm env use --global lts" , onFinished = {
100106 statusNotice(" Installing Node dependencies…" )
101107 })
102108 }
103109
104110 // --dangerously-allow-all-builds allows electron in particular to install properly
105- runCommand(SHELL , listOf ( " -ci " , " pnpm install --dangerously-allow-all-builds" ) , onFinished = {
111+ runCommand(" pnpm install --dangerously-allow-all-builds" , onFinished = {
106112 statusNotice(" All done! Enjoy p5.js mode." )
107113 })
108114 }
@@ -192,7 +198,7 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?): E
192198 Button (onClick = {
193199 if (packageToInstall.isNotBlank()) {
194200 // TODO Better error handling
195- runCommand(SHELL , listOf ( " -ci " , " pnpm add $packageToInstall --dangerously-allow-all-builds" ) )
201+ runCommand(" pnpm add $packageToInstall --dangerously-allow-all-builds" )
196202 packageToInstall = " "
197203 }
198204 }) {
@@ -220,16 +226,17 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?): E
220226
221227 // TODO: state is maintained => turn into class
222228 val processes = mutableListOf<Process >()
223- fun runCommand (type : String , actions : List < String > , directory : File = sketch.folder, onFinished : () -> Unit = {}) {
229+ fun runCommand (action : String , directory : File = sketch.folder, onFinished : () -> Unit = {}) {
224230 // Wait for previous processes to finish
225231 processes.forEach { it.waitFor() }
226232
227233 val processBuilder = ProcessBuilder ()
234+
228235 // Set the command based on the operating system
229- val command = if (System .getProperty( " os.name " ).lowercase().contains( " windows " ) ) {
230- listOf (" cmd" , " /c" , type, * actions.toTypedArray() )
236+ val command = if (isWindows ) {
237+ listOf (" cmd" , " /c" , action )
231238 } else {
232- listOf (type, * actions.toTypedArray() )
239+ listOf (shell, " -ci " , action )
233240 }
234241
235242 processBuilder.command(command)
@@ -246,7 +253,7 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?): E
246253 while (reader.readLine().also { line = it } != null ) {
247254 // TODO: so much refactoring!
248255 // Only check for errors when running the sketch
249- if (actions[ 1 ] .startsWith(" npx" ) && line.startsWith(" error" )) {
256+ if (action .startsWith(" npx" ) && line.startsWith(" error" )) {
250257 // TODO: more robust data exchange, double-check with @Stef
251258 // TODO: `statusError` does not do anything with column of a SketchException
252259 val ( msgType, msgText, msgFile, msgLine, msgCol ) = line.split(" |" )
@@ -262,10 +269,10 @@ class p5jsEditor(base: Base, path: String?, state: EditorState?, mode: Mode?): E
262269 processes.remove(process)
263270 onFinished()
264271 if (exitCode != 0 ) {
265- throw RuntimeException (" $type ${actions.joinToString( " " )} failed with exit code $exitCode ." )
272+ throw RuntimeException (" $action failed with exit code $exitCode ." )
266273 }
267274 } catch (e: Exception ) {
268- throw RuntimeException (" Failed to run $type ${actions.joinToString( " " )} ." , e)
275+ throw RuntimeException (" Failed to run $action ." , e)
269276 }
270277 }
271278}
0 commit comments