Skip to content

Commit

Permalink
Merge pull request backstage#27752 from jescalada/api-reports-script-fix
Browse files Browse the repository at this point in the history
Fix api-reports space-after-comma bug
  • Loading branch information
freben authored Nov 26, 2024
2 parents b24221d + ce46791 commit 1ac6c62
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/new-pandas-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/repo-tools': minor
---

Fix invalid path and malformed flags bugs in api-reports.ts
18 changes: 18 additions & 0 deletions packages/repo-tools/src/commands/api-reports/api-reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ describe('buildApiReports', () => {

expect(buildDocs).not.toHaveBeenCalled();
});
it('should throw an error if a path does not exist', async () => {
const paths = ['packages/package-a', 'packages/package-c'];
const opts = {};

await expect(buildApiReports(paths, opts)).rejects.toThrow(
'Invalid paths provided: packages/package-c',
);
});
it('should throw an error if an option is malformed', async () => {
const paths = ['ae-undocumented'];
const opts = {
omitMessages: 'ae-wrong-input-file-type,',
};

await expect(buildApiReports(paths, opts)).rejects.toThrow(
'Invalid paths provided: ae-undocumented',
);
});
});
describe('allowWarnings', () => {
it('should accept single path value', async () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/repo-tools/src/commands/api-reports/api-reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => {
const omitMessages = parseArrayOption(opts.omitMessages);

const isAllPackages = !paths?.length;

const selectedPackageDirs = await resolvePackagePaths({
paths,
include: opts.include,
Expand Down Expand Up @@ -130,5 +131,10 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => {
* // returns []
*/
function parseArrayOption(value: string | undefined) {
return value ? value.split(',').map(s => s.trim()) : [];
return value
? value
.split(',')
.map(s => s.trim())
.filter(Boolean)
: [];
}
17 changes: 17 additions & 0 deletions packages/repo-tools/src/lib/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ export async function resolvePackagePaths(
const { paths: providedPaths, include, exclude } = options;
let packages = await PackageGraph.listTargetPackages();

if (providedPaths && providedPaths.length > 0) {
const invalidPaths: string[] = [];
for (const path of providedPaths) {
const matches = packages.some(
({ dir }) =>
new Minimatch(path).match(relativePath(paths.targetRoot, dir)) ||
isChildPath(dir, path),
);
if (!matches) {
invalidPaths.push(path);
}
}
if (invalidPaths.length > 0) {
throw new Error(`Invalid paths provided: ${invalidPaths.join(', ')}`);
}
}

if (providedPaths && providedPaths.length > 0) {
packages = packages.filter(({ dir }) =>
providedPaths.some(
Expand Down

0 comments on commit 1ac6c62

Please sign in to comment.