Skip to content

Commit 33c81d0

Browse files
committed
Create preversion script to check exports
It would be easy to forget to add or remove a class from package.json, index.js, or README.md. Check that all classes are present before changing the package version. Signed-off-by: Kevin Locke <[email protected]>
1 parent 00844fe commit 33c81d0

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"overrides": [
2121
{
2222
"files": [
23-
"bin/*.js"
23+
"bin/*.js",
24+
"scripts/*.js"
2425
],
2526
"rules": {
2627
// Executable scripts are not expected to have exports

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"lint-js": "eslint --report-unused-disable-directives . && echo ESLint passed.",
7777
"postpublish": "git -C doc push && git push --follow-tags origin main gh-pages && echo Remember to update GitHub Releases from CHANGELOG.md",
7878
"postversion": "rimraf doc && git clone -b gh-pages -l -q . doc && npm run doc && git -C doc add . && git -C doc commit -n -m \"Docs for v$npm_package_version\"",
79-
"preversion": "npm run test-cov && c8 check-coverage --statements 95 && depcheck --ignore-dirs doc --ignores=\"eslint-*,rimraf\" && david && git-branch-is main && hub-ci-status -vv --wait",
79+
"preversion": "node ./scripts/preversion.js && npm run test-cov && c8 check-coverage --statements 95 && depcheck --ignore-dirs doc --ignores=\"eslint-*,rimraf\" && david && git-branch-is main && hub-ci-status -vv --wait",
8080
"test": "npm run lint && npm run test-unit",
8181
"test-cov": "npm run lint && npm run test-unit-cov",
8282
"test-unit": "node --throw-deprecation --unhandled-rejections=strict node_modules/mocha/bin/mocha.js --parallel --recursive test",

scripts/preversion.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
/**
3+
* @copyright Copyright 2024 Kevin Locke <[email protected]>
4+
* @license MIT
5+
*/
6+
7+
/* eslint-disable no-console */
8+
9+
import { readdir, readFile } from 'node:fs/promises';
10+
import path from 'node:path';
11+
import { fileURLToPath } from 'node:url';
12+
import { debuglog } from 'node:util';
13+
14+
const debug = debuglog('openapi-transformers-preversion');
15+
16+
const packagePath = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
17+
18+
const indexJsPath = path.join(packagePath, 'index.js');
19+
const indexJs = await readFile(indexJsPath, { encoding: 'utf8' });
20+
21+
const packageJsonPath = path.join(packagePath, 'package.json');
22+
const packageJsonStr = await readFile(packageJsonPath, { encoding: 'utf8' });
23+
const packageJson = JSON.parse(packageJsonStr);
24+
25+
const readmePath = path.join(packagePath, 'README.md');
26+
const readme = await readFile(readmePath, { encoding: 'utf8' });
27+
28+
let errors = 0;
29+
const packageFiles = await readdir(packagePath);
30+
for (const packageFile of packageFiles) {
31+
if (packageFile.slice(-3) === '.js'
32+
&& packageFile !== 'index.js') {
33+
debug('Checking %s...', packageFile);
34+
35+
const dotPackageFile = `./${packageFile}`;
36+
37+
if (!indexJs.includes(dotPackageFile)) {
38+
console.error('Error: %s not exported from index.js', dotPackageFile);
39+
errors += 1;
40+
}
41+
42+
if (packageJson.exports[dotPackageFile] !== dotPackageFile) {
43+
console.error('Error: %s not in package.json#exports', dotPackageFile);
44+
errors += 1;
45+
}
46+
47+
if (!readme.includes(dotPackageFile)) {
48+
console.error('Error: %s not in README.md', dotPackageFile);
49+
errors += 1;
50+
}
51+
}
52+
}
53+
54+
debug('preversion checks completed with %d errors', errors);
55+
56+
process.exitCode = errors > 0 ? 1 : 0;

0 commit comments

Comments
 (0)