repo-tools: refactor root api report generation

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-29 12:22:27 +01:00
parent c8ddc04f5b
commit 547e41da5a
14 changed files with 131 additions and 61 deletions
@@ -16,7 +16,7 @@
import fs from 'fs-extra';
import { spawnSync } from 'child_process';
import { paths as cliPaths } from '../../lib/paths';
import { paths as cliPaths } from '../../../lib/paths';
/**
* Generates the TypeScript declaration files for the specified project, using the provided `tsconfig.json` file.
@@ -17,3 +17,4 @@
export { runApiExtraction } from './runApiExtraction';
export { buildDocs } from './buildDocs';
export { createTemporaryTsConfig } from './createTemporaryTsConfig';
export { generateTypeDeclarations } from './generateTypeDeclarations';
@@ -17,7 +17,7 @@
import { ExtractorMessage } from '@microsoft/api-extractor';
import { AstDeclaration } from '@microsoft/api-extractor/lib/analyzer/AstDeclaration';
import { Program } from 'typescript';
import { paths as cliPaths } from '../../../lib/paths';
import { tryRunPrettier } from '../common';
let applied = false;
@@ -158,16 +158,6 @@ export function patchApiReportGeneration() {
...moreArgs,
);
try {
const prettier = require('prettier') as typeof import('prettier');
const config = prettier.resolveConfig.sync(cliPaths.targetRoot) ?? {};
return prettier.format(content, {
...config,
parser: 'markdown',
});
} catch (e) {
return content;
}
return tryRunPrettier(content);
};
}
@@ -28,7 +28,7 @@ import { minimatch } from 'minimatch';
import { join, relative as relativePath, resolve as resolvePath } from 'path';
import { getPackageExportDetails } from '../../../lib/getPackageExportDetails';
import { paths as cliPaths } from '../../../lib/paths';
import { logApiReportInstructions } from '../api-extractor';
import { logApiReportInstructions } from '../common';
import { patchApiReportGeneration } from './patchApiReportGeneration';
const tmpDir = cliPaths.resolveTargetRoot(
@@ -18,18 +18,29 @@ import { createMockDirectory } from '@backstage/backend-test-utils';
import { normalize } from 'path';
import * as pathsLib from '../../lib/paths';
import { categorizePackageDirs } from './api-extractor';
import { categorizePackageDirs } from './categorizePackageDirs';
import { buildApiReports } from './api-reports';
import { generateTypeDeclarations } from './generateTypeDeclarations';
import { buildApiReports } from './buildApiReports';
import { generateTypeDeclarations } from './api-reports';
import { PackageGraph } from '@backstage/cli-node';
import { runCliExtraction } from './cli-reports';
import { runApiExtraction, buildDocs } from './api-reports/index';
jest.mock('./generateTypeDeclarations');
// create mocks for the dependencies of the `buildApiReports` function
jest.mock('./api-extractor', () => ({
jest.mock('./api-reports', () => ({
generateTypeDeclarations: jest.fn(),
createTemporaryTsConfig: jest.fn(),
runApiExtraction: jest.fn(),
runCliExtraction: jest.fn(),
buildDocs: jest.fn(),
}));
jest.mock('./cli-reports', () => ({
runCliExtraction: jest.fn(),
}));
jest.mock('./sql-reports', () => ({
runSqlExtraction: jest.fn(),
}));
jest.mock('./categorizePackageDirs', () => ({
categorizePackageDirs: jest.fn().mockImplementation(async (p: string[]) => {
console.log('categorizePackageDirs', p);
return {
@@ -38,9 +49,6 @@ jest.mock('./api-extractor', () => ({
sqlPackageDirs: [],
};
}),
runApiExtraction: jest.fn(),
runCliExtraction: jest.fn(),
buildDocs: jest.fn(),
}));
const projectPaths = pathsLib.paths;
@@ -15,15 +15,15 @@
*/
import { OptionValues } from 'commander';
import { categorizePackageDirs } from './api-extractor';
import { categorizePackageDirs } from './categorizePackageDirs';
import { paths as cliPaths, resolvePackagePaths } from '../../lib/paths';
import { generateTypeDeclarations } from './generateTypeDeclarations';
import { runSqlExtraction } from './sql-reports';
import { runCliExtraction } from './cli-reports';
import {
runApiExtraction,
buildDocs,
createTemporaryTsConfig,
generateTypeDeclarations,
} from './api-reports/index';
type Options = {
@@ -36,7 +36,7 @@ type Options = {
validateReleaseTags?: boolean;
} & OptionValues;
export const buildApiReports = async (paths: string[] = [], opts: Options) => {
export async function buildApiReports(paths: string[] = [], opts: Options) {
const tmpDir = cliPaths.resolveTargetRoot(
'./node_modules/.cache/api-extractor',
);
@@ -119,7 +119,7 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => {
outputDir: cliPaths.resolveTargetRoot('docs/reference'),
});
}
};
}
/**
* Splits the input string on comma, and returns an array of the resulting substrings.
@@ -17,23 +17,6 @@
import fs from 'fs-extra';
import { paths as cliPaths } from '../../lib/paths';
export function logApiReportInstructions() {
console.log('');
console.log(
'*************************************************************************************',
);
console.log(
'* You have uncommitted changes to the public API or reports of a package. *',
);
console.log(
'* To solve this, run `yarn build:api-reports` and commit all md file changes. *',
);
console.log(
'*************************************************************************************',
);
console.log('');
}
export async function categorizePackageDirs(packageDirs: string[]) {
const dirs = packageDirs.slice();
const tsPackageDirs = new Array<string>();
@@ -24,7 +24,7 @@ import { createBinRunner } from '../../util';
import { CliHelpPage, CliModel } from './types';
import { paths as cliPaths } from '../../../lib/paths';
import { generateCliReport } from './generateCliReport';
import { logApiReportInstructions } from '../api-extractor';
import { logApiReportInstructions } from '../common';
function parseHelpPage(helpPageContent: string) {
const [, usage] = helpPageContent.match(/^\s*Usage: (.*)$/im) ?? [];
@@ -0,0 +1,18 @@
/*
* Copyright 2024 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.
*/
export { tryRunPrettier } from './tryRunPrettier';
export { logApiReportInstructions } from './logApiReportInstructions';
@@ -0,0 +1,32 @@
/*
* Copyright 2024 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.
*/
export function logApiReportInstructions() {
console.log('');
console.log(
'*************************************************************************************',
);
console.log(
'* You have uncommitted changes to the public API or reports of a package. *',
);
console.log(
'* To solve this, run `yarn build:api-reports` and commit all md file changes. *',
);
console.log(
'*************************************************************************************',
);
console.log('');
}
@@ -0,0 +1,35 @@
/*
* Copyright 2024 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 { paths as cliPaths } from '../../../lib/paths';
import type { Config } from 'prettier';
export function tryRunPrettier(
content: string,
extraConfig: Config = { parser: 'markdown' },
): string {
try {
const prettier = require('prettier') as typeof import('prettier');
const config = prettier.resolveConfig.sync(cliPaths.targetRoot) ?? {};
return prettier.format(content, {
...config,
...extraConfig,
});
} catch (e) {
return content;
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2024 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.
*/
export { buildApiReports } from './buildApiReports';
@@ -22,7 +22,7 @@ import { SchemaInfo } from './types';
import { getPgSchemaInfo } from './getPgSchemaInfo';
import { generateSqlReport } from './generateSqlReport';
import type { Knex } from 'knex';
import { logApiReportInstructions } from '../api-extractor';
import { logApiReportInstructions, tryRunPrettier } from '../common';
interface SqlExtractionOptions {
packageDirs: string[];
@@ -153,7 +153,7 @@ async function runSingleSqlExtraction(
}
}
const report = prettyReport(
const report = tryRunPrettier(
generateSqlReport({
reportName,
failedDownMigration,
@@ -196,17 +196,3 @@ async function runSingleSqlExtraction(
}
}
}
function prettyReport(content: string): string {
try {
const prettier = require('prettier') as typeof import('prettier');
const config = prettier.resolveConfig.sync(cliPaths.targetRoot) ?? {};
return prettier.format(content, {
...config,
parser: 'markdown',
});
} catch (e) {
return content;
}
}
+1 -1
View File
@@ -194,7 +194,7 @@ export function registerCommands(program: Command) {
'Turn on release tag validation for the public, beta, and alpha APIs',
)
.description('Generate an API report for selected packages')
.action(lazy(() => import('./api-reports/api-reports'), 'buildApiReports'));
.action(lazy(() => import('./api-reports'), 'buildApiReports'));
program
.command('type-deps')