Skip to content

Commit

Permalink
feat(macro-usage-report): add CSV output format (#9996)
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg authored Nov 10, 2023
1 parent 0d4d603 commit 85e9afa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
4 changes: 2 additions & 2 deletions tool/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand Down Expand Up @@ -1227,7 +1227,7 @@ if (Mozilla && !Mozilla.dntEnabled()) {
.option("--deprecated-only", "Only reports deprecated macros.")
.option("--format <type>", "Format of the report.", {
default: "md-table",
validator: ["json", "md-table"],
validator: ["json", "md-table", "csv"],
})
.option("--unused-only", "Only reports unused macros.")
.action(
Expand Down
85 changes: 58 additions & 27 deletions tool/macro-usage-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,11 @@ function formatCell(files: string[]): string {
return `<span title="${files[0]} …">${files.length}</span>`;
}

async function writeMarkdownTable(
function createTable(
filesByMacro: {
[macro: string]: Iterable<string>;
},
{
deprecatedMacros,
}: {
deprecatedMacros: string[];
}
deprecatedMacros: string[]
) {
const columns = ["yari"];
const paths = [MACROS_PATH];
Expand All @@ -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<string>;
},
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<string>;
},
{
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<string>;
},
deprecatedMacros: string[]
) {
const result = {};
const macros = Object.keys(filesByMacro);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}

0 comments on commit 85e9afa

Please sign in to comment.