Skip to content

Commit bc86e79

Browse files
committed
Match elm-format behavior for file formatting confirmation
1 parent c6b6db9 commit bc86e79

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

extra/Lamdera/CLI.hs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,7 @@ eval =
198198

199199

200200
format :: Terminal.Command
201-
format =
202-
let
203-
summary =
204-
"Format Elm source files."
205-
206-
details =
207-
"Usage: lamdera format [INPUT] [--output FILE] [--yes] [--validate] [--stdin]\n\n" ++
208-
" Format Elm source files."
209-
210-
example =
211-
stack
212-
[ reflow "Examples:"
213-
, P.vcat [ P.indent 2 $ P.green "lamdera format Main.el # formats Main.elm"
214-
, P.indent 2 $ P.green "lamdera format Main.elm --output Main2.elm # formats Main.elm as Main2.elm"
215-
, P.indent 2 $ P.green "lamdera format src/ # format all *.elm files in the src directory"
216-
]
217-
, ""
218-
, reflow "Full guide to using elm-format at <https://github.com/avh4/elm-format>"
219-
]
220-
in
221-
Lamdera.CLI.Format.command
201+
format = Lamdera.CLI.Format.command
222202

223203

224204
-- HELPERS

extra/Lamdera/CLI/Format.hs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ command =
9191
example =
9292
stack
9393
[ reflow "Examples:"
94-
, P.indent 2 $ P.green "lamdera format Main.elm # formats Main.elm"
95-
, P.indent 2 $ P.green "lamdera format Main.elm --output Main2.elm # formats Main.elm as Main2.elm"
96-
, P.indent 2 $ P.green "lamdera format src/ # format all *.elm files in the src directory"
97-
, ""
94+
, P.vcat [ P.indent 2 $ P.green "lamdera format Main.elm # formats Main.elm"
95+
, P.indent 2 $ P.green "lamdera format Main.elm --output Main2.elm # formats Main.elm as Main2.elm"
96+
, P.indent 2 $ P.green "lamdera format src/ # format all *.elm files in the src directory"
97+
]
9898
, reflow "Full guide to using elm-format at <https://github.com/avh4/elm-format>"
9999
]
100100

@@ -138,7 +138,31 @@ run inputs flags =
138138
TIO.putStrLn "Please specify at least one .elm file to format."
139139
Exit.exitFailure
140140
paths ->
141-
mapM_ (formatFile flags) paths
141+
do
142+
elmFilePaths <- concat <$> mapM expandPath paths
143+
if null elmFilePaths
144+
then return ()
145+
else do
146+
TIO.putStrLn "This will overwrite the following files to use Elm's preferred style:\n"
147+
mapM_ (\f -> TIO.putStrLn $ " " <> T.pack f) elmFilePaths
148+
TIO.putStrLn "\nThis cannot be undone! Make sure to back up these files before proceeding.\n"
149+
if _yes flags
150+
then formatFiles flags elmFilePaths
151+
else do
152+
TIO.putStrLn "Are you sure you want to overwrite these files with formatted versions? (y/n) "
153+
answer <- getLine
154+
case answer of
155+
"Y" -> formatFiles flags elmFilePaths
156+
"y" -> formatFiles flags elmFilePaths
157+
"" -> formatFiles flags elmFilePaths
158+
_ -> return ()
159+
160+
expandPath :: FilePath -> IO [FilePath]
161+
expandPath path = do
162+
isDir <- Dir.doesDirectoryExist path
163+
if isDir
164+
then findElmFiles path
165+
else return [path | FP.isExtensionOf "elm" path]
142166

143167
formatText :: Format -> T.Text -> IO ()
144168
formatText flags input =
@@ -155,8 +179,6 @@ formatFile flags path = do
155179
isDir <- Dir.doesDirectoryExist path
156180
if isDir
157181
then do
158-
contents <- Dir.listDirectory path
159-
let elmFiles = filter (FP.isExtensionOf "elm") contents
160182
elmFilePaths <- findElmFiles path
161183
if null elmFilePaths
162184
then return ()
@@ -210,7 +232,9 @@ formatSingleFile flags path = do
210232
Nothing -> TIO.writeFile path formatted
211233
Left err -> do
212234
TIO.putStrLn err
213-
Exit.exitFailure
235+
if _validate flags
236+
then Exit.exitFailure
237+
else return ()
214238

215239
stack :: [P.Doc] -> P.Doc
216240
stack docs =

0 commit comments

Comments
 (0)