Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions bazel/api-golden/index_npm_packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ async function main(
stripExportPattern: RegExp,
typeNames: string[],
) {
/** Whether the goldenDir provided is actually pointing to a single file. */
const singleFileMode = fs.existsSync(goldenDir) && fs.statSync(goldenDir).isFile();
// TODO(ESM) This can be replaced with an actual ESM import when `ts_library` is
// guaranteed to be ESM-only and supports the `mts` extension.
const chalk = {red: (v: string) => v, yellow: (v: string) => v};

const packageJsonPath = path.join(npmPackageDir, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as PackageJson;
const entryPoints = findEntryPointsWithinNpmPackage(npmPackageDir, packageJson);
if (entryPoints.length === 0) {
console.error(
'No entry points were found in the provided package for determining the API surface.',
);
process.exitCode = 3;
return;
}
const worker = new Piscina<Parameters<typeof testApiGolden>, string>({
filename: path.resolve(__dirname, './test_api_report.js'),
});
Expand All @@ -50,7 +59,11 @@ async function main(
// entry-point we maintain a separate golden file. These golden files are
// based on the name of the defining NodeJS exports subpath in the NPM package,
// See: https://api-extractor.com/pages/overview/demo_api_report/.
const goldenName = path.join(subpath, 'index.api.md');
let goldenName = path.join(subpath, 'index.api.md');
// In single file mode, the subpath is the golden file.
if (singleFileMode) {
goldenName = subpath;
}
const goldenFilePath = path.join(goldenDir, goldenName);
const moduleName = normalizePathToPosix(path.join(packageJson.name, subpath));

Expand Down Expand Up @@ -105,14 +118,28 @@ async function main(
const allTestsSucceeding = results.every((r) => r === true);

if (outdatedGoldens.length) {
console.error(chalk.red(`The following goldens are outdated:`));
outdatedGoldens.forEach((name) => console.info(`- ${name}`));
console.info();
console.info(
chalk.yellow(
`The goldens can be updated by running: yarn bazel run ${process.env.TEST_TARGET}.accept`,
),
);
console.error();
console.error(Array(80).fill('=').join(''));
console.error(`${Array(35).fill('=').join('')} RESULTS ${Array(36).fill('=').join('')}`);
console.error(Array(80).fill('=').join(''));
if (singleFileMode) {
console.error(
chalk.red(
`The golden is out of date and can be updated by running:\n - yarn bazel run ${process.env.TEST_TARGET}.accept`,
),
);
} else {
console.error(chalk.red(`The following goldens are outdated:`));
outdatedGoldens.forEach((name) => console.info(`- ${name}`));
console.info();
console.info(
chalk.yellow(
`The goldens can be updated by running:\n - yarn bazel run ${process.env.TEST_TARGET}.accept`,
),
);
}
console.error(Array(80).fill('=').join(''));
console.error();
}

// Bazel expects `3` as exit code for failing tests.
Expand Down
Loading