@@ -91,10 +91,10 @@ command =
91
91
example =
92
92
stack
93
93
[ 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
+ ]
98
98
, reflow " Full guide to using elm-format at <https://github.com/avh4/elm-format>"
99
99
]
100
100
@@ -138,7 +138,31 @@ run inputs flags =
138
138
TIO. putStrLn " Please specify at least one .elm file to format."
139
139
Exit. exitFailure
140
140
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 " \n This 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]
142
166
143
167
formatText :: Format -> T. Text -> IO ()
144
168
formatText flags input =
@@ -155,8 +179,6 @@ formatFile flags path = do
155
179
isDir <- Dir. doesDirectoryExist path
156
180
if isDir
157
181
then do
158
- contents <- Dir. listDirectory path
159
- let elmFiles = filter (FP. isExtensionOf " elm" ) contents
160
182
elmFilePaths <- findElmFiles path
161
183
if null elmFilePaths
162
184
then return ()
@@ -210,7 +232,9 @@ formatSingleFile flags path = do
210
232
Nothing -> TIO. writeFile path formatted
211
233
Left err -> do
212
234
TIO. putStrLn err
213
- Exit. exitFailure
235
+ if _validate flags
236
+ then Exit. exitFailure
237
+ else return ()
214
238
215
239
stack :: [P. Doc ] -> P. Doc
216
240
stack docs =
0 commit comments