module Language.PureScript.Docs.ParseInPackage
( parseFilesInPackages
) where
import Protolude
import qualified Data.Map as M
import Language.PureScript.Docs.Types
import qualified Language.PureScript as P
import System.IO.UTF8 (readUTF8FileT)
import Web.Bower.PackageMeta (PackageName)
parseFilesInPackages ::
(MonadError P.MultipleErrors m, MonadIO m) =>
[FilePath]
-> [(PackageName, FilePath)]
-> m ([(FilePath, P.Module)], Map P.ModuleName PackageName)
parseFilesInPackages inputFiles depsFiles = do
inputFiles' <- traverse (readFileAs . Local) inputFiles
depsFiles' <- traverse (readFileAs . uncurry FromDep) depsFiles
modules <- parse (inputFiles' ++ depsFiles')
let mnMap = M.fromList (mapMaybe (\(inpkg, m) -> (P.getModuleName m,) <$> inPkgToMaybe inpkg) modules)
pure (map (first fileInfoToString) modules, mnMap)
where
parse ::
(MonadError P.MultipleErrors m) =>
[(FileInfo, Text)]
-> m [(FileInfo, P.Module)]
parse =
throwLeft . P.parseModulesFromFiles fileInfoToString
inPkgToMaybe = \case
Local _ -> Nothing
FromDep pkgName _ -> Just pkgName
throwLeft :: (MonadError l m) => Either l r -> m r
throwLeft = either throwError return
type FileInfo = InPackage FilePath
fileInfoToString :: FileInfo -> FilePath
fileInfoToString (Local fn) = fn
fileInfoToString (FromDep _ fn) = fn
readFileAs :: (MonadIO m) => FileInfo -> m (FileInfo, Text)
readFileAs fi = liftIO . fmap ((fi,)) $ readUTF8FileT (ignorePackage fi)