Skip to content

Commit

Permalink
Add dry-run flag (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Sep 18, 2023
1 parent 1dbeb09 commit f654e0c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
7 changes: 6 additions & 1 deletion bin/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,15 @@ cliParser =
versionSource <- versionSourceArg
changelogFile <- changelogFileArg
changelogDir <- changelogDirArg
dryRun <- dryRunArg
Arg.flagHelp
in Update { github, versionSource, mbToken, changelogFile, changelogDir }
in Update { github, versionSource, mbToken, changelogFile, changelogDir, dryRun }
where
cmdDesc = "Updates the changelog file with a new releae entry based on files in the changelog directory"
dryRunArg = Arg.flag [ "--dry-run" ] desc
# Arg.boolean
where
desc = "Print to stdout what the new section in the changelog file would be"
githubRepoArg =
Arg.choose "repo"
[ map Left remoteArg
Expand Down
39 changes: 22 additions & 17 deletions src/UpChangelog/Command/Update.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Data.Tuple (Tuple(..))
import Data.Version (Version, showVersion)
import Data.Version as Version
import Effect.Aff.Class (liftAff)
import Effect.Class.Console (log)
import Fetch as Fetch
import Foreign (unsafeFromForeign)
import Node.Path (sep)
Expand All @@ -43,7 +44,7 @@ import UpChangelog.Utils (breakOn, breakOnEnd, breakOnSpace, commaSeparate, line
update :: App (cli :: UpdateArgs) Unit
update = do
checkFilePaths
{ cli: { changelogDir, changelogFile } } <- ask
{ cli: { changelogDir, changelogFile, dryRun } } <- ask
entries' <- (lines <<< _.stdout) <$> git "ls-tree" [ "--name-only", "HEAD", changelogDir <> sep ]
let
readmeFile = Path.concat [ changelogDir, Constants.readmeFile ]
Expand Down Expand Up @@ -74,23 +75,27 @@ update = do
, after: changelogRest
} <- breakOn (Pattern "\n## ") <$> readTextFile changelogFile
logDebug $ "Changelog preamble is: \n" <> changelogPreamble
writeTextFile changelogFile $
changelogPreamble
<> "\n## "
<> version
<> "\n"
<> conditionalSection "Breaking changes" breaks
<> conditionalSection "New features" features
<> conditionalSection "Bugfixes" fixes
<> conditionalSection "Other improvements" misc
<> conditionalSection "Internal" internal
<> changelogRest
logInfo $ "Updated changelog file"
let
newSectionContent =
"## "
<> version
<> "\n"
<> conditionalSection "Breaking changes" breaks
<> conditionalSection "New features" features
<> conditionalSection "Bugfixes" fixes
<> conditionalSection "Other improvements" misc
<> conditionalSection "Internal" internal
if dryRun then do
logInfo "Ran using --dry-run flag. Printing what would be the new section to stdout: "
log newSectionContent
else do
writeTextFile changelogFile $ changelogPreamble <> "\n" <> newSectionContent <> changelogRest
logInfo $ "Updated changelog file"

void $ git "add" [ changelogFile ]
logDebug $ "Staged changelog file in git"
void $ git "rm" entryFiles
logDebug $ "Staged the deletion of the changelog entry files in git"
void $ git "add" [ changelogFile ]
logDebug $ "Staged changelog file in git"
void $ git "rm" entryFiles
logDebug $ "Staged the deletion of the changelog entry files in git"
where
checkFilePaths = do
{ cli: options } <- ask
Expand Down
1 change: 1 addition & 0 deletions src/UpChangelog/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UpdateArgs =
, mbToken :: Maybe String
, changelogFile :: FilePath
, changelogDir :: FilePath
, dryRun :: Boolean
}

type InitArgs =
Expand Down
12 changes: 11 additions & 1 deletion test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Node.Path as Path
import Node.Process (chdir)
import Node.Process as Process
import Test.Spec (SpecT, afterAll, after_, around_, beforeAll, describe, it, sequential)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Assertions (shouldEqual, shouldNotEqual)
import Test.Spec.Reporter (consoleReporter)
import Test.Spec.Runner (defaultConfig, runSpecT)
import Test.Utils (delDir)
Expand Down Expand Up @@ -182,6 +182,16 @@ spec = do
expectedContent <- readFile correctFile
logContent `shouldEqual` expectedContent

it "update - dry-run does not modify file content" \{ repo, correctFile } -> do
result <- pursChangelog "update" [ "--repo", repo, "--dry-run" ]
when (not $ exitedNormally result) do
liftEffect $ throw $ "Result did not exit normally.\n" <> result.stdout <> "\n" <> result.stderr
files <- readDir Constants.changelogDir
files `shouldNotEqual` [ Constants.readmeFile ]
logContent <- readFile Constants.changelogFile
expectedContent <- readFile correctFile
logContent `shouldNotEqual` expectedContent

withTempDir :: Aff Unit -> Aff Unit
withTempDir f = do
originalDir <- liftEffect $ Process.cwd
Expand Down

0 comments on commit f654e0c

Please sign in to comment.