@@ -3,6 +3,7 @@ module Terminal.Init exposing
33 , run
44 )
55
6+ import CLI.PrettyPrinter as PP
67import ChildProcess
78import Stream
89import 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+
4051run : Config -> Cmd a
4152run 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 {}
79114init 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
110144dependenciesForPlatform : 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 {}
124158generateGrenJson 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 ""
0 commit comments