diff --git a/src/Git.gren b/src/Git.gren index 2eaaa68e..988a27ac 100644 --- a/src/Git.gren +++ b/src/Git.gren @@ -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 @@ -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 @@ -49,7 +50,7 @@ report title context err = ) NoSuchRepo -> - Help.report + Report.create title Nothing ( PP.verticalBlock @@ -64,7 +65,7 @@ report title context err = ) NoSuchRepoOrVersion vsn -> - Help.report + Report.create title Nothing ( PP.verticalBlock @@ -78,7 +79,7 @@ report title context err = ) FailedCommand { args, message } -> - Help.report + Report.create title Nothing ( PP.verticalBlock diff --git a/src/Main.gren b/src/Main.gren index 8e882f4c..b84e9798 100644 --- a/src/Main.gren +++ b/src/Main.gren @@ -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 @@ -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) ) @@ -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 -> @@ -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 -> @@ -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 -> @@ -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 }) @@ -508,7 +521,7 @@ parseUserArgs model compilerPath = , onComplete = CompilerRan } ) - |> Task.onError endWithError + |> Task.onError (endWithErrorReport reportType) |> Task.perform RunCmd CliParser.Run opts -> @@ -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 -> @@ -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 }) @@ -581,7 +602,7 @@ parseUserArgs model compilerPath = , onComplete = CompilerRan } ) - |> Task.onError endWithError + |> Task.onError (endWithErrorReport reportType) |> Task.perform RunCmd CliParser.PackageInstall Nothing -> @@ -589,10 +610,10 @@ 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.PackageInstall (Just requestedPackage) -> @@ -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 -> @@ -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 -> @@ -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 -> @@ -695,7 +716,7 @@ parseUserArgs model compilerPath = , onComplete = CompilerRan } ) - |> Task.onError endWithError + |> Task.onError (endWithErrorReport (Terminal.Report.Terminal printOpts)) |> Task.perform RunCmd CliParser.PackageBump -> @@ -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 -> @@ -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 -> @@ -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 @@ -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 = @@ -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 @@ -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 @@ -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 diff --git a/src/Terminal/Help.gren b/src/Terminal/Help.gren index 72ed10fd..29d3ae1d 100644 --- a/src/Terminal/Help.gren +++ b/src/Terminal/Help.gren @@ -1,7 +1,6 @@ module Terminal.Help exposing ( confirm , prettyPrint - , report , makeLink , getNullStream ) @@ -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 diff --git a/src/Terminal/Init.gren b/src/Terminal/Init.gren index 43181e25..f10e982d 100644 --- a/src/Terminal/Init.gren +++ b/src/Terminal/Init.gren @@ -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 = @@ -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 @@ -263,7 +264,7 @@ prettifyError err = ) FileSystemFailure fsErr -> - Terminal.Help.report + Report.create "FILESYSTEM ERROR" -- TODO: (Just <| FileSystem.errorPath fsErr) Nothing @@ -281,7 +282,7 @@ prettifyError err = ) PromptError streamError -> - Terminal.Help.report + Report.create "STREAM ERROR" Nothing (PP.verticalBlock @@ -304,7 +305,7 @@ prettifyError err = error NoVersionFound packageName -> - Terminal.Help.report + Report.create "FAILED TO LOAD DEPENDENCIES" Nothing (PP.verticalBlock diff --git a/src/Terminal/PackageBump.gren b/src/Terminal/PackageBump.gren index 945f3d11..20399b9f 100644 --- a/src/Terminal/PackageBump.gren +++ b/src/Terminal/PackageBump.gren @@ -30,6 +30,7 @@ import Stream.Extra import Terminal.PackageInstall import Terminal.Help as Help import CLI.PrettyPrinter as PP +import Terminal.Report as Report exposing (Report) type alias Config = @@ -176,11 +177,11 @@ run config { projectPath, outline } = -prettifyError : Error -> PP.Document +prettifyError : Error -> Report prettifyError error = when error is CannotBumpApplications -> - Help.report + Report.create "CANNOT BUMP APPLICATIONS" Nothing ( PP.words @@ -190,7 +191,7 @@ prettifyError error = ) CurrentVersionUnpublished -> - Help.report + Report.create "CURRENT VERSION IS NOT PUBLISHED" Nothing ( PP.words @@ -200,7 +201,7 @@ prettifyError error = ) PreviousPackageCorruption -> - Help.report + Report.create "CANNOT BUILD PREVIOUS VERSION" Nothing ( PP.verticalBlock diff --git a/src/Terminal/PackageDiff.gren b/src/Terminal/PackageDiff.gren index b878f743..9bab6b1e 100644 --- a/src/Terminal/PackageDiff.gren +++ b/src/Terminal/PackageDiff.gren @@ -33,6 +33,7 @@ import Stream.Extra import Terminal.PackageInstall import Terminal.Help as Help import CLI.PrettyPrinter as PP +import Terminal.Report as Report exposing (Report) type alias Config = @@ -159,14 +160,14 @@ runLocal config maybeVersion = ) -prettifyLocalError : LocalError -> PP.Document +prettifyLocalError : LocalError -> Report prettifyLocalError error = when error is LocalReadProjectOutlineError outlineError -> Terminal.PackageInstall.prettifyProjectOutlineError outlineError LocalCannotDiffApplication -> - Help.report + Report.create "CANNOT DIFF APPLICATIONS" Nothing ( PP.words "This project is an application, but I can only diff packages!" ) @@ -234,7 +235,7 @@ installHiddenProject config projectPath packageName packageVersion = ) -prettifyHiddenProjectError : InstallHiddenProjectError -> PP.Document +prettifyHiddenProjectError : InstallHiddenProjectError -> Report prettifyHiddenProjectError error = when error is HiddenProjectGitCloneError gitError -> @@ -244,7 +245,7 @@ prettifyHiddenProjectError error = gitError HiddenProjectReadOutlineError fsErr -> - Help.report + Report.create "FAILED TO READ OUTLINE" Nothing (PP.verticalBlock @@ -258,7 +259,7 @@ prettifyHiddenProjectError error = ) HiddenProjectOutlineDecodeError jsonError -> - Help.report + Report.create "FAILED TO READ OUTLINE" Nothing (PP.verticalBlock @@ -309,7 +310,7 @@ runGlobal config packageName lowerVersion upperVersion = ) -prettifyGlobalError : GlobalError -> PP.Document +prettifyGlobalError : GlobalError -> Report prettifyGlobalError error = when error is GlobalReadProjectOutlineError outlineError -> diff --git a/src/Terminal/PackageInstall.gren b/src/Terminal/PackageInstall.gren index fa6f6295..d50b84a3 100644 --- a/src/Terminal/PackageInstall.gren +++ b/src/Terminal/PackageInstall.gren @@ -41,6 +41,7 @@ import Json.Encode as Json import Json.Decode as Decode exposing (Decoder) import Stream.Extra import Terminal.Help as Help +import Terminal.Report as Report exposing (Report) type alias Config = @@ -611,11 +612,11 @@ installStep config opts = } -prettifyError : PackageInstallError -> PP.Document +prettifyError : PackageInstallError -> Report prettifyError error = when error is PackageInstallFailedToFindProjectSourceFiles fsErr -> - Help.report + Report.create "FAILED TO FIND PROJECT SOURCES" Nothing (PP.verticalBlock @@ -629,7 +630,7 @@ prettifyError error = ) PackageInstallFailedToFindSourceFiles { package, error = fsErr} -> - Help.report + Report.create ("FAILED TO FIND SOURCES FOR PACKAGE " ++ PackageName.toString package) Nothing (PP.verticalBlock @@ -643,7 +644,7 @@ prettifyError error = ) PackageInstallInvalidOutline { package, message } -> - Help.report + Report.create ("INVALID gren.json IN " ++ PackageName.toString package) Nothing (PP.verticalBlock @@ -657,7 +658,7 @@ prettifyError error = ) PackageInstallFailedToLoadBundle { package, path, message } -> - Help.report + Report.create "FAILED TO LOAD PACKAGE BUNDLE" (Just path) (PP.verticalBlock @@ -678,7 +679,7 @@ prettifyError error = ) PackageInstallInvalidBundle { package, path, message } -> - Help.report + Report.create "FAILED TO LOAD PACKAGE BUNDLE" (Just path) (PP.verticalBlock @@ -699,7 +700,7 @@ prettifyError error = ) PackageInstallConflict { package, version1, version2 } -> - Help.report + Report.create "CANNOT FIND COMPATIBLE VERSION" Nothing (PP.verticalBlock @@ -727,7 +728,7 @@ prettifyError error = gitErr PackageInstallFileSystemError { package, error = fsErr } -> - Help.report + Report.create "FAILED TO LOAD PACKAGE" Nothing (PP.verticalBlock @@ -746,7 +747,7 @@ prettifyError error = ) PackageInstallCreateBundleError { package, path, error = streamErr } -> - Help.report + Report.create "FAILED TO CREATE PACKAGE BUNDLE" (Just path) (PP.verticalBlock @@ -765,7 +766,7 @@ prettifyError error = ) PackageInstallMissingIndirectDependency { package, version } -> - Help.report + Report.create "MISSING INDIRECT DEPENDENCIES" (Just (Path.fromPosixString "gren.json")) (PP.verticalBlock @@ -780,7 +781,7 @@ prettifyError error = ) PackageInstallBadLocalDependency { package, dependency } -> - Help.report + Report.create "INDIRECT LOCAL DEPENDENCY" Nothing (PP.verticalBlock @@ -802,7 +803,7 @@ prettifyError error = ) PackageInstallRootIncompatibleCompiler rootCompiler -> - Help.report + Report.create "INCOMPATIBLE COMPILER" Nothing (PP.verticalBlock @@ -821,7 +822,7 @@ prettifyError error = ) PackageInstallIncompatibleCompiler { package, rootCompiler, packageCompiler } -> - Help.report + Report.create "INCOMPATIBLE PACKAGE" Nothing (PP.verticalBlock @@ -841,7 +842,7 @@ prettifyError error = ) PackageInstallIncompatiblePlatform { package, rootPlatform, packagePlatform } -> - Help.report + Report.create "INCOMPATIBLE PACKAGE" Nothing (PP.verticalBlock @@ -1125,14 +1126,14 @@ promptUpdateOutline config grenJsonPath newPackages finalOutline = |> Task.map (\_ -> {}) ) -prettifyAddPackageError : AddPackageError -> PP.Document +prettifyAddPackageError : AddPackageError -> Report prettifyAddPackageError error = when error is AddPackageAlreadyInstalled -> - PP.empty + Report.empty AddPackagePromptError streamErr -> - Help.report + Report.create "FAILED TO COMMUNICATE WITH USER" Nothing (PP.verticalBlock @@ -1148,10 +1149,10 @@ prettifyAddPackageError error = ) AddPackageUserCancelled -> - PP.empty + Report.empty AddPackageWriteOutlineError fsErr -> - Help.report + Report.create "FAILED TO UPDATE OUTLINE" Nothing (PP.verticalBlock @@ -1234,11 +1235,11 @@ readProjectOutline fsPermission = ) ) -prettifyProjectOutlineError : ReadProjectOutlineError -> PP.Document +prettifyProjectOutlineError : ReadProjectOutlineError -> Report prettifyProjectOutlineError error = when error is ReadProjectOutlineNoProject _ -> - Help.report + Report.create "COULDN'T FIND PROJECT" (Just (Path.fromPosixString "gren.json")) (PP.verticalBlock @@ -1253,7 +1254,7 @@ prettifyProjectOutlineError error = ) ReadProjectOutlineInvalidGrenJson jsonErr -> - Help.report + Report.create "FAILED TO READ OUTLINE" (Just (Path.fromPosixString "gren.json")) (PP.verticalBlock diff --git a/src/Terminal/PackageOutdated.gren b/src/Terminal/PackageOutdated.gren index d7ffe00a..38eb1878 100644 --- a/src/Terminal/PackageOutdated.gren +++ b/src/Terminal/PackageOutdated.gren @@ -24,6 +24,7 @@ import Json.Encode as Json import Json.Decode as Decode exposing (Decoder) import Stream.Extra import CLI.PrettyPrinter as PP +import Terminal.Report as Report exposing (Report) type alias Config = @@ -129,7 +130,7 @@ run config { projectPath, outline } = -- TODO: Ask user to auto-update dependencies -prettifyError : Error -> PP.Document +prettifyError : Error -> Report prettifyError error = when error is FailedToFetchVersions { package, error = gitError } -> diff --git a/src/Terminal/PackageUninstall.gren b/src/Terminal/PackageUninstall.gren index 02107862..191c2582 100644 --- a/src/Terminal/PackageUninstall.gren +++ b/src/Terminal/PackageUninstall.gren @@ -26,6 +26,7 @@ import Json.Decode as Decode exposing (Decoder) import Stream.Extra import Terminal.Help as Help import Terminal.PackageInstall +import Terminal.Report as Report exposing (Report) type alias Config = @@ -217,11 +218,11 @@ promptUpdateOutline config grenJsonPath finalOutline removedPackages = ) -prettifyError : Error -> PP.Document +prettifyError : Error -> Report prettifyError error = when error is PackageNotInstalled -> - Help.report + Report.create "PACKAGE NOT INSTALLED" Nothing (PP.verticalBlock @@ -232,7 +233,7 @@ prettifyError error = ) PackageInstallError (Terminal.PackageInstall.PackageInstallMissingIndirectDependency { package }) -> - Help.report + Report.create "FAILED TO UNINSTALL PACKAGE" (Just <| Path.fromPosixString "gren.json") (PP.verticalBlock @@ -243,13 +244,13 @@ prettifyError error = ) PackageInstallError _ -> - Help.report + Report.create "FAILED TO UNINSTALL PACKAGE" (Just <| Path.fromPosixString "gren.json") (PP.words "I cannot seem to resolve your dependencies once I remove the requested package, for some reason.") PromptError streamErr -> - Help.report + Report.create "FAILED TO COMMUNICATE WITH USER" Nothing (PP.verticalBlock @@ -265,10 +266,10 @@ prettifyError error = ) UserCancelled -> - PP.empty + Report.empty OutlineWriteError fsErr -> - Help.report + Report.create "FAILED TO UPDATE OUTLINE" Nothing (PP.verticalBlock diff --git a/src/Terminal/PackageValidate.gren b/src/Terminal/PackageValidate.gren index d2c7bc15..e0e69c04 100644 --- a/src/Terminal/PackageValidate.gren +++ b/src/Terminal/PackageValidate.gren @@ -31,6 +31,7 @@ import Stream.Extra import Terminal.PackageInstall import Terminal.Help as Help import CLI.PrettyPrinter as PP +import Terminal.Report as Report exposing (Report) type alias Config = @@ -265,17 +266,17 @@ verifyLicense permission projectPath = |> Task.map (\_ -> {}) -prettifyError : Error -> PP.Document +prettifyError : Error -> Report prettifyError error = when error is IsApplication -> - Help.report + Report.create "NOT A PACKAGE" Nothing (PP.words "I cannot validate applications, only packages!") NoExposedModules -> - Help.report + Report.create "NO EXPOSED MODULES" (Just (Path.fromPosixString "gren.json")) (PP.verticalBlock @@ -294,7 +295,7 @@ prettifyError error = ) BadSummary -> - Help.report + Report.create "BAD SUMMARY" (Just (Path.fromPosixString "gren.json")) (PP.verticalBlock @@ -314,7 +315,7 @@ prettifyError error = HasLocalDependencies -> - Help.report + Report.create "FOUND LOCAL DEPENDENCIES" (Just (Path.fromPosixString "gren.json")) ( PP.verticalBlock @@ -339,7 +340,7 @@ prettifyError error = gitError FailedToReadReadme _ -> - Help.report + Report.create "NO README" (Just (Path.fromPosixString "README.md")) (PP.words @@ -350,7 +351,7 @@ prettifyError error = ) ShortReadme -> - Help.report + Report.create "SHORT README" (Just (Path.fromPosixString "README.md")) (PP.words @@ -361,7 +362,7 @@ prettifyError error = ) FailedToReadLicense _ -> - Help.report + Report.create "NO LICENSE FILE" (Just (Path.fromPosixString "LICENSE")) ( PP.verticalBlock @@ -384,7 +385,7 @@ prettifyError error = ) NoPublishedVersionAndNot1 -> - Help.report + Report.create "BAD VERSION" (Just (Path.fromPosixString "gren.json")) ( PP.words @@ -395,7 +396,7 @@ prettifyError error = ) NoLocalTag -> - Help.report + Report.create "NO TAG" Nothing ( PP.verticalBlock @@ -413,7 +414,7 @@ prettifyError error = ) HasLocalChanges -> - Help.report + Report.create "LOCAL CHANGES" Nothing ( PP.words @@ -428,7 +429,7 @@ prettifyError error = Terminal.PackageInstall.prettifyError installError PreviousPackageCorrupted -> - Help.report + Report.create "CANNOT INSTALL PREVIOUS VERSION OF THIS PACKAGE" Nothing ( PP.verticalBlock diff --git a/src/Terminal/Repl.gren b/src/Terminal/Repl.gren index d053a909..482d0f58 100644 --- a/src/Terminal/Repl.gren +++ b/src/Terminal/Repl.gren @@ -28,6 +28,7 @@ import Dict import Meta import Terminal.PackageInstall as PackageInstall import Terminal.Init +import Terminal.Report as Report exposing (Report) type alias Config = @@ -101,11 +102,11 @@ setupGlobalReplProject config = ) -prettifyError : Error -> PP.Document +prettifyError : Error -> Report prettifyError error = when error is FileSystemError data -> - Terminal.Help.report + Report.create "CANNOT ACCESS REPL PROJECT" (Just data.replOutlinePath) (PP.verticalBlock @@ -117,7 +118,7 @@ prettifyError error = ) InvalidOutline replOutlinePath -> - Terminal.Help.report + Report.create "CANNOT READ REPL OUTLINE" (Just replOutlinePath) (PP.verticalBlock @@ -128,7 +129,7 @@ prettifyError error = ) InitFailure path -> - Terminal.Help.report + Report.create "CANNOT INITIALIZE REPL PROJECT" (Just path) (PP.verticalBlock diff --git a/src/Terminal/Report.gren b/src/Terminal/Report.gren new file mode 100644 index 00000000..d341754c --- /dev/null +++ b/src/Terminal/Report.gren @@ -0,0 +1,112 @@ +module Terminal.Report exposing + ( Report + -- + , empty + , create + -- + , OutputType (..) + , toString + ) + + +import FileSystem.Path as Path exposing (Path) +import CLI.PrettyPrinter as PP +import Terminal.Help +import Json.Encode as Encode + + +type Report + = Empty + | Report + { title : String + , maybePath : Maybe Path + , message : PP.Document + } + + +empty : Report +empty = + Empty + + +create : String -> Maybe Path -> PP.Document -> Report +create title maybePath message = + Report + { title = title + , maybePath = maybePath + , message = message + } + + +type OutputType + = Terminal { useColor : Bool } + | Json + + +toString : OutputType -> Report -> String +toString outputType report = + when outputType is + Json -> + when report is + Empty -> + Encode.encode 0 (Encode.object []) + + Report { title, maybePath, message } -> + Encode.encode 4 <| + Encode.object + [ { key = "type", value = Encode.string "error" } + , { key = "title", value = Encode.string title } + , { key = "path" + , value = + maybePath + |> Maybe.map Path.toPosixString + |> Maybe.withDefault "" + |> Encode.string + } + , { key = "message" + , value = + message + |> Terminal.Help.prettyPrint { useColor = False } + |> Encode.string + } + ] + + Terminal printOpts -> + Terminal.Help.prettyPrint printOpts <| + when report is + Empty -> + PP.empty + + 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 + ] diff --git a/src/Terminal/Run.gren b/src/Terminal/Run.gren index cd35fa1b..13ee63b5 100644 --- a/src/Terminal/Run.gren +++ b/src/Terminal/Run.gren @@ -28,6 +28,7 @@ import Stream import Task exposing (Task) import Terminal.PackageInstall as PackageInstall exposing (PackageResolution) import Terminal.Help as Help +import Terminal.Report as Report exposing (Report) type Error @@ -273,11 +274,11 @@ getOutputPath projectOutline = Nothing -prettifyError : Error -> PP.Document +prettifyError : Error -> Report prettifyError error = when error is NotAnApplication -> - Help.report + Report.create "NOT AN APPLICATION" Nothing ( PP.words