Skip to content

Commit 38b5e85

Browse files
committed
Write functions for displaying errors.
1 parent 7cb4178 commit 38b5e85

File tree

5 files changed

+206
-49
lines changed

5 files changed

+206
-49
lines changed

src/Git.gren

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Git exposing
2-
( clonePackage
2+
( Error(..)
3+
, displayError
4+
, clonePackage
35
, fetchLatestVersion
46
, fetchVersions
57
, hasLocalTag
@@ -22,6 +24,7 @@ type Error
2224
| FailedCommand
2325

2426

27+
-- TODO
2528
displayError : Error -> String
2629
displayError err =
2730
""
@@ -89,13 +92,15 @@ clonePackage cpPerm repo name version =
8992
}
9093

9194

92-
fetchLatestVersion : ChildProcess.Permission -> PackageName -> Task ChildProcess.FailedRun (Maybe SemanticVersion)
95+
fetchLatestVersion : ChildProcess.Permission -> PackageName -> Task Error (Maybe SemanticVersion)
9396
fetchLatestVersion cpPerm name =
9497
fetchVersions cpPerm name
9598
|> Task.map Array.last
99+
-- TODO
100+
|> Task.mapError (\_ -> FailedCommand)
96101

97102

98-
fetchVersions : ChildProcess.Permission -> PackageName -> Task ChildProcess.FailedRun (Array SemanticVersion)
103+
fetchVersions : ChildProcess.Permission -> PackageName -> Task Error (Array SemanticVersion)
99104
fetchVersions cpPerm name =
100105
let
101106
githubUrl =
@@ -130,6 +135,8 @@ fetchVersions cpPerm name =
130135
|> Array.sortWith SemanticVersion.compare
131136
|> Task.succeed
132137
)
138+
-- TODO
139+
|> Task.mapError (\_ -> FailedCommand)
133140

134141

135142
hasLocalTag : ChildProcess.Permission -> SemanticVersion -> Task String {}

src/Terminal/Init.gren

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Terminal.Init exposing
33
, run
44
)
55

6+
import CLI.PrettyPrinter as PP
67
import ChildProcess
78
import Stream
89
import Stream.Log
@@ -37,45 +38,79 @@ type alias Config =
3738
}
3839

3940

