@@ -3,77 +3,32 @@ module Main exposing (main)
3
3
import Node
4
4
import Bytes exposing (Bytes)
5
5
import Stream exposing (Stream)
6
- import Node exposing (Program, Environment)
6
+ import Node exposing (Environment)
7
7
import FileSystem
8
+ import FileSystem.Path as Path
8
9
import Init
9
10
import Task
10
11
11
12
12
- main : Program Model Msg
13
+ main : Node.SimpleProgram a
13
14
main =
14
- Node.defineProgram
15
- { init = init
16
- , update = update
17
- , subscriptions = \_ -> Sub.none
18
- }
15
+ Node.defineSimpleProgram init
19
16
20
17
21
- type alias Model =
22
- { stdout : Stream
23
- , stderr : Stream
24
- }
25
-
26
-
27
- type Msg
28
- = OpenResult (Result FileSystem.AccessError (FileSystem.ReadableFileHandle Never))
29
- | ReadResult (Result FileSystem.UnknownFileSystemError Bytes)
30
-
31
- init : Environment -> Init.Task { model : Model, command : Cmd Msg }
18
+ init : Environment -> Init.Task (Cmd a)
32
19
init env =
33
20
Init.await FileSystem.initialize <| \fsPermission ->
34
- Node.startProgram
35
- { model =
36
- { stdout = env.stdout
37
- , stderr = env.stderr
38
- }
39
- , command =
40
- case env.args of
41
- [ _, _, file ] ->
42
- FileSystem.openForRead fsPermission file
43
- |> Task.attempt OpenResult
44
-
45
- _ ->
46
- Stream.sendLine env.stderr <|
47
- "Exactly one argument is required: the file name to read"
48
- }
49
-
50
- update : Msg -> Model -> { model : Model, command : Cmd Msg }
51
- update msg model =
52
- case msg of
53
- OpenResult (Ok fh) ->
54
- { model = model
55
- , command =
56
- FileSystem.read fh
57
- |> Task.attempt ReadResult
58
- }
59
-
60
- OpenResult (Err error) ->
61
- { model = model
62
- , command =
63
- Stream.sendLine model.stderr <|
64
- "Failed to open file"
65
- }
66
-
67
- ReadResult (Ok bytes) ->
68
- { model = model
69
- , command =
70
- Stream.send model.stdout bytes
71
- }
72
-
73
- ReadResult (Err error) ->
74
- { model = model
75
- , command =
76
- Stream.sendLine model.stderr <|
77
- "Failed to read file"
78
- }
79
-
21
+ case env.args of
22
+ [ _, _, file ] ->
23
+ FileSystem.readFile fsPermission (Path.fromPosixString file)
24
+ |> Task.map (Bytes.toString >> Maybe.withDefault "")
25
+ |> Task.onError (Task.succeed << FileSystem.errorToString)
26
+ |> Task.andThen (Stream.sendLine env.stdout)
27
+ |> Task.execute
28
+ |> Node.endWithCmd
29
+
30
+ _ ->
31
+ "Exactly one argument is required: the file name to read"
32
+ |> Stream.sendLine env.stderr
33
+ |> Task.execute
34
+ |> Node.endWithCmd
0 commit comments