11import * as core from "@arethetypeswrong/core" ;
2+ import { filterProblems , problemAffectsEntrypoint , problemKindInfo } from "@arethetypeswrong/core/problems" ;
23import { allResolutionKinds , getResolutionOption , groupProblemsByKind } from "@arethetypeswrong/core/utils" ;
34import chalk from "chalk" ;
45import Table , { type GenericTable , type HorizontalTableRow } from "cli-table3" ;
56import { marked } from "marked" ;
6-
7- import { filterProblems , problemAffectsEntrypoint , problemKindInfo } from "@arethetypeswrong/core/problems" ;
8- import type { Opts } from "../index.js" ;
7+ import TerminalRenderer from "marked-terminal" ;
98import { moduleKinds , problemFlags , resolutionKinds } from "../problemUtils.js" ;
109import { asciiTable } from "./asciiTable.js" ;
11- import TerminalRenderer from "marked-terminal " ;
10+ import type { RenderOptions } from "./index.js " ;
1211
13- export async function typed ( analysis : core . Analysis , opts : Opts ) {
14- const problems = analysis . problems . filter ( ( problem ) => ! opts . ignoreRules || ! opts . ignoreRules . includes ( problem . kind ) ) ;
12+ export async function typed ( analysis : core . Analysis , opts : RenderOptions ) : Promise < string > {
13+ let output = "" ;
14+ const problems = analysis . problems . filter (
15+ ( problem ) => ! opts . ignoreRules || ! opts . ignoreRules . includes ( problemFlags [ problem . kind ] ) ,
16+ ) ;
1517 const grouped = groupProblemsByKind ( problems ) ;
1618 const entrypoints = Object . keys ( analysis . entrypoints ) ;
1719 marked . setOptions ( {
1820 renderer : new TerminalRenderer ( ) ,
1921 } ) ;
2022
21- console . log ( `${ analysis . packageName } v${ analysis . packageVersion } ` ) ;
23+ out ( `${ analysis . packageName } v${ analysis . packageVersion } ` ) ;
2224 if ( analysis . types . kind === "@types" ) {
23- console . log ( `${ analysis . types . packageName } v${ analysis . types . packageVersion } ` ) ;
25+ out ( `${ analysis . types . packageName } v${ analysis . types . packageVersion } ` ) ;
2426 }
25- console . log ( ) ;
27+ out ( ) ;
2628 if ( Object . keys ( analysis . buildTools ) . length ) {
27- console . log ( "Build tools:" ) ;
28- console . log (
29+ out ( "Build tools:" ) ;
30+ out (
2931 Object . entries ( analysis . buildTools )
3032 . map ( ( [ tool , version ] ) => {
3133 return `- ${ tool } @${ version } ` ;
3234 } )
3335 . join ( "\n" ) ,
3436 ) ;
35- console . log ( ) ;
37+ out ( ) ;
3638 }
3739
3840 if ( opts . ignoreRules && opts . ignoreRules . length ) {
39- console . log (
40- chalk . gray (
41- ` (ignoring rules: ${ opts . ignoreRules
42- . map ( ( rule ) => `'${ problemFlags [ rule as core . ProblemKind ] } '` )
43- . join ( ", " ) } )\n`,
44- ) ,
45- ) ;
41+ out ( chalk . gray ( ` (ignoring rules: ${ opts . ignoreRules . map ( ( rule ) => `'${ rule } '` ) . join ( ", " ) } )\n` ) ) ;
4642 }
4743
4844 if ( opts . summary ) {
@@ -54,7 +50,7 @@ export async function typed(analysis: core.Analysis, opts: Opts) {
5450 return `${ emoji } ${ description } ` ;
5551 } ) ;
5652
57- console . log ( summaryTexts . join ( "" ) || defaultSummary ) ;
53+ out ( summaryTexts . join ( "" ) || defaultSummary ) ;
5854 }
5955
6056 const entrypointNames = entrypoints . map (
@@ -119,25 +115,31 @@ export async function typed(analysis: core.Analysis, opts: Opts) {
119115
120116 switch ( opts . format ) {
121117 case "table" :
122- console . log ( table ! . toString ( ) ) ;
118+ out ( table ! . toString ( ) ) ;
123119 break ;
124120 case "table-flipped" :
125- console . log ( flippedTable ! . toString ( ) ) ;
121+ out ( flippedTable ! . toString ( ) ) ;
126122 break ;
127123 case "ascii" :
128- console . log ( asciiTable ( table ! ) ) ;
124+ out ( asciiTable ( table ! ) ) ;
129125 break ;
130126 case "auto" :
131127 const terminalWidth = process . stdout . columns || 133 ; // This looks like GitHub Actions' width
132128 if ( table ! . width <= terminalWidth ) {
133- console . log ( table ! . toString ( ) ) ;
129+ out ( table ! . toString ( ) ) ;
134130 } else if ( flippedTable ! . width <= terminalWidth ) {
135- console . log ( flippedTable ! . toString ( ) ) ;
131+ out ( flippedTable ! . toString ( ) ) ;
136132 } else {
137- console . log ( asciiTable ( table ! ) ) ;
133+ out ( asciiTable ( table ! ) ) ;
138134 }
139135 break ;
140136 }
137+
138+ return output . trimEnd ( ) ;
139+
140+ function out ( s : string = "" ) {
141+ output += s + "\n" ;
142+ }
141143}
142144
143145function memo < Args extends ( string | number ) [ ] , Result > ( fn : ( ...args : Args ) => Result ) : ( ...args : Args ) => Result {
0 commit comments