diff --git a/tool/cli.ts b/tool/cli.ts index ead3e0622a6a..d79de953121b 100644 --- a/tool/cli.ts +++ b/tool/cli.ts @@ -213,7 +213,7 @@ interface OptimizeClientBuildActionParameters extends ActionParameters { interface MacroUsageReportActionParameters extends ActionParameters { options: { deprecatedOnly: boolean; - format: "md-table" | "json"; + format: "md-table" | "csv" | "json"; unusedOnly: boolean; }; } @@ -1227,7 +1227,7 @@ if (Mozilla && !Mozilla.dntEnabled()) { .option("--deprecated-only", "Only reports deprecated macros.") .option("--format ", "Format of the report.", { default: "md-table", - validator: ["json", "md-table"], + validator: ["json", "md-table", "csv"], }) .option("--unused-only", "Only reports unused macros.") .action( diff --git a/tool/macro-usage-report.ts b/tool/macro-usage-report.ts index c90e2992c133..bfb8c44d8bf8 100644 --- a/tool/macro-usage-report.ts +++ b/tool/macro-usage-report.ts @@ -132,15 +132,11 @@ function formatCell(files: string[]): string { return `${files.length}`; } -async function writeMarkdownTable( +function createTable( filesByMacro: { [macro: string]: Iterable; }, - { - deprecatedMacros, - }: { - deprecatedMacros: string[]; - } + deprecatedMacros: string[] ) { const columns = ["yari"]; const paths = [MACROS_PATH]; @@ -153,39 +149,71 @@ async function writeMarkdownTable( } } - process.stdout.write( - `| macro |${columns.map((column) => ` ${column} `).join("|")}|\n` - ); - process.stdout.write( - `|:----- |${columns - .map((column) => ` ${"-".repeat(column.length)}:`) - .join("|")}|\n` - ); + const table: any[][] = [["macro", ...columns]]; const macros = Object.keys(filesByMacro); - for (const macro of macros) { const files = filesByMacro[macro]; const macroCell = deprecatedMacros.includes(macro) ? `${macro} 🗑` : macro; - const cells = [ + table.push([ macroCell, - ...paths.map((path) => formatCell(filterFilesByBase(files, path))), - ]; + ...paths.map((path) => filterFilesByBase(files, path)), + ]); + } + + return table; +} + +function writeMarkdownTable( + filesByMacro: { + [macro: string]: Iterable; + }, + deprecatedMacros: string[] +) { + const table = createTable(filesByMacro, deprecatedMacros); + const headerRow = table.shift(); - process.stdout.write(`|${cells.map((cell) => ` ${cell} `).join("|")}|\n`); + process.stdout.write(`| ${headerRow.join(" | ")} |\n`); + process.stdout.write( + `|:----- |${headerRow + .slice(1) + .map((column) => ` ${"-".repeat(column.length)}:`) + .join("|")}|\n` + ); + + for (const row of table) { + process.stdout.write( + `| ${row + .map((cell) => + Array.isArray(cell) ? ` ${formatCell(cell)} ` : ` ${cell} ` + ) + .join(" | ")} |\n` + ); } } -function writeJson( +function writeCsvTable( filesByMacro: { [macro: string]: Iterable; }, - { - deprecatedMacros, - }: { - deprecatedMacros: string[]; + deprecatedMacros: string[] +) { + const table = createTable(filesByMacro, deprecatedMacros); + for (const row of table) { + process.stdout.write( + `${row + .map((cell) => (Array.isArray(cell) ? `${cell.length}` : `${cell}`)) + .join(",")}\n` + ); } +} + +function writeJson( + filesByMacro: { + [macro: string]: Iterable; + }, + deprecatedMacros: string[] ) { const result = {}; const macros = Object.keys(filesByMacro); @@ -215,7 +243,7 @@ export async function macroUsageReport({ unusedOnly, }: { deprecatedOnly: boolean; - format: "md-table" | "json"; + format: "md-table" | "csv" | "json"; unusedOnly: boolean; }) { const macros = await getMacros(); @@ -235,9 +263,12 @@ export async function macroUsageReport({ switch (format) { case "md-table": - return writeMarkdownTable(filesByMacro, { deprecatedMacros }); + return writeMarkdownTable(filesByMacro, deprecatedMacros); + + case "csv": + return writeCsvTable(filesByMacro, deprecatedMacros); case "json": - return writeJson(filesByMacro, { deprecatedMacros }); + return writeJson(filesByMacro, deprecatedMacros); } }