Skip to content

Commit

Permalink
clean up, fixes from review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wclr committed Jan 11, 2025
1 parent 33206e9 commit 64473cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
18 changes: 10 additions & 8 deletions backend-es/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 =
Expand Down Expand Up @@ -382,25 +382,27 @@ 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
, shareStream Process.stderr
]
, 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
| otherwise -> k (Right unit)
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
Expand Down
8 changes: 4 additions & 4 deletions backend-es/test/Utils.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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)
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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can edit this file as you like.
, "node-process"
, "node-streams"
, "now"
, "nullable"
, "numbers"
, "ordered-collections"
, "orders"
Expand Down

0 comments on commit 64473cf

Please sign in to comment.