@@ -132,15 +132,11 @@ function formatCell(files: string[]): string {
132
132
return `<span title="${ files [ 0 ] } …">${ files . length } </span>` ;
133
133
}
134
134
135
- async function writeMarkdownTable (
135
+ function createTable (
136
136
filesByMacro : {
137
137
[ macro : string ] : Iterable < string > ;
138
138
} ,
139
- {
140
- deprecatedMacros,
141
- } : {
142
- deprecatedMacros : string [ ] ;
143
- }
139
+ deprecatedMacros : string [ ]
144
140
) {
145
141
const columns = [ "yari" ] ;
146
142
const paths = [ MACROS_PATH ] ;
@@ -153,39 +149,71 @@ async function writeMarkdownTable(
153
149
}
154
150
}
155
151
156
- process . stdout . write (
157
- `| macro |${ columns . map ( ( column ) => ` ${ column } ` ) . join ( "|" ) } |\n`
158
- ) ;
159
- process . stdout . write (
160
- `|:----- |${ columns
161
- . map ( ( column ) => ` ${ "-" . repeat ( column . length ) } :` )
162
- . join ( "|" ) } |\n`
163
- ) ;
152
+ const table : any [ ] [ ] = [ [ "macro" , ...columns ] ] ;
164
153
165
154
const macros = Object . keys ( filesByMacro ) ;
166
-
167
155
for ( const macro of macros ) {
168
156
const files = filesByMacro [ macro ] ;
169
157
const macroCell = deprecatedMacros . includes ( macro ) ? `${ macro } 🗑` : macro ;
170
158
171
- const cells = [
159
+ table . push ( [
172
160
macroCell ,
173
- ...paths . map ( ( path ) => formatCell ( filterFilesByBase ( files , path ) ) ) ,
174
- ] ;
161
+ ...paths . map ( ( path ) => filterFilesByBase ( files , path ) ) ,
162
+ ] ) ;
163
+ }
164
+
165
+ return table ;
166
+ }
167
+
168
+ function writeMarkdownTable (
169
+ filesByMacro : {
170
+ [ macro : string ] : Iterable < string > ;
171
+ } ,
172
+ deprecatedMacros : string [ ]
173
+ ) {
174
+ const table = createTable ( filesByMacro , deprecatedMacros ) ;
175
+ const headerRow = table . shift ( ) ;
175
176
176
- process . stdout . write ( `|${ cells . map ( ( cell ) => ` ${ cell } ` ) . join ( "|" ) } |\n` ) ;
177
+ process . stdout . write ( `| ${ headerRow . join ( " | " ) } |\n` ) ;
178
+ process . stdout . write (
179
+ `|:----- |${ headerRow
180
+ . slice ( 1 )
181
+ . map ( ( column ) => ` ${ "-" . repeat ( column . length ) } :` )
182
+ . join ( "|" ) } |\n`
183
+ ) ;
184
+
185
+ for ( const row of table ) {
186
+ process . stdout . write (
187
+ `| ${ row
188
+ . map ( ( cell ) =>
189
+ Array . isArray ( cell ) ? ` ${ formatCell ( cell ) } ` : ` ${ cell } `
190
+ )
191
+ . join ( " | " ) } |\n`
192
+ ) ;
177
193
}
178
194
}
179
195
180
- function writeJson (
196
+ function writeCsvTable (
181
197
filesByMacro : {
182
198
[ macro : string ] : Iterable < string > ;
183
199
} ,
184
- {
185
- deprecatedMacros,
186
- } : {
187
- deprecatedMacros : string [ ] ;
200
+ deprecatedMacros : string [ ]
201
+ ) {
202
+ const table = createTable ( filesByMacro , deprecatedMacros ) ;
203
+ for ( const row of table ) {
204
+ process . stdout . write (
205
+ `${ row
206
+ . map ( ( cell ) => ( Array . isArray ( cell ) ? `${ cell . length } ` : `${ cell } ` ) )
207
+ . join ( "," ) } \n`
208
+ ) ;
188
209
}
210
+ }
211
+
212
+ function writeJson (
213
+ filesByMacro : {
214
+ [ macro : string ] : Iterable < string > ;
215
+ } ,
216
+ deprecatedMacros : string [ ]
189
217
) {
190
218
const result = { } ;
191
219
const macros = Object . keys ( filesByMacro ) ;
@@ -215,7 +243,7 @@ export async function macroUsageReport({
215
243
unusedOnly,
216
244
} : {
217
245
deprecatedOnly : boolean ;
218
- format : "md-table" | "json" ;
246
+ format : "md-table" | "csv" | " json";
219
247
unusedOnly : boolean ;
220
248
} ) {
221
249
const macros = await getMacros ( ) ;
@@ -235,9 +263,12 @@ export async function macroUsageReport({
235
263
236
264
switch ( format ) {
237
265
case "md-table" :
238
- return writeMarkdownTable ( filesByMacro , { deprecatedMacros } ) ;
266
+ return writeMarkdownTable ( filesByMacro , deprecatedMacros ) ;
267
+
268
+ case "csv" :
269
+ return writeCsvTable ( filesByMacro , deprecatedMacros ) ;
239
270
240
271
case "json" :
241
- return writeJson ( filesByMacro , { deprecatedMacros } ) ;
272
+ return writeJson ( filesByMacro , deprecatedMacros ) ;
242
273
}
243
274
}
0 commit comments