Parcourir la source

Use node-execa instead for fun and profit

jherve il y a 1 an
Parent
commit
92216eb57c
2 fichiers modifiés avec 15 ajouts et 73 suppressions
  1. 1 0
      spago.dhall
  2. 14 73
      src/Main.purs

+ 1 - 0
spago.dhall

@@ -27,6 +27,7 @@ You can edit this file as you like.
   , "maybe"
   , "node-buffer"
   , "node-child-process"
+  , "node-execa"
   , "node-event-emitter"
   , "node-fs"
   , "node-os"

+ 14 - 73
src/Main.purs

@@ -2,83 +2,24 @@ module Main where
 
 import Prelude
 
-import Data.Either (Either(..))
-import Data.Maybe (Maybe(..))
 import Effect (Effect)
-import Effect.Aff (Aff, effectCanceler, launchAff_, makeAff)
-import Effect.Class (liftEffect)
-import Effect.Class.Console (log, logShow)
-import Effect.Exception (throw, throwException)
-import Node.Buffer (toString)
-import Node.ChildProcess (ExecResult, connected, exitCode, spawn, stdout)
-import Node.ChildProcess as ChildProcess
-import Node.ChildProcess.Aff (waitSpawned)
+import Effect.Aff (Aff, launchAff_)
+import Effect.Class.Console (log)
 import Node.ChildProcess.Types (Exit(..))
-import Node.Encoding (Encoding(..))
-import Node.Errors.SystemError (code, message)
-import Node.EventEmitter (EventHandle, once)
-import Node.Stream (read)
-import Unsafe.Coerce (unsafeCoerce)
+import Node.Library.Execa (execa)
 
 
 main :: Effect Unit
 main = do
   log "hello"
-  _ <- ChildProcess.exec' "pwd" identity readExec
-  _ <- ChildProcess.exec' "touch /test.log" identity readExec
-
-  execCmd "pwd"
-  execCmd "touch /test.log"
-
-  launchAff_ spawnLs
-
-  pure unit
-
-readExec ∷ ExecResult → Effect Unit
-readExec {error: Nothing, stdout, stderr} = do
-  log "OK !"
-  out <- toString UTF8 stdout
-  log out
-
-readExec {error: Just err, stdout, stderr} = do
-  log $ "error ! [" <> code err <> "], " <> message err
-
-execCmd str = do
-  cp <- ChildProcess.exec str
-  exit <- exitCode cp
-  case exit of
-    Nothing -> log "exit with Nothing"
-    Just 0 -> log "exit with 0"
-    Just i -> log $ "exit with " <> show i
-
-spawnLs :: Aff Unit
-spawnLs = do
-  log "\nspawns processes ok"
-  ls <- liftEffect $ spawn "ls" [ "-la" ]
-  res <- waitSpawned ls
-  case res of
-    Right pid -> log $ "ls successfully spawned with PID: " <> show pid
-    Left err -> liftEffect $ throwException $ unsafeCoerce err
-  exit <- until ls ChildProcess.closeH \complete -> \exit -> complete exit
-  case exit of
-    Normally 0 -> do 
-      log $ "ls exited with 0"
-      out <- liftEffect $ read $ stdout ls
-      case out of 
-        Nothing -> log "Could not get stdout"
-        Just out' -> do
-          outStr <- liftEffect $ toString UTF8 out'
-          logShow outStr
-      
-    Normally i -> liftEffect $ throw $ "ls had non-zero exit: " <> show i
-    BySignal sig -> liftEffect $ throw $ "ls exited with sig: " <> show sig
-
-until
-  :: forall emitter psCb jsCb a
-   . emitter
-  -> EventHandle emitter psCb jsCb
-  -> ((a -> Effect Unit) -> psCb)
-  -> Aff a
-until ee event cb = makeAff \done -> do
-  rm <- ee # once event (cb (done <<< Right))
-  pure $ effectCanceler rm
+  launchAff_ $ runCommand "pwd" []
+
+runCommand ∷ String → Array String → Aff Unit
+runCommand cmd args = do
+    process <- execa cmd args identity
+    result <- process.getResult
+    case result.exit of
+      Normally 0 -> do
+        log "worked"
+        log result.stdout
+      _ -> log "failed"