Skip to content

Implement --report=json flag for make and docs commands. #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Git.gren
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Task exposing (Task)
import Terminal.Help as Help
import CLI.PrettyPrinter as PP
import Compiler.Paths
import Terminal.Report as Report exposing (Report)


type Error
Expand All @@ -29,11 +30,11 @@ type Error
| FailedCommand { args : Array String, message : String }


report : String -> String -> Error -> PP.Document
report : String -> String -> Error -> Report
report title context err =
when err is
NoVersions ->
Help.report
Report.create
title
Nothing
( PP.verticalBlock
Expand All @@ -49,7 +50,7 @@ report title context err =
)

NoSuchRepo ->
Help.report
Report.create
title
Nothing
( PP.verticalBlock
Expand All @@ -64,7 +65,7 @@ report title context err =
)

NoSuchRepoOrVersion vsn ->
Help.report
Report.create
title
Nothing
( PP.verticalBlock
Expand All @@ -78,7 +79,7 @@ report title context err =
)

FailedCommand { args, message } ->
Help.report
Report.create
title
Nothing
( PP.verticalBlock
Expand Down
61 changes: 41 additions & 20 deletions src/Main.gren
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Terminal.Init
import Terminal.Run
import Terminal.Help
import Terminal.Repl
import Terminal.Report exposing (Report)
import Compiler.Backend
import Compiler.PackageName as PackageName exposing (PackageName)
import Compiler.Paths
Expand Down Expand Up @@ -293,7 +294,7 @@ update msg model =
|> Task.onError
(\error ->
Terminal.Run.prettifyError error
|> Terminal.Help.prettyPrint { useColor = model.useColor }
|> Terminal.Report.toString (Terminal.Report.Terminal { useColor = model.useColor })
|> Stream.Log.line model.stderr
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)
)
Expand Down Expand Up @@ -351,6 +352,10 @@ parseUserArgs model compilerPath =
endWithErrorString stringErr =
Stream.Log.string model.stderr stringErr
|> Task.map (\_ -> Task.execute <| Node.exitWithCode 1)

endWithErrorReport outputType report =
Terminal.Report.toString outputType report
|> endWithErrorString
in
when CLI.Parser.run model.args CliParser.parser is
CLI.Parser.UnknownCommand commandName ->
Expand Down Expand Up @@ -410,7 +415,7 @@ parseUserArgs model compilerPath =
}
|> Task.map (\_ -> Cmd.none)
|> Task.mapError Terminal.Init.prettifyError
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.Repl flags ->
Expand Down Expand Up @@ -467,7 +472,7 @@ parseUserArgs model compilerPath =
, onComplete = CompilerRan
}
)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.Make flags ->
Expand All @@ -482,6 +487,14 @@ parseUserArgs model compilerPath =

_ ->
Task.succeed model.stdout

reportType =
when flags.report is
Just {} ->
Terminal.Report.Json

Nothing ->
Terminal.Report.Terminal printOpts
in
determineOutputStreamForResolve
|> Task.andThen (\output -> resolveProject { model | stdout = output })
Expand All @@ -508,7 +521,7 @@ parseUserArgs model compilerPath =
, onComplete = CompilerRan
}
)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport reportType)
|> Task.perform RunCmd

CliParser.Run opts ->
Expand Down Expand Up @@ -543,7 +556,7 @@ parseUserArgs model compilerPath =
)
}
|> Task.mapError Terminal.Run.prettifyError
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.Docs flags ->
Expand All @@ -558,6 +571,14 @@ parseUserArgs model compilerPath =

_ ->
Task.succeed model.stdout

reportType =
when flags.report is
Just {} ->
Terminal.Report.Json

Nothing ->
Terminal.Report.Terminal printOpts
in
determineOutputStreamForResolve
|> Task.andThen (\output -> resolveProject { model | stdout = output })
Expand All @@ -581,18 +602,18 @@ parseUserArgs model compilerPath =
, onComplete = CompilerRan
}
)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport reportType)
|> Task.perform RunCmd

CliParser.PackageInstall Nothing ->
resolveProject model
|> Task.andThen
(\res ->
Terminal.PackageInstall.cleanPackageDirectory model.fsPermission res
|> Task.mapError (\_ -> PP.empty)
|> Task.mapError (\_ -> Terminal.Report.empty)
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.PackageInstall (Just requestedPackage) ->
Expand All @@ -615,7 +636,7 @@ parseUserArgs model compilerPath =
|> Task.mapError Terminal.PackageInstall.prettifyAddPackageError
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.PackageUninstall packageName ->
Expand All @@ -638,11 +659,11 @@ parseUserArgs model compilerPath =
|> Task.andThen
(\res ->
Terminal.PackageInstall.cleanPackageDirectory model.fsPermission res
|> Task.mapError (\_ -> PP.empty)
|> Task.mapError (\_ -> Terminal.Report.empty)
)
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.PackageOutdated ->
Expand All @@ -662,7 +683,7 @@ parseUserArgs model compilerPath =
|> Task.mapError Terminal.PackageOutdated.prettifyError
)
|> Task.map (\_ -> Cmd.none)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.PackageValidate ->
Expand Down Expand Up @@ -695,7 +716,7 @@ parseUserArgs model compilerPath =
, onComplete = CompilerRan
}
)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.PackageBump ->
Expand Down Expand Up @@ -728,7 +749,7 @@ parseUserArgs model compilerPath =
, onComplete = CompilerRan
}
)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.PackageDiff args ->
Expand Down Expand Up @@ -818,7 +839,7 @@ parseUserArgs model compilerPath =
, onComplete = CompilerRan
}
)
|> Task.onError endWithError
|> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts))
|> Task.perform RunCmd

