Skip to content

Commit a44e8f6

Browse files
authored
Merge pull request #277 from wolfadex/no-color
Avoid spinners when NO_COLOR is set or the output is not a TTY
2 parents 4a07e31 + 7528cdf commit a44e8f6

2 files changed

Lines changed: 90 additions & 13 deletions

File tree

‎cli/custom-backend-task.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export function profile(label) {
55
export function profileEnd(label) {
66
console.profileEnd(label);
77
}
8+
9+
export function isTTY() {
10+
return !!process.stdout.isTTY;
11+
}

‎cli/src/Cli.elm‎

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Ansi
44
import Ansi.Color
55
import Ansi.Font
66
import BackendTask exposing (BackendTask)
7+
import BackendTask.Custom
8+
import BackendTask.Env
79
import BackendTask.Extra
810
import BackendTask.File
911
import BackendTask.Http
@@ -382,16 +384,55 @@ parseCliOptions cliOptions =
382384
)
383385

384386

387+
type alias Step a =
388+
( Pages.Script.Spinner.Steps FatalError a
389+
, BackendTask FatalError a
390+
)
391+
392+
393+
withStep :
394+
String
395+
-> (a -> BackendTask FatalError b)
396+
-> Step a
397+
-> Step b
398+
withStep label toTask ( steps, task ) =
399+
( Pages.Script.Spinner.withStep label toTask steps
400+
, task
401+
|> BackendTask.andThen
402+
(\input ->
403+
Pages.Script.log label
404+
|> BackendTask.andThen (\() -> toTask input)
405+
)
406+
)
407+
408+
385409
withInnerStep :
386410
Int
387411
-> Int
388412
-> String
389413
-> (a -> BackendTask FatalError b)
390-
-> Pages.Script.Spinner.Steps FatalError ( a, c, d )
391-
-> Pages.Script.Spinner.Steps FatalError ( b, c, d )
392-
withInnerStep index total label toTask =
393-
Pages.Script.Spinner.withStep (counter index total ++ " " ++ label)
394-
(\( input, acc1, acc2 ) -> toTask input |> BackendTask.map (\result -> ( result, acc1, acc2 )))
414+
-> Step ( a, c, d )
415+
-> Step ( b, c, d )
416+
withInnerStep index total label toTask ( steps, task ) =
417+
let
418+
fullLabel : String
419+
fullLabel =
420+
counter index total ++ " " ++ label
421+
422+
combinedTask : ( a, c, d ) -> BackendTask FatalError ( b, c, d )
423+
combinedTask ( input, acc1, acc2 ) =
424+
toTask input |> BackendTask.map (\result -> ( result, acc1, acc2 ))
425+
in
426+
( Pages.Script.Spinner.withStep fullLabel
427+
combinedTask
428+
steps
429+
, task
430+
|> BackendTask.andThen
431+
(\( input, acc1, acc2 ) ->
432+
Pages.Script.log fullLabel
433+
|> BackendTask.andThen (\() -> combinedTask ( input, acc1, acc2 ))
434+
)
435+
)
395436

396437

397438
withConfig : OpenApi.Config.Config -> BackendTask FatalError ()
@@ -458,7 +499,7 @@ withConfig config =
458499
Just destination ->
459500
withInnerStep index total "Writing merged OAS" (writeMerged destination)
460501
)
461-
|> Pages.Script.Spinner.withStep (counter index total ++ " Parse OAS")
502+
|> withStep (counter index total ++ " Parse OAS")
462503
(\( merged, apiSpecAcc, formatsAcc ) ->
463504
merged
464505
|> decodeOpenApiSpecOrFail { hasAttemptedToConvertFromSwagger = False } config input
@@ -473,12 +514,14 @@ withConfig config =
473514
)
474515
)
475516
( 1
476-
, Pages.Script.Spinner.steps
477-
|> Pages.Script.Spinner.withStep "Collecting configuration" (\_ -> BackendTask.succeed ( (), [], [] ))
517+
, ( Pages.Script.Spinner.steps
518+
|> Pages.Script.Spinner.withStep "Collecting configuration" (\_ -> BackendTask.succeed ( (), [], [] ))
519+
, Pages.Script.log "Collecting configuration" |> BackendTask.map (\() -> ( (), [], [] ))
520+
)
478521
)
479522
(OpenApi.Config.inputs config)
480523
|> Tuple.second
481-
|> Pages.Script.Spinner.withStep "Generate Elm modules"
524+
|> withStep "Generate Elm modules"
482525
(\( (), apiSpecs, allFormats ) ->
483526
OpenApi.Config.toGenerationConfig (List.concat allFormats) config apiSpecs
484527
|> generateFilesFromOpenApiSpecs
@@ -487,14 +530,34 @@ withConfig config =
487530
identity
488531

489532
else
490-
Pages.Script.Spinner.withStep "Format with elm-format" (onFirst attemptToFormat)
533+
withStep "Format with elm-format" (onFirst attemptToFormat)
534+
)
535+
|> withStep "Write to disk" (onFirst (writeSdkToDisk (OpenApi.Config.outputDirectory config)))
536+
|> (\( steps, task ) ->
537+
terminalInfo
538+
|> BackendTask.andThen
539+
(\{ isTTY, noColor } ->
540+
if isTTY && not noColor then
541+
Pages.Script.Spinner.runSteps steps
542+
543+
else
544+
task
545+
)
491546
)
492-
|> Pages.Script.Spinner.withStep "Write to disk" (onFirst (writeSdkToDisk (OpenApi.Config.outputDirectory config)))
493-
|> Pages.Script.Spinner.runSteps
494547
|> BackendTask.map convertElmCodegenWarnings
495548
|> BackendTask.andThen printSuccessMessageAndWarnings
496549

497550

551+
terminalInfo : BackendTask FatalError { isTTY : Bool, noColor : Bool }
552+
terminalInfo =
553+
BackendTask.map2 (\isTTY noColor -> { isTTY = isTTY, noColor = noColor })
554+
(BackendTask.Custom.run "isTTY" Json.Encode.null Json.Decode.bool
555+
|> BackendTask.allowFatal
556+
|> BackendTask.quiet
557+
)
558+
(BackendTask.Env.get "NO_COLOR" |> BackendTask.map (\nc -> nc /= Nothing && nc /= Just ""))
559+
560+
498561
convertElmCodegenWarnings :
499562
( List ( String, List { declaration : String, warning : String } )
500563
, { warnings : List OpenApi.Generate.Message, requiredPackages : FastSet.Set String }
@@ -730,7 +793,17 @@ convertToSwaggerAndThenDecode config input value =
730793
parseOriginal input swagger
731794
|> BackendTask.andThen mergeOverrides
732795
)
733-
|> Pages.Script.Spinner.runTask "Convert Swagger to Open API"
796+
|> (\task ->
797+
terminalInfo
798+
|> BackendTask.andThen
799+
(\{ isTTY, noColor } ->
800+
if isTTY && not noColor then
801+
Pages.Script.Spinner.runTask "Convert Swagger to Open API" task
802+
803+
else
804+
task
805+
)
806+
)
734807
|> BackendTask.andThen (\converted -> decodeOpenApiSpecOrFail { hasAttemptedToConvertFromSwagger = True } config input converted)
735808

736809

0 commit comments

Comments
 (0)