@@ -13,20 +13,15 @@ import Data.Array as Array
1313import Data.Array.NonEmpty as NonEmptyArray
1414import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
1515import Data.Either (Either(..))
16−import Data.Posix.Signal (Signal(..))
17−import Effect.Aff (Aff, effectCanceler, error, makeAff, throwError)
16+import Data.Maybe (Maybe(..))
17+import Effect.Aff (Aff, effectCanceler, error, makeAff, message, throwError)
1818import Effect.Class (liftEffect)
19−import Node.Buffer (Buffer, freeze)
20−import Node.Buffer.Immutable as ImmutableBuffer
21−import Node.ChildProcess (ExecResult, Exit(..), defaultExecOptions, defaultSpawnOptions, inherit)
22−import Node.ChildProcess as ChildProcess
23−import Node.Encoding (Encoding(..))
19+import Node.EventEmitter (on_)
2420import Node.FS.Aff as FS
25−import Node.FS.Perms (mkPerms)
2621import Node.FS.Perms as Perms
2722import Node.FS.Stats as Stats
2823import Node.FS.Stream (createReadStream, createWriteStream)
29−import Node.Library.Execa (ExecaError, ExecaSuccess, execa)
24+import Node.Library.Execa (ExecaResult, execa)
3025import Node.Path (FilePath)
3126import Node.Process as Process
3227import Node.Stream as Stream
@@ -35,29 +30,22 @@ import PureScript.CST.Errors (printParseError)
3530import PureScript.CST.Types as CSTT
3631
3732spawnFromParent :: String -> Array String -> Aff Unit
38−spawnFromParent command args = makeAff \k -> do
39− childProc <- ChildProcess.spawn command args defaultSpawnOptions { stdio = inherit }
40− ChildProcess.onExit childProc case _ of
41− Normally code
42− | code > 0 -> Process.exit code
43− | otherwise -> k (Right unit)
44− BySignal _ ->
45− Process.exit 1
46− pure $ effectCanceler do
47− ChildProcess.kill SIGABRT childProc
33+spawnFromParent command args = do
34+ spawned <- execa command args identity
35+ spawned.stdout.pipeToParentStdout
36+ spawned.stderr.pipeToParentStderr
37+ result <- spawned.getResult
38+ unless (result.exitCode == Just 0) do
39+ liftEffect $ Process.exit' 1
4840
49−execWithStdin :: String -> String -> Aff ExecResult
50−execWithStdin command input = makeAff \k -> do
51− childProc <- ChildProcess.exec command defaultExecOptions (k <<< pure)
52− _ <- Stream.writeString (ChildProcess.stdin childProc) UTF8 input mempty
53− Stream.end (ChildProcess.stdin childProc) mempty
54− pure $ effectCanceler $ ChildProcess.kill SIGABRT childProc
55−
56−bufferToUTF8 :: Buffer -> Aff String
57−bufferToUTF8 = liftEffect <<< map (ImmutableBuffer.toString UTF8) <<< freeze
41+execWithStdin :: String -> Array String -> String -> Aff ExecaResult
42+execWithStdin command args input = do
43+ spawned <- execa command args identity
44+ spawned.stdin.writeUtf8End input
45+ spawned.getResult
5846
5947mkdirp :: FilePath -> Aff Unit
60−mkdirp path = FS.mkdir' path { recursive: true, mode: mkPerms Perms.all Perms.all Perms.all }
48+mkdirp path = FS.mkdir' path { recursive: true, mode: Perms.permsAll }
6149
6250loadModuleMain
6351 :: { libdir :: FilePath
@@ -66,7 +54,7 @@ loadModuleMain
6654 , moduleName :: String
6755 , modulePath :: String
6856 }
69− -> Aff (Either ExecaError ExecaSuccess)
57+ -> Aff ExecaResult
7058loadModuleMain options = do
7159 let
7260 arguments :: Array String
@@ -81,9 +69,9 @@ loadModuleMain options = do
8169 [ "(base-exception-handler (lambda (e) (display-condition e (console-error-port)) (newline (console-error-port)) (exit -1)))"
8270 , "(top-level-program (import (" <> options.moduleName <> " lib)) (main))"
8371 ]
84− void $ liftEffect $ Stream.pipe spawned.stdout.stream Process.stdout
85− void $ liftEffect $ Stream.pipe spawned.stderr.stream Process.stderr
86− spawned.result
72+ spawned.stdout.pipeToParentStdout
73+ spawned.stderr.pipeToParentStderr
74+ spawned.getResult
8775
8876copyFile :: FilePath -> FilePath -> Aff Unit
8977copyFile from to = do
@@ -93,13 +81,12 @@ copyFile from to = do
9381 makeAff \k -> do
9482 src <- createReadStream from
9583 dst <- createWriteStream to
96− res <- Stream.pipe src dst
97− Stream.onError src (k <<< Left)
98− Stream.onError dst (k <<< Left)
99− Stream.onError res (k <<< Left)
100− Stream.onFinish res (k (Right unit))
84+ src # on_ Stream.errorH \err -> do
85+ Stream.destroy' dst $ error $ "Got error in src stream: " <> message err
86+ k $ Left err
87+ dst # on_ Stream.errorH (k <<< Left)
88+ Stream.pipe src dst
10189 pure $ effectCanceler do
102− Stream.destroy res
10390 Stream.destroy dst
10491 Stream.destroy src
10592