Skip to content

Commit 3e18c92

Browse files
committed
Split up run and encodeCommand operations.
1 parent 9a09746 commit 3e18c92

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/Compiler/Backend.gren

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Compiler.Backend exposing
99
, DocsFlags
1010
, DocsOutput(..)
1111
, DiffArgs(..)
12+
, encodeCommand
1213
, run
1314
--
1415
, UnsupportedPlatform(..)
@@ -25,7 +26,7 @@ module Compiler.Backend exposing
2526

2627
@docs UnsupportedPlatform, downloadUrl, download, cachePath, isCached
2728

28-
@docs Command, InitFlags, ReplFlags, MakeFlags, MakeOutput, DocsFlags, DocsOutput, DiffArgs, Platform, run
29+
@docs Command, InitFlags, ReplFlags, MakeFlags, MakeOutput, DocsFlags, DocsOutput, DiffArgs, Platform, encodeCommand, run
2930

3031
-}
3132

@@ -257,9 +258,7 @@ type DiffArgs
257258

258259
{-|-}
259260
type alias RunOptions msg =
260-
{ command : Command
261-
, interactiveSession : Bool
262-
, useColor : Bool
261+
{ useColor : Bool
263262
, compilerPath : Path
264263
, pathToString : (Path -> String)
265264
, onInit : { processId : Process.Id, streams : Maybe ChildProcess.StreamIO } -> msg
@@ -269,35 +268,36 @@ type alias RunOptions msg =
269268

270269
{-| Execute the compiler backend. The backend will write to stdout and stderr. There's currently no way
271270
to redirect what is written to these streams.
272-
273-
The `encodedCommand` should be fed to the backend's stdin stream on initialization.
274271
-}
275-
run : ChildProcess.Permission -> RunOptions msg -> { encodedCommand : String, command : Cmd msg }
272+
run : ChildProcess.Permission -> RunOptions msg -> Cmd msg
276273
run permission options =
277274
let
278-
commandAsJson =
279-
options.command
280-
|> commandEncoder options.interactiveSession options.pathToString
281-
|> Json.encode 0
282-
283275
colorEnvVar =
284276
if options.useColor then
285277
Dict.singleton "FORCE_COLOR" "1"
286278
else
287279
Dict.singleton "NO_COLOR" "1"
288280
in
289-
{ encodedCommand = commandAsJson
290-
, command =
291-
ChildProcess.spawn
292-
permission
293-
(options.pathToString options.compilerPath)
294-
[]
295-
{ ( ChildProcess.defaultSpawnOptions options.onInit options.onComplete )
296-
| shell = NoShell
297-
, environmentVariables =
298-
ChildProcess.MergeWithEnvironmentVariables colorEnvVar
299-
}
300-
}
281+
ChildProcess.spawn
282+
permission
283+
(options.pathToString options.compilerPath)
284+
[]
285+
{ ( ChildProcess.defaultSpawnOptions options.onInit options.onComplete )
286+
| shell = NoShell
287+
, environmentVariables =
288+
ChildProcess.MergeWithEnvironmentVariables colorEnvVar
289+
}
290+
291+
292+
{-| Encode a backend command to JSON. This should be fed to the compiler backend upon
293+
initialization.
294+
-}
295+
encodeCommand : { interactiveSession : Bool, pathToString : Path -> String } -> Command -> Bytes
296+
encodeCommand options command =
297+
command
298+
|> commandEncoder options.interactiveSession options.pathToString
299+
|> Json.encode 0
300+
|> Bytes.fromString
301301

302302

303303
commandEncoder : Bool -> (Path -> String) -> Command -> Json.Value

0 commit comments

Comments
 (0)