diff --git a/package.json b/package.json index be173f8..2aa1e24 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@actions/cache": "^1.0.2", "@actions/core": "^1.2.5", "@actions/exec": "^1.0.4", + "@actions/glob": "^0.1.0", "@actions/io": "^1.0.2", "@actions/tool-cache": "^1.6.0" } diff --git a/src/GitHub/Actions/Glob.js b/src/GitHub/Actions/Glob.js new file mode 100644 index 0000000..7b34672 --- /dev/null +++ b/src/GitHub/Actions/Glob.js @@ -0,0 +1,13 @@ +"use strict"; + +const glob = require("@actions/glob"); + +exports.createImpl = glob.create; + +exports.getSearchPaths = function (globber) { + return globber.getSearchPaths(); +}; + +exports.globImpl = function (globber) { + return globber.glob(); +}; diff --git a/src/GitHub/Actions/Glob.purs b/src/GitHub/Actions/Glob.purs new file mode 100644 index 0000000..c04e650 --- /dev/null +++ b/src/GitHub/Actions/Glob.purs @@ -0,0 +1,75 @@ +-- | Exports functions from the @actions/glob module provided by GitHub +-- | https://github.com/actions/toolkit/tree/main/packages/glob +module GitHub.Actions.Glob + ( Globber + , GlobOptions(..) + , getSearchPaths + , glob + ) where + +import Prelude + +import Control.Monad.Except (ExceptT(..)) +import Control.Promise (Promise, toAffE) +import Data.Maybe (Maybe(..)) +import Data.Nullable (Nullable, toNullable) +import Effect (Effect) +import Effect.Aff (Aff, Error, try) +import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2) + +-- | Used to match files and directories. +foreign import data Globber :: Type + +-- | Options to control globbing behavior. +type GlobOptions = + { followSymbolicLinks :: Maybe Boolean + , implicitDescendants :: Maybe Boolean + , omitBrokenSymbolicLinks :: Maybe Boolean + } + +-- | Defaults for `GlobOptions`. +defaultGlobOptions :: GlobOptions +defaultGlobOptions = + { followSymbolicLinks: Just true + , implicitDescendants: Just true + , omitBrokenSymbolicLinks: Just true + } + +type JSGlobOptions = + { followSymbolicLinks :: Nullable Boolean + , implicitDescendants :: Nullable Boolean + , omitBrokenSymbolicLinks :: Nullable Boolean + } + +toJSGlobOptions :: GlobOptions -> JSGlobOptions +toJSGlobOptions opts = + { followSymbolicLinks: toNullable opts.followSymbolicLinks + , implicitDescendants: toNullable opts.implicitDescendants + , omitBrokenSymbolicLinks: toNullable opts.omitBrokenSymbolicLinks + } + +foreign import createImpl :: EffectFn2 String (Nullable (JSGlobOptions)) (Promise Globber) + +-- | Constructs a globber. +create :: Maybe GlobOptions -> String -> ExceptT Error Aff Globber +create opts pattern = + runEffectFn2 createImpl pattern jsOpts + # toAffE + # (try >>> ExceptT) + where + jsOpts = toNullable $ map toJSGlobOptions $ opts + +-- | Returns the search path preceding the first glob segment, from each pattern. +-- | Duplicates and descendants of other paths are filtered out. +foreign import getSearchPaths :: Globber -> Effect (Array String) + +foreign import globImpl :: EffectFn1 Globber (Promise (Array String)) + +-- | Returns files and directories matching the glob patterns. +-- | +-- | Order of the results is not guaranteed. +glob :: Globber -> ExceptT Error Aff (Array String) +glob globber = + runEffectFn1 globImpl globber + # toAffE + # (try >>> ExceptT)