Skip to content

Commit c3e9cdf

Browse files
committed
Rewrite Terminal.Init to expose same interface as other Terminal modules.
1 parent 490a56f commit c3e9cdf

File tree

2 files changed

+78
-78
lines changed

2 files changed

+78
-78
lines changed

src/Main.gren

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ parseUserArgs model compilerPath =
381381
, package = flags.package
382382
, platform = flags.platform
383383
}
384+
|> Task.map (\_ -> Cmd.none)
385+
|> Task.mapError Terminal.Init.prettifyError
386+
|> Task.onError endWithError
387+
|> Task.executeCmd
384388

385389
CliParser.Repl flags ->
386390
-- TODO: create repl project when no project outline is found

src/Terminal/Init.gren

Lines changed: 74 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Terminal.Init exposing
22
( Config
3+
, Error (..)
34
, run
5+
, prettifyError
46
)
57

68
import CLI.PrettyPrinter as PP
@@ -47,7 +49,7 @@ type Error
4749
| InstallError PackageInstall.PackageInstallError
4850

4951

50-
run : Config -> Cmd a
52+
run : Config -> Task Error {}
5153
run config =
5254
let
5355
grenJsonExists =
@@ -103,11 +105,6 @@ run config =
103105
else
104106
Stream.Log.line config.stdout "Ok, I won't do anything."
105107
)
106-
|> Task.onError
107-
(\err ->
108-
Stream.Log.line config.stdout (displayError { useColor = config.useColor } err)
109-
)
110-
|> Task.execute
111108

112109

113110
init : Config -> Task Error {}
@@ -240,81 +237,80 @@ generateGrenJson config deps =
240237
)
241238

242239

243-
displayError : { useColor : Bool } -> Error -> String
244-
displayError printOpts err =
245-
Terminal.Help.prettyPrint printOpts <|
246-
when err is
247-
AlreadyInitialized ->
248-
Terminal.Help.report
249-
"EXISTING PROJECT"
250-
Nothing
251-
(PP.verticalBlock
252-
[ PP.words "You already have a gren.json file, so there's nothing for me to initialize!"
253-
, PP.empty
254-
, PP.block
255-
[ PP.text "Maybe "
256-
, PP.color PP.Green (Terminal.Help.makeLink "init")
257-
, PP.text " "
258-
, PP.words "can help you figure out what to do next?"
259-
]
240+
prettifyError : Error -> PP.Document
241+
prettifyError err =
242+
when err is
243+
AlreadyInitialized ->
244+
Terminal.Help.report
245+
"EXISTING PROJECT"
246+
Nothing
247+
(PP.verticalBlock
248+
[ PP.words "You already have a gren.json file, so there's nothing for me to initialize!"
249+
, PP.empty
250+
, PP.block
251+
[ PP.text "Maybe "
252+
, PP.color PP.Green (Terminal.Help.makeLink "init")
253+
, PP.text " "
254+
, PP.words "can help you figure out what to do next?"
260255
]
261-
)
256+
]
257+
)
262258

263-
FileSystemFailure fsErr ->
264-
Terminal.Help.report
265-
"FILESYSTEM ERROR"
266-
(Just <| FileSystem.errorPath fsErr)
267-
(PP.verticalBlock
268-
[ PP.words "An unexpected error occured when accessing the filesystem."
269-
, PP.empty
270-
, PP.words "The error provided by the operating system is:"
271-
, PP.empty
272-
, PP.words (FileSystem.errorToString fsErr)
273-
|> PP.color PP.Red
274-
|> PP.indent
275-
, PP.empty
276-
, PP.words "Verify that you have access to this path. If the error persist, consider submitting a bug report."
277-
]
278-
)
259+
FileSystemFailure fsErr ->
260+
Terminal.Help.report
261+
"FILESYSTEM ERROR"
262+
(Just <| FileSystem.errorPath fsErr)
263+
(PP.verticalBlock
264+
[ PP.words "An unexpected error occured when accessing the filesystem."
265+
, PP.empty
266+
, PP.words "The error provided by the operating system is:"
267+
, PP.empty
268+
, PP.words (FileSystem.errorToString fsErr)
269+
|> PP.color PP.Red
270+
|> PP.indent
271+
, PP.empty
272+
, PP.words "Verify that you have access to this path. If the error persist, consider submitting a bug report."
273+
]
274+
)
279275

280-
PromptError streamError ->
281-
Terminal.Help.report
282-
"STREAM ERROR"
283-
Nothing
284-
(PP.verticalBlock
285-
[ PP.words "An unexpected error occured with either the standard output or standard input streams."
286-
, PP.empty
287-
, PP.words "The error is:"
288-
, PP.empty
289-
, PP.words (Stream.errorToString streamError)
290-
|> PP.color PP.Red
291-
|> PP.indent
292-
, PP.empty
293-
, PP.words "This is most likely a bug. Consider submitting a bug report."
294-
]
295-
)
276+
PromptError streamError ->
277+
Terminal.Help.report
278+
"STREAM ERROR"
279+
Nothing
280+
(PP.verticalBlock
281+
[ PP.words "An unexpected error occured with either the standard output or standard input streams."
282+
, PP.empty
283+
, PP.words "The error is:"
284+
, PP.empty
285+
, PP.words (Stream.errorToString streamError)
286+
|> PP.color PP.Red
287+
|> PP.indent
288+
, PP.empty
289+
, PP.words "This is most likely a bug. Consider submitting a bug report."
290+
]
291+
)
296292

297-
CouldNotRetrieveVersions { packageName, error } ->
298-
Git.report
299-
"FAILED TO LOAD DEPENDENCIES"
300-
("I tried to find the latest compatible versions of the " ++ PackageName.toString packageName)
301-
error
293+
CouldNotRetrieveVersions { packageName, error } ->
294+
Git.report
295+
"FAILED TO LOAD DEPENDENCIES"
296+
("I tried to find the latest compatible versions of the " ++ PackageName.toString packageName)
297+
error
302298

303-
NoVersionFound packageName ->
304-
Terminal.Help.report
305-
"FAILED TO LOAD DEPENDENCIES"
306-
Nothing
307-
(PP.verticalBlock
308-
[ PP.words "I tried to find the latest compatible versions of the"
309-
, PP.text " "
310-
, PP.text (PackageName.toString packageName)
311-
|> PP.color PP.Green
312-
, PP.text " "
313-
, PP.words "but failed to find any versions for that package!"
314-
, PP.empty
315-
, PP.words "This is most likely a bug. Consider submitting a bug report."
316-
]
317-
)
299+
NoVersionFound packageName ->
300+
Terminal.Help.report
301+
"FAILED TO LOAD DEPENDENCIES"
302+
Nothing
303+
(PP.verticalBlock
304+
[ PP.words "I tried to find the latest compatible versions of the"
305+
, PP.text " "
306+
, PP.text (PackageName.toString packageName)
307+
|> PP.color PP.Green
308+
, PP.text " "
309+
, PP.words "but failed to find any versions for that package!"
310+
, PP.empty
311+
, PP.words "This is most likely a bug. Consider submitting a bug report."
312+
]
313+
)
318314

319-
InstallError installError ->
320-
PackageInstall.prettifyError installError
315+
InstallError installError ->
316+
PackageInstall.prettifyError installError

0 commit comments

Comments
 (0)