|
| 1 | +importprocessfrom'node:process' |
1 | 2 | import{styleText}from'node:util' |
2 | | -import{log,S_INFO}from'@clack/prompts' |
| 3 | +import{introasclackIntro,outroasclackOutro,log,S_ERROR,S_INFO,S_STEP_SUBMIT,S_SUCCESS,S_WARN}from'@clack/prompts' |
3 | 4 | import{createDebug}from'obug' |
| 5 | +import{isCI}from'std-env' |
4 | 6 |
|
5 | 7 | import{blankLineBefore,writeDirect}from'./stdout' |
6 | 8 |
|
7 | 9 | typeLoggerImpl=Pick<typeoflog,'info'|'warn'|'error'|'success'|'step'|'message'> |
8 | 10 |
|
9 | | -letimpl: LoggerImpl=log |
| 11 | +typeLineColor='blue'|'yellow'|'red'|'green' |
| 12 | + |
| 13 | +/** |
| 14 | + * A log line with no clack framing: the `│` gutter connects one prompt to the |
| 15 | + * next on screen, and in a redirected log it is a bare column of punctuation |
| 16 | + * between every line of real output. |
| 17 | + */ |
| 18 | +functionplainLine(symbol: string,color: LineColor,message?: string): void{ |
| 19 | +constbody=String(message??'').replace(/\n/g,'\n ') |
| 20 | +writeDirect(`${styleText(color,symbol)}${body}\n`) |
| 21 | +} |
| 22 | + |
| 23 | +constplain: LoggerImpl={ |
| 24 | +info: message=>plainLine(S_INFO,'blue',message), |
| 25 | +warn: message=>plainLine(S_WARN,'yellow',message), |
| 26 | +error: message=>plainLine(S_ERROR,'red',message), |
| 27 | +success: message=>plainLine(S_SUCCESS,'green',message), |
| 28 | +step: message=>plainLine(S_STEP_SUBMIT,'green',message), |
| 29 | +message: message=>writeDirect(`${String(message??'')}\n`), |
| 30 | +} |
| 31 | + |
| 32 | +functiondefaultImpl(): LoggerImpl{ |
| 33 | +return!process.stdout.isTTY||isCI ? plain : log |
| 34 | +} |
| 35 | + |
| 36 | +letimpl: LoggerImpl|undefined |
10 | 37 | letdepth=0 |
11 | 38 |
|
12 | 39 | /** |
@@ -36,16 +63,37 @@ function emit<T>(write: () => T): T { |
36 | 63 | * guideline, which reads as a stray artefact next to a persistent footer. |
37 | 64 | */ |
38 | 65 | exportfunctionsetLoggerImpl(next?: LoggerImpl): void{ |
39 | | -impl=next??log |
| 66 | +impl=next |
40 | 67 | } |
41 | 68 |
|
42 | 69 | exportconstlogger: LoggerImpl={ |
43 | | -info: message=>emit(()=>impl.info(message)), |
44 | | -warn: message=>emit(()=>impl.warn(message)), |
45 | | -error: message=>emit(()=>impl.error(message)), |
46 | | -success: message=>emit(()=>impl.success(message)), |
47 | | -step: message=>emit(()=>impl.step(message)), |
48 | | -message: (message,options)=>emit(()=>impl.message(message,options)), |
| 70 | +info: message=>emit(()=>(impl??defaultImpl()).info(message)), |
| 71 | +warn: message=>emit(()=>(impl??defaultImpl()).warn(message)), |
| 72 | +error: message=>emit(()=>(impl??defaultImpl()).error(message)), |
| 73 | +success: message=>emit(()=>(impl??defaultImpl()).success(message)), |
| 74 | +step: message=>emit(()=>(impl??defaultImpl()).step(message)), |
| 75 | +message: (message,options)=>emit(()=>(impl??defaultImpl()).message(message,options)), |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Open a command's output. Falls back to a bare headline where clack's opening |
| 80 | + * corner would only introduce a gutter nothing draws against. |
| 81 | + */ |
| 82 | +exportfunctionintro(message: string): void{ |
| 83 | +if(!process.stdout.isTTY||isCI){ |
| 84 | +writeDirect(`${message}\n`) |
| 85 | +return |
| 86 | +} |
| 87 | +clackIntro(message) |
| 88 | +} |
| 89 | + |
| 90 | +/** {@link intro}, for the line a command finishes on. */ |
| 91 | +exportfunctionoutro(message: string): void{ |
| 92 | +if(!process.stdout.isTTY||isCI){ |
| 93 | +writeDirect(`${styleText('green',S_SUCCESS)}${message}\n`) |
| 94 | +return |
| 95 | +} |
| 96 | +clackOutro(message) |
49 | 97 | } |
50 | 98 |
|
51 | 99 | exportconstdebug=createDebug('nuxi') |
|
0 commit comments