Skip to content

Commit

Permalink
feat: add markdown output format to stats command (#1395)
Browse files Browse the repository at this point in the history
* fix: Remove non-json format elements from JSON output

* feat: Add markdown output format, suitable for use in GitHub Job Summaries

* chore: run prettier, update snapshot for existing tests

* chore: add tests for all the stats output formats

* chore: add museum file, run prettier

* fix: Tidy up some ends spotted by eslint

* fix: Tidy up some ends spotted by eslint

* feat: reinstate debug/timing output for stylish format only

* chore: update snapshots with timing output

* chore: run prettier

* feat: Add markdown table formatting to stats markdown output

* chore: update snapshots for new output format

* docs: Add changeset for markdown format option in stats command
  • Loading branch information
lornajane authored Jan 31, 2024
1 parent aa65490 commit 4868a5b
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-waves-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/cli": patch
---

Added markdown format option to stats command for use with GitHub job summaries.
31 changes: 31 additions & 0 deletions __tests__/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,35 @@ describe('E2E', () => {
expect(fs.statSync(join(testPath, 'nested/redoc-static.html')).size).toEqual(33016);
});
});

describe('stats', () => {
const folderPath = join(__dirname, 'stats');

test('stats should produce correct output (stylish format)', () => {
const testPath = join(folderPath, 'stats-stylish');
const args = getParams('../../../packages/cli/src/index.ts', 'stats', ['museum.yaml']);
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});

test('stats should produce correct JSON output', () => {
const testPath = join(folderPath, 'stats-json');
const args = getParams('../../../packages/cli/src/index.ts', 'stats', [
'museum.yaml',
'--format=json',
]);
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});

test('stats should produce correct Markdown format', () => {
const testPath = join(folderPath, 'stats-markdown');
const args = getParams('../../../packages/cli/src/index.ts', 'stats', [
'museum.yaml',
'--format=markdown',
]);
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});
});
});
1 change: 1 addition & 0 deletions __tests__/stats/stats-json/museum.yaml
39 changes: 39 additions & 0 deletions __tests__/stats/stats-json/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E stats stats should produce correct JSON output 1`] = `
{
"refs": {
"metric": "🚗 References",
"total": 39
},
"externalDocs": {
"metric": "📦 External Documents",
"total": 0
},
"schemas": {
"metric": "📈 Schemas",
"total": 22
},
"parameters": {
"metric": "👉 Parameters",
"total": 6
},
"links": {
"metric": "🔗 Links",
"total": 0
},
"pathItems": {
"metric": "➡️ Path Items",
"total": 5
},
"operations": {
"metric": "👷 Operations",
"total": 8
},
"tags": {
"metric": "🔖 Tags",
"total": 3
}
}
`;
1 change: 1 addition & 0 deletions __tests__/stats/stats-markdown/museum.yaml
16 changes: 16 additions & 0 deletions __tests__/stats/stats-markdown/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E stats stats should produce correct Markdown format 1`] = `
| Feature | Count |
| --- | --- |
| 🚗 References | 39 |
| 📦 External Documents | 0 |
| 📈 Schemas | 22 |
| 👉 Parameters | 6 |
| 🔗 Links | 0 |
| ➡️ Path Items | 5 |
| 👷 Operations | 8 |
| 🔖 Tags | 3 |
`;
1 change: 1 addition & 0 deletions __tests__/stats/stats-stylish/museum.yaml
19 changes: 19 additions & 0 deletions __tests__/stats/stats-stylish/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E stats stats should produce correct output (stylish format) 1`] = `
Document: museum.yaml stats:
🚗 References: 39
📦 External Documents: 0
📈 Schemas: 22
👉 Parameters: 6
🔗 Links: 0
➡️ Path Items: 5
👷 Operations: 8
🔖 Tags: 3
museum.yaml: stats processed in <test>ms
`;
29 changes: 25 additions & 4 deletions packages/cli/src/commands/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,37 @@ function printStatsJson(statsAccumulator: StatsAccumulator) {
process.stdout.write(JSON.stringify(json, null, 2));
}

function printStats(statsAccumulator: StatsAccumulator, api: string, format: string) {
process.stderr.write(`Document: ${colors.magenta(api)} stats:\n\n`);
function printStatsMarkdown(statsAccumulator: StatsAccumulator) {
let output = '| Feature | Count |\n| --- | --- |\n';
for (const key of Object.keys(statsAccumulator)) {
output +=
'| ' +
statsAccumulator[key as StatsName].metric +
' | ' +
statsAccumulator[key as StatsName].total +
' |\n';
}
process.stdout.write(output);
}

function printStats(
statsAccumulator: StatsAccumulator,
api: string,
startedAt: number,
format: string
) {
switch (format) {
case 'stylish':
process.stderr.write(`Document: ${colors.magenta(api)} stats:\n\n`);
printStatsStylish(statsAccumulator);
printExecutionTime('stats', startedAt, api);
break;
case 'json':
printStatsJson(statsAccumulator);
break;
case 'markdown':
printStatsMarkdown(statsAccumulator);
break;
}
}

Expand Down Expand Up @@ -107,6 +129,5 @@ export async function handleStats(argv: StatsOptions, config: Config) {
ctx,
});

printStats(statsAccumulator, path, argv.format);
printExecutionTime('stats', startedAt, path);
printStats(statsAccumulator, path, startedAt, argv.format);
}
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ yargs
},
format: {
description: 'Use a specific output format.',
choices: ['stylish', 'json'] as ReadonlyArray<OutputFormat>,
choices: ['stylish', 'json', 'markdown'] as ReadonlyArray<OutputFormat>,
default: 'stylish' as OutputFormat,
},
}),
Expand Down
Loading

1 comment on commit 4868a5b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 76.1% 4306/5658
🟡 Branches 65.75% 2236/3401
🟡 Functions 68.74% 695/1011
🟡 Lines 76.31% 4049/5306

Test suite run success

699 tests passing in 100 suites.

Report generated by 🧪jest coverage report action from 4868a5b

Please sign in to comment.