Skip to content

Commit 2b8c748

Browse files
committed
render using output* functions
1 parent af1e6d1 commit 2b8c748

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

packages/cli-kit/src/private/node/ui.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {collectLog, consoleWarn, Logger, LogLevel, outputWhereAppropriate} from '../../public/node/output.js'
1+
import {output, Logger, LogLevel} from '../../public/node/output.js'
22
import {isUnitTest} from '../../public/node/context/local.js'
33
import {treeKill} from '../../public/node/tree-kill.js'
44
import {ReactElement} from 'react'
@@ -11,20 +11,16 @@ interface RenderOnceOptions {
1111
renderOptions?: RenderOptions
1212
}
1313

14-
export function renderOnce(
15-
element: JSX.Element,
16-
{logLevel = 'info', logger = consoleWarn, renderOptions}: RenderOnceOptions,
17-
) {
18-
const {output, unmount} = renderString(element, renderOptions)
14+
export function renderOnce(element: JSX.Element, {logLevel = 'info', renderOptions}: RenderOnceOptions) {
15+
const {output: renderedString, unmount} = renderString(element, renderOptions)
1916

20-
if (output) {
21-
if (isUnitTest()) collectLog(logLevel, output)
22-
outputWhereAppropriate(logLevel, logger, output)
17+
if (renderedString) {
18+
output(renderedString, logLevel)
2319
}
2420

2521
unmount()
2622

27-
return output
23+
return renderedString
2824
}
2925

3026
export async function render(element: JSX.Element, options?: RenderOptions) {

packages/cli-kit/src/private/node/ui/alert.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Alert, AlertProps} from './components/Alert.js'
22
import {renderOnce} from '../ui.js'
3-
import {consoleError, consoleLog, consoleWarn, Logger, LogLevel} from '../../../public/node/output.js'
3+
import {LogLevel} from '../../../public/node/output.js'
44
import React from 'react'
55
import {RenderOptions} from 'ink'
66

@@ -11,13 +11,6 @@ const typeToLogLevel: {[key in AlertProps['type']]: LogLevel} = {
1111
error: 'error',
1212
}
1313

14-
const typeToLogger: {[key in AlertProps['type']]: Logger} = {
15-
info: consoleWarn,
16-
warning: consoleWarn,
17-
success: consoleLog,
18-
error: consoleError,
19-
}
20-
2114
export interface AlertOptions extends AlertProps {
2215
renderOptions?: RenderOptions
2316
}
@@ -47,6 +40,6 @@ export function alert({
4740
orderedNextSteps={orderedNextSteps}
4841
customSections={customSections}
4942
/>,
50-
{logLevel: typeToLogLevel[type], logger: typeToLogger[type], renderOptions},
43+
{logLevel: typeToLogLevel[type], renderOptions},
5144
)
5245
}

packages/cli-kit/src/public/node/output.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,20 @@ export function outputWarn(content: OutputMessage, logger: Logger = consoleWarn)
332332
outputWhereAppropriate('warn', logger, message)
333333
}
334334

335+
/**
336+
* Logs an unformatted message at the given log level.
337+
* Note: By default, messages are sent through the standard error.
338+
*
339+
* @param content - The content to be output to the user.
340+
* @param logLevel - The log level associated with the message.
341+
* @param logger - The logging function to use to output to the user.
342+
*/
343+
export function output(content: OutputMessage, logLevel: LogLevel = 'info', logger: Logger = consoleWarn): void {
344+
if (isUnitTest()) collectLog(logLevel, content)
345+
const message = stringifyMessage(content)
346+
outputWhereAppropriate(logLevel, logger, message)
347+
}
348+
335349
/**
336350
* Prints a new line in the terminal.
337351
*/
@@ -382,25 +396,16 @@ export interface OutputProcess {
382396
*
383397
* @param message - The message to print.
384398
*/
385-
export function consoleLog(message: string): void {
399+
function consoleLog(message: string): void {
386400
process.stdout.write(`${withOrWithoutStyle(message)}\n`)
387401
}
388402

389-
/**
390-
* Prints an error message in the console to stderr.
391-
*
392-
* @param message - The message to print.
393-
*/
394-
export function consoleError(message: string): void {
395-
process.stderr.write(`${withOrWithoutStyle(message)}\n`)
396-
}
397-
398403
/**
399404
* Prints a warning message in the console to stderr.
400405
*
401406
* @param message - The message to print.
402407
*/
403-
export function consoleWarn(message: string): void {
408+
function consoleWarn(message: string): void {
404409
process.stderr.write(`${withOrWithoutStyle(message)}\n`)
405410
}
406411

packages/cli-kit/src/public/node/ui.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
/* eslint-disable tsdoc/syntax */
33
import {AbortError, AbortSilentError, FatalError as Fatal} from './error.js'
4-
import {
5-
collectLog,
6-
consoleError,
7-
consoleWarn,
8-
Logger,
9-
LogLevel,
10-
outputContent,
11-
outputDebug,
12-
outputToken,
13-
outputWhereAppropriate,
14-
} from './output.js'
15-
import {isUnitTest} from './context/local.js'
4+
import {Logger, LogLevel, output, outputContent, outputDebug, outputToken} from './output.js'
165
import {terminalSupportsPrompting} from './system.js'
176
import {AbortController} from './abort.js'
187
import {runWithTimer} from './metadata.js'
@@ -247,7 +236,7 @@ interface RenderFatalErrorOptions {
247236
*/
248237
// eslint-disable-next-line max-params
249238
export function renderFatalError(error: Fatal, {renderOptions}: RenderFatalErrorOptions = {}) {
250-
return renderOnce(<FatalError error={error} />, {logLevel: 'error', logger: consoleError, renderOptions})
239+
return renderOnce(<FatalError error={error} />, {logLevel: 'error', renderOptions})
251240
}
252241

253242
export interface RenderSelectPromptOptions<T> extends Omit<SelectPromptProps<T>, 'onSubmit'> {
@@ -594,12 +583,12 @@ interface RenderTextOptions {
594583
* Hello world!
595584
*
596585
*/
597-
export function renderText({text, logLevel = 'info', logger = consoleWarn}: RenderTextOptions) {
586+
export function renderText({text, logLevel = 'info'}: RenderTextOptions) {
598587
let textWithLineReturn = text
599588
if (!text.endsWith('\n')) textWithLineReturn += '\n'
600589

601-
if (isUnitTest()) collectLog(logLevel, textWithLineReturn)
602-
outputWhereAppropriate(logLevel, logger, textWithLineReturn)
590+
output(textWithLineReturn, logLevel)
591+
603592
return textWithLineReturn
604593
}
605594

packages/cli/src/cli/commands/debug/command-flags.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Command from '@shopify/cli-kit/node/base-command'
2-
import {consoleLog} from '@shopify/cli-kit/node/output'
3-
import {renderTable, renderText} from '@shopify/cli-kit/node/ui'
2+
import {outputResult} from '@shopify/cli-kit/node/output'
3+
import {renderTable} from '@shopify/cli-kit/node/ui'
44
import {Flags} from '@oclif/core'
55

66
export default class CommandFlags extends Command {
@@ -53,7 +53,7 @@ export default class CommandFlags extends Command {
5353
const header = `${columns.join(',')}\n`
5454
const rows = data.map((obj) => columns.map((key) => obj[key]).join(',')).join('\n')
5555
const csvString = header + rows
56-
renderText({text: csvString, logger: consoleLog})
56+
outputResult(csvString)
5757
} else {
5858
renderTable({
5959
rows: data,

0 commit comments

Comments
 (0)