1module ElmFormat.Filesystem where 2 3import Control.Monad.Free 4import ElmFormat.FileStore 5import System.FilePath ((</>)) 6import qualified System.FilePath as FilePath 7 8 9collectFiles :: Monad m => (a -> m [a]) -> a -> m [a] 10collectFiles children root = 11 do 12 xs <- children root 13 subChildren <- mapM (collectFiles children) xs 14 return $ root : concat subChildren 15 16 17listDir :: FileStore f => FilePath -> Free f [FilePath] 18listDir path = 19 map (path </>) <$> listDirectory path 20 21 22doesDirectoryExist :: FileStore f => FilePath -> Free f Bool 23doesDirectoryExist path = 24 do 25 fileType <- stat path 26 case fileType of 27 IsDirectory -> return True 28 _ -> return False 29 30 31fileList :: FileStore f => FilePath -> Free f [FilePath] 32fileList = 33 let 34 children path = 35 if isSkippable path then 36 return [] 37 else 38 do 39 directory <- doesDirectoryExist path 40 if directory then listDir path else return [] 41 in 42 collectFiles children 43 44 45isSkippable :: FilePath -> Bool 46isSkippable path = 47 or 48 [ hasFilename "elm-stuff" path 49 , hasFilename "node_modules" path 50 , hasFilename ".git" path 51 ] 52 53hasExtension :: String -> FilePath -> Bool 54hasExtension ext path = 55 ext == FilePath.takeExtension path 56 57 58findAllElmFiles :: FileStore f => FilePath -> Free f [FilePath] 59findAllElmFiles inputFile = 60 filter (hasExtension ".elm") <$> fileList inputFile 61 62 63hasFilename :: String -> FilePath -> Bool 64hasFilename name path = 65 name == FilePath.takeFileName path 66