diff --git a/scripts/api-extractor.ts b/scripts/api-extractor.ts index 1a00ef4c5e..731bf4ecf9 100644 --- a/scripts/api-extractor.ts +++ b/scripts/api-extractor.ts @@ -281,6 +281,35 @@ async function createTemporaryTsConfig(includedPackageDirs: string[]) { return path; } +async function countApiReportWarnings(projectFolder: string) { + const path = resolvePath(projectFolder, 'api-report.md'); + try { + const content = await fs.readFile(path, 'utf8'); + const lines = content.split('\n'); + + const lineWarnings = lines.filter(line => + line.includes('// Warning:'), + ).length; + + const trailerStart = lines.findIndex( + line => line === '// Warnings were encountered during analysis:', + ); + const trailerWarnings = + trailerStart === -1 + ? 0 + : lines.length - + trailerStart - + 4; /* 4 lines at the trailer and after are not warnings */ + + return lineWarnings + trailerWarnings; + } catch (error) { + if (error.code === 'ENOENT') { + return 0; + } + throw error; + } +} + async function getTsDocConfig() { const tsdocConfigFile = await TSDocConfigFile.loadFile( require.resolve('@microsoft/api-extractor/extends/tsdoc-base.json'), @@ -331,11 +360,15 @@ async function runApiExtraction({ let compilerState: CompilerState | undefined = undefined; + const warnings = new Array(); + for (const packageDir of packageDirs) { console.log(`## Processing ${packageDir}`); const projectFolder = resolvePath(__dirname, '..', packageDir); const packageFolder = resolvePath(__dirname, '../dist-types', packageDir); + const warningCountBefore = await countApiReportWarnings(projectFolder); + const extractorConfig = ExtractorConfig.prepare({ configObject: { mainEntryPointFilePath: resolvePath(packageFolder, 'src/index.d.ts'), @@ -470,6 +503,22 @@ async function runApiExtraction({ ` and ${extractorResult.warningCount} warnings`, ); } + + const warningCountAfter = await countApiReportWarnings(projectFolder); + if (warningCountAfter > warningCountBefore) { + warnings.push( + `The API Report for ${packageDir} introduces new warnings. ` + + 'Please fix these warnings in order to keep the API Reports tidy.', + ); + } + } + + if (warnings.length > 0) { + console.warn(); + for (const warning of warnings) { + console.warn(warning); + } + console.warn(); } }