41+
type Error
42+
= AlreadyInitialized
43+
| FileSystemFailure FileSystem.Error
44+
| PromptError Stream.Error
45+
| CouldNotRetrieveVersions { packageName : PackageName, error : Git.Error }
46+
| NoVersionFound PackageName
47+
-- TODO
48+
| InstallError
49+
50+
4051
run : Config -> Cmd a
4152
run config =
42-
FileSystem.checkAccess config.fsPermission [] (Path.fromPosixString "gren.json")
53+
let
54+
grenJsonExists =
55+
FileSystem.checkAccess config.fsPermission [] (Path.fromPosixString "gren.json")
56+
|> Task.andThen
57+
(\_ ->
58+
Task.succeed True
59+
)
60+
|> Task.onError
61+
(\err ->
62+
if FileSystem.errorIsNoSuchFileOrDirectory err then
63+
Task.succeed False
64+
65+
else
66+
Task.fail (FileSystemFailure err)
67+
)
68+
69+
in
70+
grenJsonExists
4371
|> Task.andThen
44-
(\_ ->
45-
Stream.Log.line config.stdout "File exists!"
72+
(\exists ->
73+
if exists then
74+
Task.fail AlreadyInitialized
75+
76+
else
77+
Terminal.User.confirm
78+
{ stdout = config.stdout
79+
, stdin = config.stdin
80+
, interactive = config.interactive
81+
, question =
82+
PP.verticalBlock
83+
[ PP.block
84+
[ PP.words "Hello! Gren projects always start with a"
85+
, PP.text " "
86+
, PP.color PP.Green (PP.text "gren.json")
87+
, PP.text " "
88+
, PP.words "file."
89+
]
90+
, PP.words "I can create one for you."
91+
, PP.empty
92+
, PP.words "Would you like me to create a gren.json file now?"
93+
]
94+
, defaultValue = True
95+
}
96+
|> Task.mapError PromptError
4697
)
47-
|> Task.onError
48-
(\_ ->
49-
Terminal.User.confirm
50-
{ stdout = config.stdout
51-
, stdin = config.stdin
52-
, interactive = config.interactive
53-
, question =
54-
"""
55-
Hello! Gren projects always start with a <GREEN>gren.json</> file.
56-
I can create one for you.
57-
58-
Would you like me to create a gren.json file now?
59-
"""
60-
, defaultValue = True
61-
}
62-
|> Task.andThen
63-
(\confirmed ->
64-
if confirmed then
65-
init config
98+
|> Task.andThen
99+
(\confirmed ->
100+
if confirmed then
101+
init config
66102

67-
else
68-
Stream.Log.line config.stdout "Ok, I won't do anything."
69-
)
103+
else
104+
Stream.Log.line config.stdout "Ok, I won't do anything."
70105
)
71106
|> Task.onError
72-
(\_ ->
73-
Stream.Log.line config.stdout "Error happened!"
107+
(\err ->
108+
Stream.Log.line config.stdout (displayError err)
74109
)
75110
|> Task.execute
76111

77112

78-
init : Config -> Task x {}
113+
init : Config -> Task Error {}
79114
init config =
80115
let
81116
deps =
@@ -85,9 +120,9 @@ init config =
85120
|> Array.map
86121
(\packageName ->
87122
Git.fetchLatestVersion config.cpPermission packageName
88-
|> Task.onError
89-
(\_ ->
90-
Task.fail ("Something went wrong when retrieving version for " ++ PackageName.toString packageName)
123+
|> Task.mapError
124+
(\err ->
125+
CouldNotRetrieveVersions { packageName = packageName, error = err }
91126
)
92127
|> Task.andThen
93128
(\maybeVsn ->
@@ -99,12 +134,11 @@ init config =
99134
}
100135

101136
Nothing ->
102-
Task.fail ("No version found for " ++ PackageName.toString packageName)
137+
Task.fail (NoVersionFound packageName)
103138
)
104139
)
105140
|> Task.concurrent
106141
|> Task.andThen (generateGrenJson config)
107-
|> Task.onError (\err -> Stream.Log.line config.stdout err)
108142

109143

110144
dependenciesForPlatform : Platform -> Array PackageName
@@ -120,7 +154,7 @@ dependenciesForPlatform platform =
120154
[ PackageName.core, PackageName.node ]
121155

122156

123-
generateGrenJson : Config -> Array { name : PackageName, version : SemanticVersion } -> Task String {}
157+
generateGrenJson : Config -> Array { name : PackageName, version : SemanticVersion } -> Task Error {}
124158
generateGrenJson config deps =
125159
let
126160
outline =
@@ -173,7 +207,7 @@ generateGrenJson config deps =
173207
{ projectPath = Path.empty
174208
, outline = outline
175209
}
176-
|> Task.mapError Debug.toString
210+
|> Task.mapError (\_ -> InstallError)
177211
|> Task.andThen
178212
(\resolution ->
179213
let
@@ -207,6 +241,31 @@ generateGrenJson config deps =
207241
in
208242
Path.fromPosixString "gren.json"
209243
|> FileSystem.writeFile config.fsPermission (Bytes.fromString encodedOutline)
210-
|> Task.mapError (\fsErr -> Debug.toString fsErr)
244+
|> Task.mapError FileSystemFailure
211245
|> Task.map (\_ -> {})
212246
)
247+
248+
249+
displayError : Error -> String
250+
displayError err =
251+
Terminal.User.prettyPrint <|
252+
when err is
253+
AlreadyInitialized ->
254+
Terminal.User.report
255+
"EXISTING PROJECT"
256+
Nothing
257+
(PP.verticalBlock
258+
[ PP.words "You already have a gren.json file, so there's nothing for me to initialize!"
259+
, PP.empty
260+
, PP.block
261+
[ PP.text "Maybe "
262+
, PP.color PP.Green (Terminal.User.makeLink "init")
263+
, PP.text " "
264+
, PP.words "can help you figure out what to do next?"
265+
]
266+
]
267+
)
268+
269+
_ ->
270+
-- TODO
271+
PP.text ""

src/Terminal/PackageInstall.gren

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Terminal.PackageInstall exposing
99
)
1010

1111

12+
import CLI.PrettyPrinter as PP
1213
import ChildProcess
1314
import Compiler.PackageName as PackageName exposing (PackageName)
1415
import Compiler.Paths
@@ -408,9 +409,17 @@ addPackage config { projectPath, outline } requestedPackage =
408409
}
409410

410411
plan =
411-
"The plan is to add the following dependencies:\n\n"
412-
++ (Dict.foldl (\k v acc -> acc ++ " " ++ k ++ " " ++ SemanticVersion.toString v ++ "\n") "" newPackages)
413-
++ "\nDo you want me to update the gren.json file accordingly?"
412+
PP.verticalBlock
413+
[ PP.words "The plan is to add the following dependencies:"
414+
, PP.empty
415+
, PP.empty
416+
, Dict.foldl (\k v acc -> Array.pushLast (k ++ " " ++ SemanticVersion.toString v) acc) [] newPackages
417+
|> Array.map PP.text
418+
|> PP.verticalBlock
419+
|> PP.indent
420+
, PP.empty
421+
, PP.words "Do you want me to update the gren.json file accordingly?"
422+
]
414423
in
415424
User.confirm
416425
{ stdout = config.stdout

src/Terminal/PackageUninstall.gren

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Terminal.PackageUninstall exposing
44
)
55

66

7+
import CLI.PrettyPrinter as PP
78
import ChildProcess
89
import Compiler.PackageName as PackageName exposing (PackageName)
910
import Compiler.Paths
@@ -49,7 +50,6 @@ type Error
4950
| PackageInstallDebug String
5051

5152

52-
-- TODO: Respect interactivity flag
5353
-- TODO: Also remove indirect dependencies which are no longer needed
5454
-- TODO: Suggest moving a direct dependency to indirect
5555
-- TODO: Make sure to clean gren_packages folder
@@ -113,9 +113,16 @@ run config { projectPath, outline } requestedPackage =
113113
|> Dict.keys
114114

115115
plan =
116-
"The plan is to remove the following dependencies:\n\n"
117-
++ (Array.foldl (\package acc -> acc ++ " " ++ package ++ "\n") "" removedPackages)
118-
++ "\nDo you want me to update the gren.json file accordingly? [Y/n]: "
116+
PP.verticalBlock
117+
[ PP.words "The plan is to remove the following dependencies:"
118+
, PP.empty
119+
, removedPackages
120+
|> Array.map PP.text
121+
|> PP.verticalBlock
122+
|> PP.indent
123+
, PP.empty
124+
, PP.words "Do you want me to update the gren.json file accordingly?"
125+
]
119126
in
120127
User.confirm
121128
{ stdout = config.stdout

0 commit comments

Comments
 (0)