get the nearest elm.json

haskellify
Brian Hicks 2019-09-07 14:48:13 -05:00
parent cf0dda73b7
commit 6bb6ee6cf4
3 changed files with 25 additions and 5 deletions

View File

@ -59,7 +59,8 @@ executable elm-swapper
-- Other library packages from which modules are imported.
build-depends: base ^>=4.12.0.0,
directory ^>=1.3.3.0
directory ^>=1.3.3.0,
filepath ^>=1.4.2.1
-- Directories containing source files.
hs-source-dirs: src

View File

@ -1,11 +1,11 @@
{ mkDerivation, base, directory, stdenv }:
{ mkDerivation, base, directory, filepath, stdenv }:
mkDerivation {
pname = "elm-swapper";
version = "0.1.0.0";
src = ./.;
isLibrary = false;
isExecutable = true;
executableHaskellDepends = [ base directory ];
executableHaskellDepends = [ base directory filepath ];
description = "automatically dispatch to the right version of Elm";
license = "unknown";
hydraPlatforms = stdenv.lib.platforms.none;

View File

@ -1,8 +1,27 @@
module Main where
import System.Directory
import System.FilePath
nearestElmJson :: IO (Maybe FilePath)
nearestElmJson =
getCurrentDirectory >>= nearestElmJsonHelp
nearestElmJsonHelp :: FilePath -> IO (Maybe FilePath)
nearestElmJsonHelp filepath = do
let candidate = filepath </> "elm.json"
exists <- doesFileExist candidate
if exists then
pure $ Just candidate
else
let next = takeDirectory filepath in
if next == filepath then
pure Nothing
else
nearestElmJsonHelp next
main :: IO ()
main = do
here <- getCurrentDirectory
putStrLn here
elmJson <- nearestElmJson
putStrLn (show elmJson)