CliParser.Paths opts ->
Expand All @@ -836,7 +857,7 @@ parseUserArgs model compilerPath =
|> Task.perform RunCmd


resolveProject : Model -> Task PP.Document Terminal.PackageInstall.PackageResolution
resolveProject : Model -> Task Report Terminal.PackageInstall.PackageResolution
resolveProject model =
Terminal.PackageInstall.readProjectOutline model.fsPermission
|> Task.mapError Terminal.PackageInstall.prettifyProjectOutlineError
Expand All @@ -858,7 +879,7 @@ resolveProject model =


-- TODO: Move to gren-lang/compiler-node
verifyMakeFlags : FileSystem.Permission -> Array ModuleName -> Maybe Compiler.Backend.MakeOutput -> Terminal.PackageInstall.PackageResolution -> Task PP.Document Terminal.PackageInstall.PackageResolution
verifyMakeFlags : FileSystem.Permission -> Array ModuleName -> Maybe Compiler.Backend.MakeOutput -> Terminal.PackageInstall.PackageResolution -> Task Report Terminal.PackageInstall.PackageResolution
verifyMakeFlags fsPermission entryPoints maybeOutput resolution =
let
maybeOutputPath =
Expand Down Expand Up @@ -908,7 +929,7 @@ verifyMakeFlags fsPermission entryPoints maybeOutput resolution =

else
Task.fail <|
Terminal.Help.report
Terminal.Report.create
"CANNOT ACCESS OUTPUT PATH"
(Just path)
(PP.verticalBlock
Expand All @@ -928,7 +949,7 @@ verifyMakeFlags fsPermission entryPoints maybeOutput resolution =

Just metadata ->
if metadata.entityType == FileSystem.Directory then
Terminal.Help.report
Terminal.Report.create
"OUTPUT PATH IS A DIRECTORY"
(Just path)
(PP.verticalBlock
Expand All @@ -955,7 +976,7 @@ verifyMakeFlags fsPermission entryPoints maybeOutput resolution =

Just { value = moduleName } ->
Task.fail <|
Terminal.Help.report
Terminal.Report.create
"MODULE DOES NOT EXIST"
Nothing
(PP.verticalBlock
Expand Down
37 changes: 0 additions & 37 deletions src/Terminal/Help.gren
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Terminal.Help exposing
( confirm
, prettyPrint
, report
, makeLink
, getNullStream
)
Expand Down Expand Up @@ -73,42 +72,6 @@ prettyPrint { useColor } doc =
)


report : String -> Maybe Path -> PP.Document -> PP.Document
report title maybePath message =
let
makeDashes n =
String.repeat (max 1 (80 - n)) "-"

errorBarEnd =
when maybePath is
Nothing ->
makeDashes (4 + String.unitLength title)

Just path ->
-- TODO: platform toString
let
pathStr =
Path.toPosixString path
in
makeDashes (5 + String.unitLength title + String.unitLength pathStr) ++ " " ++ pathStr

errorBar =
PP.block
[ PP.text "--"
, PP.text title
, PP.text errorBarEnd
]
|> PP.color PP.Cyan
in
PP.verticalBlock
[ errorBar
, PP.empty
, message
, PP.empty
, PP.empty
]


makeLink : String -> PP.Document
makeLink filename =
PP.block
Expand Down
11 changes: 6 additions & 5 deletions src/Terminal/Init.gren
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import SemanticVersionRange
import Dict
import Meta
import Terminal.PackageInstall as PackageInstall
import Terminal.Report as Report exposing (Report)


type alias Config =
Expand Down Expand Up @@ -243,11 +244,11 @@ generateGrenJson config projectPath deps =
)


prettifyError : Error -> PP.Document
prettifyError : Error -> Report
prettifyError err =
when err is
AlreadyInitialized ->
Terminal.Help.report
Report.create
"EXISTING PROJECT"
Nothing
(PP.verticalBlock
Expand All @@ -263,7 +264,7 @@ prettifyError err =
)

FileSystemFailure fsErr ->
Terminal.Help.report
Report.create
"FILESYSTEM ERROR"
-- TODO: (Just <| FileSystem.errorPath fsErr)
Nothing
Expand All @@ -281,7 +282,7 @@ prettifyError err =
)

PromptError streamError ->
Terminal.Help.report
Report.create
"STREAM ERROR"
Nothing
(PP.verticalBlock
Expand All @@ -304,7 +305,7 @@ prettifyError err =
error

NoVersionFound packageName ->
Terminal.Help.report
Report.create
"FAILED TO LOAD DEPENDENCIES"
Nothing
(PP.verticalBlock
Expand Down
Loading