Skip to content

Commit 7528cdf

Browse files
committed
Avoid spinners when NO_COLOR is set or the output is not a TTY
1 parent fc0e026 commit 7528cdf

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
@@ -377,16 +379,55 @@ parseCliOptions cliOptions =
377379
)
378380

379381

382+
type alias Step a =
383+
( Pages.Script.Spinner.Steps FatalError a
384+
, BackendTask FatalError a
385+
)
386+
387+
388+
withStep :
389+
String
390+
-> (a -> BackendTask FatalError b)
391+
-> Step a
392+
-> Step b
393+
withStep label toTask ( steps, task ) =
394+
( Pages.Script.Spinner.withStep label toTask steps
395+
, task
396+
|> BackendTask.andThen
397+
(\input ->
398+
Pages.Script.log label
399+
|> BackendTask.andThen (\() -> toTask input)
400+
)
401+
)
402+
403+
380404
withInnerStep :
381405
Int
382406
-> Int
383407
-> String
384408
-> (a -> BackendTask FatalError b)
385-
-> Pages.Script.Spinner.Steps FatalError ( a, c, d )
386-
-> Pages.Script.Spinner.Steps FatalError ( b, c, d )
387-
withInnerStep index total label toTask =
388-
Pages.Script.Spinner.withStep (counter index total ++ " " ++ label)
389-
(\( input, acc1, acc2 ) -> toTask input |> BackendTask.map (\result -> ( result, acc1, acc2 )))
409+
-> Step ( a, c, d )
410+
-> Step ( b, c, d )
411+
withInnerStep index total label toTask ( steps, task ) =
412+
let
413+
fullLabel : String
414+
fullLabel =
415+
counter index total ++ " " ++ label
416+
417+
combinedTask : ( a, c, d ) -> BackendTask FatalError ( b, c, d )
418+
combinedTask ( input, acc1, acc2 ) =
419+
toTask input |> BackendTask.map (\result -> ( result, acc1, acc2 ))
420+
in
421+
( Pages.Script.Spinner.withStep fullLabel
422+
combinedTask
423+
steps
424+
, task
425+
|> BackendTask.andThen
426+
(\( input, acc1, acc2 ) ->
427+
Pages.Script.log fullLabel
428+
|> BackendTask.andThen (\() -> combinedTask ( input, acc1, acc2 ))
429+
)
430+
)
390431

391432

392433
withConfig : OpenApi.Config.Config -> BackendTask FatalError ()
@@ -453,7 +494,7 @@ withConfig config =
453494
Just destination ->
454495
withInnerStep index total "Writing merged OAS" (writeMerged destination)
455496
)
456-
|> Pages.Script.Spinner.withStep (counter index total ++ " Parse OAS")
497+
|> withStep (counter index total ++ " Parse OAS")
457498
(\( merged, apiSpecAcc, formatsAcc ) ->
458499
merged
459500
|> decodeOpenApiSpecOrFail { hasAttemptedToConvertFromSwagger = False } config input
@@ -468,12 +509,14 @@ withConfig config =
468509
)
469510
)
470511
( 1
471-
, Pages.Script.Spinner.steps
472-
|> Pages.Script.Spinner.withStep "Collecting configuration" (\_ -> BackendTask.succeed ( (), [], [] ))
512+
, ( Pages.Script.Spinner.steps
513+
|> Pages.Script.Spinner.withStep "Collecting configuration" (\_ -> BackendTask.succeed ( (), [], [] ))
514+
, Pages.Script.log "Collecting configuration" |> BackendTask.map (\() -> ( (), [], [] ))
515+
)
473516
)
474517
(OpenApi.Config.inputs config)
475518
|> Tuple.second
476-
|> Pages.Script.Spinner.withStep "Generate Elm modules"
519+
|> withStep "Generate Elm modules"
477520
(\( (), apiSpecs, allFormats ) ->
478521
OpenApi.Config.toGenerationConfig (List.concat allFormats) config apiSpecs
479522
|> generateFilesFromOpenApiSpecs
@@ -482,14 +525,34 @@ withConfig config =
482525
identity
483526

484527
else
485-
Pages.Script.Spinner.withStep "Format with elm-format" (onFirst attemptToFormat)
528+
withStep "Format with elm-format" (onFirst attemptToFormat)
529+
)
530+
|> withStep "Write to disk" (onFirst (writeSdkToDisk (OpenApi.Config.outputDirectory config)))
531+
|> (\( steps, task ) ->
532+
terminalInfo
533+
|> BackendTask.andThen
534+
(\{ isTTY, noColor } ->
535+
if isTTY && not noColor then
536+
Pages.Script.Spinner.runSteps steps
537+
538+
else
539+
task
540+
)
486541
)
487-
|> Pages.Script.Spinner.withStep "Write to disk" (onFirst (writeSdkToDisk (OpenApi.Config.outputDirectory config)))
488-
|> Pages.Script.Spinner.runSteps
489542
|> BackendTask.map convertElmCodegenWarnings
490543
|> BackendTask.andThen printSuccessMessageAndWarnings
491544

492545

546+
terminalInfo : BackendTask FatalError { isTTY : Bool, noColor : Bool }
547+
terminalInfo =
548+
BackendTask.map2 (\isTTY noColor -> { isTTY = isTTY, noColor = noColor })
549+
(BackendTask.Custom.run "isTTY" Json.Encode.null Json.Decode.bool
550+
|> BackendTask.allowFatal
551+
|> BackendTask.quiet
552+
)
553+
(BackendTask.Env.get "NO_COLOR" |> BackendTask.map (\nc -> nc /= Nothing && nc /= Just ""))
554+
555+
493556
convertElmCodegenWarnings :
494557
( List ( String, List { declaration : String, warning : String } )
495558
, { warnings : List OpenApi.Generate.Message, requiredPackages : FastSet.Set String }
@@ -725,7 +788,17 @@ convertToSwaggerAndThenDecode config input value =
725788
parseOriginal input swagger
726789
|> BackendTask.andThen mergeOverrides
727790
)
728-
|> Pages.Script.Spinner.runTask "Convert Swagger to Open API"
791+
|> (\task ->
792+
terminalInfo
793+
|> BackendTask.andThen
794+
(\{ isTTY, noColor } ->
795+
if isTTY && not noColor then
796+
Pages.Script.Spinner.runTask "Convert Swagger to Open API" task
797+
798+
else
799+
task
800+
)
801+
)
729802
|> BackendTask.andThen (\converted -> decodeOpenApiSpecOrFail { hasAttemptedToConvertFromSwagger = True } config input converted)
730803

731804

0 commit comments

Comments
 (0)