diff --git a/packages/repo-tools/cli-report.md b/packages/repo-tools/cli-report.md index 309ddad2a3..b2a171cf42 100644 --- a/packages/repo-tools/cli-report.md +++ b/packages/repo-tools/cli-report.md @@ -26,6 +26,9 @@ Options: --ci --tsc --docs + --allow-warnings [allowWarningsPaths...] + --folders + --omitMessages -h, --help ``` diff --git a/packages/repo-tools/package.json b/packages/repo-tools/package.json index a0caa3ee9c..b779b0798f 100644 --- a/packages/repo-tools/package.json +++ b/packages/repo-tools/package.json @@ -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", diff --git a/packages/repo-tools/src/commands/api-reports/api-extractor.ts b/packages/repo-tools/src/commands/api-reports/api-extractor.ts index bae62ccc7e..9f4f9b238d 100644 --- a/packages/repo-tools/src/commands/api-reports/api-extractor.ts +++ b/packages/repo-tools/src/commands/api-reports/api-extractor.ts @@ -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 { - 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(); - 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(); 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`, ); 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 3ac740d596..c66d084d64 100644 --- a/packages/repo-tools/src/commands/api-reports/api-reports.ts +++ b/packages/repo-tools/src/commands/api-reports/api-reports.ts @@ -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) { diff --git a/packages/repo-tools/src/commands/index.ts b/packages/repo-tools/src/commands/index.ts index ebbcb5be8e..5821dfdd3d 100644 --- a/packages/repo-tools/src/commands/index.ts +++ b/packages/repo-tools/src/commands/index.ts @@ -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 ', 'packages folder containers', [ + 'packages', + 'plugins', + ]) + .option( + '--omitMessages ', + '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)), diff --git a/packages/repo-tools/src/lib/paths.ts b/packages/repo-tools/src/lib/paths.ts new file mode 100644 index 0000000000..2c658c27b3 --- /dev/null +++ b/packages/repo-tools/src/lib/paths.ts @@ -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); diff --git a/yarn.lock b/yarn.lock index 441430dd19..e39514bb98 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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