scripts/api-extractor: warn when introducing new warnings

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-01-07 15:58:26 +01:00
parent c6c571dff0
commit ad0ceca026
+49
View File
@@ -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<string>();
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();
}
}