Move validation into resolvePackagePaths

Signed-off-by: Juan Escalada <juanescalada175@gmail.com>
This commit is contained in:
Juan Escalada
2024-11-26 12:15:11 +09:00
parent 58ec127c52
commit da94fabdac
2 changed files with 17 additions and 21 deletions
@@ -24,7 +24,6 @@ import {
} from './api-extractor';
import { paths as cliPaths, resolvePackagePaths } from '../../lib/paths';
import { generateTypeDeclarations } from './generateTypeDeclarations';
import fs from 'fs/promises';
type Options = {
ci?: boolean;
@@ -50,10 +49,6 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => {
const isAllPackages = !paths?.length;
if (!isAllPackages) {
checkValidPaths(paths);
}
const selectedPackageDirs = await resolvePackagePaths({
paths,
include: opts.include,
@@ -143,19 +138,3 @@ function parseArrayOption(value: string | undefined) {
.filter(Boolean)
: [];
}
/**
* Checks if the provided paths are valid.
*
* @throws Error if any of the paths are invalid.
* @param paths An array of paths to check.
*/
function checkValidPaths(paths: string[]) {
paths.forEach(async path => {
try {
await fs.access(path);
} catch (error) {
throw new Error(`Invalid path provided: ${path}`);
}
});
}
+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(