add cofig options to allow warnings and omit messages

Signed-off-by: Juan Pablo Garcia Ripa <juanpablog@spotify.com>
This commit is contained in:
Juan Pablo Garcia Ripa
2022-11-28 18:10:10 +01:00
parent 80d9674a4a
commit b56bfd12de
7 changed files with 86 additions and 25 deletions
+3
View File
@@ -26,6 +26,9 @@ Options:
--ci
--tsc
--docs
--allow-warnings [allowWarningsPaths...]
--folders <folders...>
--omitMessages <messageCodes...>
-h, --help
```
+1
View File
@@ -30,6 +30,7 @@
"backstage-repo-tools": "bin/backstage-repo-tools"
},
"dependencies": {
"@backstage/cli-common": "workspace:^",
"@backstage/errors": "workspace:^",
"@manypkg/get-packages": "^1.1.3",
"@microsoft/api-documenter": "^7.17.11",
@@ -65,9 +65,10 @@ import {
} from '@microsoft/api-documenter/lib/markdown/CustomMarkdownEmitter';
import { IMarkdownEmitterContext } from '@microsoft/api-documenter/lib/markdown/MarkdownEmitter';
import { AstDeclaration } from '@microsoft/api-extractor/lib/analyzer/AstDeclaration';
import { paths as cliPaths } from '../../lib/paths';
const tmpDir = resolvePath(
process.cwd(),
cliPaths.targetRoot,
'./node_modules/.cache/api-extractor',
);
@@ -219,21 +220,10 @@ ApiReportGenerator.generateReviewFileContent =
});
};
const PACKAGE_ROOTS = ['packages', 'plugins'];
const ALLOW_WARNINGS = [
'packages/core-components',
'plugins/catalog',
'plugins/catalog-import',
'plugins/git-release-manager',
'plugins/jenkins',
'plugins/kubernetes',
];
async function resolvePackagePath(
packagePath: string,
): Promise<string | undefined> {
const projectRoot = resolvePath(process.cwd());
const projectRoot = resolvePath(cliPaths.targetRoot);
const fullPackageDir = resolvePath(projectRoot, packagePath);
const stat = await fs.stat(fullPackageDir);
@@ -269,11 +259,11 @@ export async function findSpecificPackageDirs(unresolvedPackageDirs: string[]) {
return packageDirs;
}
export async function findPackageDirs() {
export async function findPackageDirs(packageRoots: string[]) {
const packageDirs = new Array<string>();
const projectRoot = resolvePath(process.cwd());
const projectRoot = resolvePath(cliPaths.targetRoot);
for (const packageRoot of PACKAGE_ROOTS) {
for (const packageRoot of packageRoots) {
const dirs = await fs.readdir(resolvePath(projectRoot, packageRoot));
for (const dir of dirs) {
const packageDir = await resolvePackagePath(join(packageRoot, dir));
@@ -289,7 +279,7 @@ export async function findPackageDirs() {
}
export async function createTemporaryTsConfig(includedPackageDirs: string[]) {
const path = resolvePath(process.cwd(), 'tsconfig.tmp.json');
const path = resolvePath(cliPaths.targetRoot, 'tsconfig.tmp.json');
process.once('exit', () => {
fs.removeSync(path);
@@ -375,6 +365,8 @@ interface ApiExtractionOptions {
outputDir: string;
isLocalBuild: boolean;
tsconfigFilePath: string;
allowWarnings: boolean | string[];
omitMessages?: string[];
}
export async function runApiExtraction({
@@ -382,25 +374,39 @@ export async function runApiExtraction({
outputDir,
isLocalBuild,
tsconfigFilePath,
allowWarnings,
omitMessages = [],
}: ApiExtractionOptions) {
await fs.remove(outputDir);
const entryPoints = packageDirs.map(packageDir => {
return resolvePath(
process.cwd(),
cliPaths.targetRoot,
`./dist-types/${packageDir}/src/index.d.ts`,
);
});
let compilerState: CompilerState | undefined = undefined;
const allowWarningPkg = Array.isArray(allowWarnings) ? allowWarnings : [];
const messagesConf: { [key: string]: { logLevel: string } } = {};
for (const messageCode of omitMessages) {
messagesConf[messageCode] = {
logLevel: 'none',
};
}
const warnings = new Array<string>();
for (const packageDir of packageDirs) {
console.log(`## Processing ${packageDir}`);
const projectFolder = resolvePath(process.cwd(), packageDir);
const noBail = Array.isArray(allowWarnings)
? allowWarnings.includes(packageDir)
: allowWarnings;
const projectFolder = resolvePath(cliPaths.targetRoot, packageDir);
const packageFolder = resolvePath(
process.cwd(),
cliPaths.targetRoot,
'./dist-types',
packageDir,
);
@@ -453,6 +459,7 @@ export async function runApiExtraction({
logLevel: 'warning' as ExtractorLogLevel.Warning,
addToApiReportFile: true,
},
...messagesConf,
},
tsdocMessageReporting: {
default: {
@@ -543,12 +550,12 @@ export async function runApiExtraction({
}
const warningCountAfter = await countApiReportWarnings(projectFolder);
if (warningCountAfter > 0 && !ALLOW_WARNINGS.includes(packageDir)) {
if (warningCountAfter > 0 && !noBail) {
throw new Error(
`The API Report for ${packageDir} is not allowed to have warnings`,
);
}
if (warningCountAfter === 0 && ALLOW_WARNINGS.includes(packageDir)) {
if (warningCountAfter === 0 && allowWarningPkg.includes(packageDir)) {
console.log(
`No need to allow warnings for ${packageDir}, it does not have any`,
);
@@ -27,16 +27,29 @@ import {
runCliExtraction,
buildDocs,
} from './api-extractor';
import { paths as cliPaths } from '../../lib/paths';
export default async (paths: string[], opts: OptionValues) => {
console.log(opts);
console.log({
ownDir: cliPaths.ownDir,
ownRoot: cliPaths.ownRoot,
targetDir: cliPaths.targetDir,
targetRoot: cliPaths.targetRoot,
'process.cwd()': process.cwd(),
});
const tmpDir = resolvePath(
process.cwd(),
cliPaths.targetRoot,
'./node_modules/.cache/api-extractor',
);
const projectRoot = resolvePath(process.cwd());
const projectRoot = resolvePath(cliPaths.targetRoot);
const isCiBuild = opts.ci;
const isDocsBuild = opts.docs;
const runTsc = opts.tsc;
const packageRoots = opts.folders;
const allowWarnings: boolean | string[] = opts.allowWarnings;
const omitMessages = opts.omitMessages;
const selectedPackageDirs = await findSpecificPackageDirs(paths);
@@ -85,7 +98,8 @@ export default async (paths: string[], opts: OptionValues) => {
}
}
const packageDirs = selectedPackageDirs ?? (await findPackageDirs());
const packageDirs =
selectedPackageDirs ?? (await findPackageDirs(packageRoots));
const { tsPackageDirs, cliPackageDirs } = await categorizePackageDirs(
projectRoot,
@@ -99,6 +113,8 @@ export default async (paths: string[], opts: OptionValues) => {
outputDir: tmpDir,
isLocalBuild: !isCiBuild,
tsconfigFilePath,
allowWarnings,
omitMessages,
});
}
if (cliPackageDirs.length > 0) {
+13
View File
@@ -24,6 +24,19 @@ export function registerCommands(program: Command) {
.option('--ci', 'CI run checks that there is no changes on API reports')
.option('--tsc', 'executes the tsc compilation before extracting the APIs')
.option('--docs', 'generates the api documentation')
.option(
'--allow-warnings [allowWarningsPaths...]',
'continue processing packages after getting errors on selected packages',
false,
)
.option('--folders <folders...>', 'packages folder containers', [
'packages',
'plugins',
])
.option(
'--omitMessages <messageCodes...>',
'select some message code to be omited on the API Extractor (i.e ae-cyclic-inherit-doc)',
)
.description('Generate an API report for selected packages')
.action(
lazy(() => import('./api-reports/api-reports').then(m => m.default)),
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { findPaths } from '@backstage/cli-common';
/* eslint-disable-next-line no-restricted-syntax */
export const paths = findPaths(__dirname);
+1
View File
@@ -8467,6 +8467,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/repo-tools@workspace:packages/repo-tools"
dependencies:
"@backstage/cli-common": "workspace:^"
"@backstage/errors": "workspace:^"
"@manypkg/get-packages": ^1.1.3
"@microsoft/api-documenter": ^7.17.11