Merge pull request #27752 from jescalada/api-reports-script-fix

Fix api-reports space-after-comma bug
This commit is contained in:
Fredrik Adelöw
2024-11-26 10:34:23 +01:00
committed by GitHub
4 changed files with 47 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/repo-tools': minor
---
Fix invalid path and malformed flags bugs in api-reports.ts
@@ -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 () => {
@@ -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,
@@ -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
View File
@@ -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(