Skip to content

Commit 1fbe5d3

Browse files
committed
Require versions file as JSON in index.js
1 parent 7bfd8b6 commit 1fbe5d3

File tree

6 files changed

+16
-29
lines changed

6 files changed

+16
-29
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/update.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

33
var Main = require("./output/index");
4+
var versions = require("./dist/versions.json");
45

5-
Main.main();
6+
Main.main(versions)();

src/Main.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ module Main where
22

33
import Prelude
44

5+
import Data.Argonaut.Core (Json)
56
import Data.Foldable (traverse_)
67
import Effect (Effect)
78
import Effect.Aff (launchAff_)
89
import Setup.BuildPlan (constructBuildPlan)
910
import Setup.Download (download)
1011
import Setup.UpdateVersions (updateVersions)
1112

12-
main :: Effect Unit
13-
main = do
14-
plan <- constructBuildPlan
13+
main :: Json -> Effect Unit
14+
main json = do
15+
plan <- constructBuildPlan json
1516
launchAff_ $ traverse_ download plan
1617

1718
update :: Effect Unit

src/Setup/BuildPlan.purs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Setup.BuildPlan (constructBuildPlan, BuildPlan) where
33
import Prelude
44

55
import Actions.Core as Core
6+
import Data.Argonaut.Core (Json)
67
import Data.Argonaut.Decode (decodeJson, printJsonDecodeError, (.:))
78
import Data.Array as Array
89
import Data.Bifunctor (bimap, lmap)
@@ -18,16 +19,15 @@ import Setup.Data.Key (Key)
1819
import Setup.Data.Key as Key
1920
import Setup.Data.Tool (Tool, required)
2021
import Setup.Data.Tool as Tool
21-
import Setup.Data.VersionsFile (readVersionsFile)
2222
import Text.Parsing.Parser (parseErrorMessage)
2323
import Text.Parsing.Parser as ParseError
2424

2525
-- | The list of tools that should be downloaded and cached by the action
2626
type BuildPlan = Array { tool :: Tool, version :: Version }
2727

2828
-- | Construct the list of tools that sholud be downloaded and cached by the action
29-
constructBuildPlan :: Effect BuildPlan
30-
constructBuildPlan = map Array.catMaybes $ traverse resolve Tool.allTools
29+
constructBuildPlan :: Json -> Effect BuildPlan
30+
constructBuildPlan json = map Array.catMaybes $ traverse (resolve json) Tool.allTools
3131

3232
-- | The parsed value of an input field that specifies a version
3333
data VersionField = Latest | Exact Version
@@ -42,8 +42,8 @@ getVersionField = map (map parse) <<< Core.getInput
4242

4343
-- | Resolve the exact version to provide for a tool in the environment, based
4444
-- | on the action.yml file.
45-
resolve :: Tool -> Effect (Maybe { tool :: Tool, version :: Version })
46-
resolve tool = do
45+
resolve :: Json -> Tool -> Effect (Maybe { tool :: Tool, version :: Version })
46+
resolve versionsContents tool = do
4747
let key = Key.fromTool tool
4848
getVersionField key >>= case _ of
4949
Nothing | required tool -> throwError $ error "No input received for required key."
@@ -63,10 +63,9 @@ resolve tool = do
6363

6464
Right Latest -> do
6565
Core.info $ fold [ "Fetching latest tag for ", Tool.name tool ]
66-
json <- readVersionsFile
6766

6867
let
69-
version = lmap printJsonDecodeError $ (_ .: Tool.name tool) =<< decodeJson json
68+
version = lmap printJsonDecodeError $ (_ .: Tool.name tool) =<< decodeJson versionsContents
7069
parse = lmap parseErrorMessage <<< Version.parseVersion
7170

7271
case parse =<< version of

src/Setup/Data/VersionsFile.purs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,13 @@ module Setup.Data.VersionsFile where
22

33
import Prelude
44

5-
import Control.Monad.Error.Class (throwError)
65
import Data.Argonaut.Core (Json, stringify)
7-
import Data.Argonaut.Decode (printJsonDecodeError)
8-
import Data.Argonaut.Decode as Json
9-
import Data.Either (Either(..))
106
import Effect (Effect)
11-
import Effect.Exception (error)
127
import Node.Encoding (Encoding(..))
13-
import Node.FS.Sync (readTextFile, writeTextFile)
8+
import Node.FS.Sync (writeTextFile)
149
import Node.Path (FilePath)
1510

1611
path = "./dist/versions.json" :: FilePath
1712

1813
writeVersionsFile :: Json -> Effect Unit
19-
writeVersionsFile = writeTextFile UTF8 path <<< stringify
20-
21-
readVersionsFile :: Effect Json
22-
readVersionsFile = do
23-
str <- readTextFile UTF8 path
24-
case Json.parseJson str of
25-
Left e ->
26-
throwError $ error $ printJsonDecodeError e
27-
Right v ->
28-
pure v
14+
writeVersionsFile = writeTextFile UTF8 path <<< stringify

0 commit comments

Comments
 (0)