Skip to content

Commit 85e9afa

Browse files
authored
feat(macro-usage-report): add CSV output format (#9996)
1 parent 0d4d603 commit 85e9afa

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

Diff for: tool/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ interface OptimizeClientBuildActionParameters extends ActionParameters {
213213
interface MacroUsageReportActionParameters extends ActionParameters {
214214
options: {
215215
deprecatedOnly: boolean;
216-
format: "md-table" | "json";
216+
format: "md-table" | "csv" | "json";
217217
unusedOnly: boolean;
218218
};
219219
}
@@ -1227,7 +1227,7 @@ if (Mozilla && !Mozilla.dntEnabled()) {
12271227
.option("--deprecated-only", "Only reports deprecated macros.")
12281228
.option("--format <type>", "Format of the report.", {
12291229
default: "md-table",
1230-
validator: ["json", "md-table"],
1230+
validator: ["json", "md-table", "csv"],
12311231
})
12321232
.option("--unused-only", "Only reports unused macros.")
12331233
.action(

Diff for: tool/macro-usage-report.ts

+58-27
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,11 @@ function formatCell(files: string[]): string {
132132
return `<span title="${files[0]} …">${files.length}</span>`;
133133
}
134134

135-
async function writeMarkdownTable(
135+
function createTable(
136136
filesByMacro: {
137137
[macro: string]: Iterable<string>;
138138
},
139-
{
140-
deprecatedMacros,
141-
}: {
142-
deprecatedMacros: string[];
143-
}
139+
deprecatedMacros: string[]
144140
) {
145141
const columns = ["yari"];
146142
const paths = [MACROS_PATH];
@@ -153,39 +149,71 @@ async function writeMarkdownTable(
153149
}
154150
}
155151

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]];
164153

165154
const macros = Object.keys(filesByMacro);
166-
167155
for (const macro of macros) {
168156
const files = filesByMacro[macro];
169157
const macroCell = deprecatedMacros.includes(macro) ? `${macro} 🗑` : macro;
170158

171-
const cells = [
159+
table.push([
172160
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();
175176

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+
);
177193
}
178194
}
179195

180-
function writeJson(
196+
function writeCsvTable(
181197
filesByMacro: {
182198
[macro: string]: Iterable<string>;
183199
},
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+
);
188209
}
210+
}
211+
212+
function writeJson(
213+
filesByMacro: {
214+
[macro: string]: Iterable<string>;
215+
},
216+
deprecatedMacros: string[]
189217
) {
190218
const result = {};
191219
const macros = Object.keys(filesByMacro);
@@ -215,7 +243,7 @@ export async function macroUsageReport({
215243
unusedOnly,
216244
}: {
217245
deprecatedOnly: boolean;
218-
format: "md-table" | "json";
246+
format: "md-table" | "csv" | "json";
219247
unusedOnly: boolean;
220248
}) {
221249
const macros = await getMacros();
@@ -235,9 +263,12 @@ export async function macroUsageReport({
235263

236264
switch (format) {
237265
case "md-table":
238-
return writeMarkdownTable(filesByMacro, { deprecatedMacros });
266+
return writeMarkdownTable(filesByMacro, deprecatedMacros);
267+
268+
case "csv":
269+
return writeCsvTable(filesByMacro, deprecatedMacros);
239270

240271
case "json":
241-
return writeJson(filesByMacro, { deprecatedMacros });
272+
return writeJson(filesByMacro, deprecatedMacros);
242273
}
243274
}

0 commit comments

Comments
 (0)