Skip to content

Commit 7c03320

Browse files
shibeshduwcloud-sdk-jsZhongpinWang
authored
chore: Refactor GH action for Public Api Check (#5203)
* use regex pattern * Changes from lint:fix * refactor: remove sep comma for regex * Changes from lint:fix * refactor: change input name * chore: compile new check public api script * fix: update action inputs --------- Co-authored-by: cloud-sdk-js <[email protected]> Co-authored-by: Zhongpin Wang <[email protected]>
1 parent e0fa9c8 commit 7c03320

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

.github/actions/check-public-api/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ inputs:
66
default: 'false'
77
excluded_packages:
88
description: 'Packages to exclude when checking the public API.'
9-
ignored_paths:
10-
description: 'Paths to ignore when checking the public API.'
9+
ignored_path_pattern:
10+
description: 'Regular expression for paths to be ignore when checking the public API.'
1111

1212
runs:
1313
using: 'node20'

.github/actions/check-public-api/index.js

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build-packages/check-public-api/index.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
/* eslint-disable jsdoc/require-jsdoc */
22

3-
import path, {
4-
join,
5-
resolve,
6-
parse,
7-
basename,
8-
dirname,
9-
posix,
10-
sep
11-
} from 'path';
3+
import { join, resolve, parse, basename, dirname, posix, sep } from 'path';
124
import { promises, existsSync } from 'fs';
135
import { glob } from 'glob';
146
import { info, warning, error, getInput, setFailed } from '@actions/core';
@@ -20,15 +12,11 @@ import {
2012
readIncludeExcludeWithDefaults,
2113
transpileDirectory
2214
} from '@sap-cloud-sdk/generator-common/internal';
23-
import type { CompilerOptions } from 'typescript';
2415
import { getPackages } from '@manypkg/get-packages';
16+
import type { CompilerOptions } from 'typescript';
2517

2618
const { readFile, lstat, readdir } = promises;
2719

28-
const localConfigPath = join(
29-
process.cwd(),
30-
'build-packages/check-public-api/local-config.json'
31-
);
3220
const pathToTsConfigRoot = join(process.cwd(), 'tsconfig.json');
3321
const pathRootNodeModules = join(process.cwd(), 'node_modules');
3422
export const regexExportedIndex = /export(?:type)?\{([\w,]+)\}from'\./g;
@@ -48,14 +36,24 @@ function paths(pathToPackage: string): {
4836
pathCompiled: string;
4937
} {
5038
return {
51-
pathToSource: join(pathToPackage, 'src'),
52-
pathToPackageJson: join(pathToPackage, 'package.json'),
53-
pathToTsConfig: join(pathToPackage, 'tsconfig.json'),
54-
pathToNodeModules: join(pathToPackage, 'node_modules'),
39+
pathToSource: getPathWithPosixSeparator(join(pathToPackage, 'src')),
40+
pathToPackageJson: getPathWithPosixSeparator(
41+
join(pathToPackage, 'package.json')
42+
),
43+
pathToTsConfig: getPathWithPosixSeparator(
44+
join(pathToPackage, 'tsconfig.json')
45+
),
46+
pathToNodeModules: getPathWithPosixSeparator(
47+
join(pathToPackage, 'node_modules')
48+
),
5549
pathCompiled: 'dist'
5650
};
5751
}
5852

53+
function getPathWithPosixSeparator(filePath: string): string {
54+
return filePath.split(sep).join(posix.sep);
55+
}
56+
5957
function mockFileSystem(pathToPackage: string) {
6058
const { pathToSource, pathToTsConfig, pathToNodeModules, pathToPackageJson } =
6159
paths(pathToPackage);
@@ -105,15 +103,13 @@ function compareApisAndLog(
105103
allExportedTypes: ExportedObject[]
106104
): boolean {
107105
let setsAreEqual = true;
108-
const ignoredPaths = getListFromInput('ignored_paths');
106+
const ignoredPathPattern = getInput('ignored_path_pattern');
109107

110108
allExportedTypes.forEach(exportedType => {
111-
const normalizedPath = exportedType.path.split(sep).join(posix.sep);
109+
const normalizedPath = getPathWithPosixSeparator(exportedType.path);
112110

113-
const isPathMatched = ignoredPaths.length
114-
? ignoredPaths.some(ignoredPath =>
115-
normalizedPath.includes(ignoredPath.split(sep).join(posix.sep))
116-
)
111+
const isPathMatched = ignoredPathPattern
112+
? new RegExp(ignoredPathPattern).test(normalizedPath)
117113
: false;
118114
if (
119115
!allExportedIndex.find(nameInIndex => exportedType.name === nameInIndex)
@@ -169,7 +165,10 @@ export async function checkApiOfPackage(pathToPackage: string): Promise<void> {
169165
usePrettier: false
170166
}
171167
},
172-
{ exclude: includeExclude?.exclude!, include: ['**/*.ts'] }
168+
{
169+
exclude: includeExclude ? includeExclude.exclude : [],
170+
include: ['**/*.ts']
171+
}
173172
);
174173

175174
const forceInternalExports = getInput('force_internal_exports') === 'true';
@@ -309,16 +308,16 @@ export async function parseIndexFile(
309308
...parseExportedObjectsInFile(fileContent).map(obj => obj.name)
310309
];
311310
const starFiles = captureGroupsFromGlobalRegex(
312-
/export \* from '([\w\/.-]+)'/g,
311+
/export \* from '([\w/.-]+)'/g,
313312
fileContent
314313
);
315314
const starFileExports = await Promise.all(
316315
starFiles.map(async relativeFilePath => {
317-
const filePath = relativeFilePath.endsWith('.js')
316+
const absolutePath = relativeFilePath.endsWith('.js')
318317
? resolve(cwd, `${relativeFilePath.slice(0, -3)}.ts`)
319318
: resolve(cwd, `${relativeFilePath}.ts`);
320319

321-
return parseIndexFile(filePath, forceInternalExports);
320+
return parseIndexFile(absolutePath, forceInternalExports);
322321
})
323322
);
324323
return [...localExports, ...starFileExports.flat()];
@@ -410,8 +409,8 @@ async function runCheckApi() {
410409
for (const pkg of packagesToCheck) {
411410
try {
412411
await checkApiOfPackage(pkg.dir);
413-
} catch (error) {
414-
setFailed(`API check failed for ${pkg.relativeDir}: ${error}`);
412+
} catch (e) {
413+
setFailed(`API check failed for ${pkg.relativeDir}: ${e}`);
415414
process.exit(1);
416415
}
417416
}

0 commit comments

Comments
 (0)