From 58ec127c52256bda7dd2829e73feda2b79ea7445 Mon Sep 17 00:00:00 2001 From: Juan Escalada Date: Tue, 26 Nov 2024 11:18:31 +0900 Subject: [PATCH] Add path validation and remove -o flag check Signed-off-by: Juan Escalada --- .../src/commands/api-reports/api-reports.ts | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/repo-tools/src/commands/api-reports/api-reports.ts b/packages/repo-tools/src/commands/api-reports/api-reports.ts index 0603188dac..b8b9f9fb23 100644 --- a/packages/repo-tools/src/commands/api-reports/api-reports.ts +++ b/packages/repo-tools/src/commands/api-reports/api-reports.ts @@ -24,6 +24,7 @@ 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; @@ -45,17 +46,14 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => { const runTsc = opts.tsc; const allowWarnings = parseArrayOption(opts.allowWarnings); const allowAllWarnings = opts.allowAllWarnings; - const omitMessagesRaw = opts.omitMessages; - - if (omitMessagesRaw && omitMessagesRaw.endsWith(',')) { - throw new Error( - `Invalid value for --omit-messages: ${omitMessagesRaw}\nMust be a comma-separated list of strings without spaces or wrapped in quotations.`, - ); - } - - const omitMessages = parseArrayOption(omitMessagesRaw); + const omitMessages = parseArrayOption(opts.omitMessages); const isAllPackages = !paths?.length; + + if (!isAllPackages) { + checkValidPaths(paths); + } + const selectedPackageDirs = await resolvePackagePaths({ paths, include: opts.include, @@ -138,5 +136,26 @@ 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) + : []; +} + +/** + * 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}`); + } + }); }