From 64473cf1a6b743d6999bf80067a279ae6cbc0ef9 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 11 Jan 2025 19:44:21 +0500 Subject: [PATCH] clean up, fixes from review comments --- backend-es/src/Main.purs | 18 ++++++++++-------- backend-es/test/Utils.purs | 8 ++++---- spago.dhall | 1 + 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/backend-es/src/Main.purs b/backend-es/src/Main.purs index 15fd2b2..053c2d7 100644 --- a/backend-es/src/Main.purs +++ b/backend-es/src/Main.purs @@ -17,8 +17,10 @@ import Data.Map as Map import Data.Maybe (Maybe(..), fromMaybe, maybe) import Data.Monoid (guard, power) import Data.Newtype (over2, unwrap) +import Data.Nullable (toMaybe) import Data.Number.Format as Number import Data.Ord.Down (Down(..)) +import Data.Posix.Signal (Signal(..)) import Data.Set (Set) import Data.Set as Set import Data.String (Pattern(..), Replacement(..)) @@ -35,9 +37,7 @@ import Effect.Class.Console as Console import Effect.Now (now) import Effect.Ref (Ref) import Effect.Ref as Ref -import Node.ChildProcess (exitH) -import Node.ChildProcess as ChildProcess -import Node.ChildProcess.Types (Exit(..), enableShell, pipe, shareStream, stringSignal) +import Node.ChildProcess.Types (Exit(..), enableShell, pipe, shareStream) import Node.Encoding (Encoding(..)) import Node.EventEmitter (on_) import Node.FS.Aff (writeTextFile) @@ -50,6 +50,7 @@ import Node.Path as Path import Node.Process as Process import Node.Stream (errorH, finishH) import Node.Stream as Stream +import Node.UnsafeChildProcess.Safe (exitH, killSignal) import Node.UnsafeChildProcess.Unsafe as UnsafeChildProcess import PureScript.Backend.Optimizer.Codegen.EcmaScript (codegenModule, esModulePath) import PureScript.Backend.Optimizer.Codegen.EcmaScript.Builder (basicBuildMain, externalDirectivesFromFile) @@ -62,7 +63,6 @@ import PureScript.CST.Lexer (lexToken) import PureScript.CST.Lexer as Lexer import PureScript.CST.Types (Token(..)) import PureScript.CST.Types as CST -import Unsafe.Coerce (unsafeCoerce) import Version as Version type BuildArgs = @@ -382,7 +382,7 @@ esModulePackageJson = """{"type": "module"}""" spawnFromParentWithStdin :: String -> Array String -> Maybe String -> Aff Unit spawnFromParentWithStdin command args input = makeAff \k -> do -- To preserve colors in stdout need to pass it into spawn's stdio. - childProc <- unsafeCoerce <$> UnsafeChildProcess.spawn' command args + childProc <- UnsafeChildProcess.spawn' command args { stdio: [ pipe , shareStream Process.stdout @@ -390,9 +390,11 @@ spawnFromParentWithStdin command args input = makeAff \k -> do ] , shell: enableShell } + for_ input \inp -> do - _ <- Stream.writeString (ChildProcess.stdin childProc) UTF8 inp - Stream.end (ChildProcess.stdin childProc) + for_ (toMaybe $ UnsafeChildProcess.unsafeStdin childProc) \stdin -> do + _ <- Stream.writeString stdin UTF8 inp + Stream.end stdin childProc # on_ exitH case _ of Normally code | code > 0 -> Process.exit' code @@ -400,7 +402,7 @@ spawnFromParentWithStdin command args input = makeAff \k -> do BySignal _ -> Process.exit' 1 pure $ effectCanceler do - void $ ChildProcess.kill' (stringSignal "SIGABRT") childProc + void $ killSignal SIGABRT childProc timeDiff :: Instant -> Instant -> Milliseconds timeDiff = over2 Milliseconds (flip (-)) `on` Instant.unInstant diff --git a/backend-es/test/Utils.purs b/backend-es/test/Utils.purs index c871827..01746c4 100644 --- a/backend-es/test/Utils.purs +++ b/backend-es/test/Utils.purs @@ -4,6 +4,7 @@ import Prelude import Data.Either (Either(..)) import Data.Maybe (Maybe(..)) +import Data.Posix.Signal (Signal(..)) import Effect (Effect) import Effect.Aff (Aff, Error, effectCanceler, error, makeAff, throwError) import Effect.Class (liftEffect) @@ -11,7 +12,7 @@ import Node.Buffer (Buffer, freeze) import Node.Buffer.Immutable as ImmutableBuffer import Node.ChildProcess (ChildProcess, ExecResult, exitH) import Node.ChildProcess as ChildProcess -import Node.ChildProcess.Types (Exit(..), stringSignal) +import Node.ChildProcess.Types (Exit(..)) import Node.Encoding (Encoding(..)) import Node.EventEmitter (on_) import Node.FS.Aff as FS @@ -34,15 +35,14 @@ spawnFromParent command args = makeAff \k -> do BySignal _ -> Process.exit' 1 pure $ effectCanceler do - void $ ChildProcess.kill' (stringSignal "SIGABRT") childProc + void $ ChildProcess.killSignal SIGABRT childProc execWithStdin :: String -> String -> Aff ExecResult execWithStdin command input = makeAff \k -> do childProc <- ChildProcess.exec' command identity (k <<< pure) _ <- Stream.writeString' (ChildProcess.stdin childProc) UTF8 input mempty - --Stream.end (ChildProcess.stdin childProc) mempty (ChildProcess.stdin childProc) # on_ finishH mempty - pure $ effectCanceler $ void $ ChildProcess.kill' (stringSignal "SIGABRT") childProc + pure $ effectCanceler $ void $ ChildProcess.killSignal SIGABRT childProc bufferToUTF8 :: Buffer -> Aff String bufferToUTF8 = liftEffect <<< map (ImmutableBuffer.toString UTF8) <<< freeze diff --git a/spago.dhall b/spago.dhall index 888c713..ee3a532 100644 --- a/spago.dhall +++ b/spago.dhall @@ -37,6 +37,7 @@ You can edit this file as you like. , "node-process" , "node-streams" , "now" + , "nullable" , "numbers" , "ordered-collections" , "orders"