Skip to content

Commit

Permalink
Create preversion script to check exports
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kevinoid committed Nov 16, 2024
1 parent 00844fe commit 33c81d0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"overrides": [
{
"files": [
"bin/*.js"
"bin/*.js",
"scripts/*.js"
],
"rules": {
// Executable scripts are not expected to have exports
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"lint-js": "eslint --report-unused-disable-directives . && echo ESLint passed.",
"postpublish": "git -C doc push && git push --follow-tags origin main gh-pages && echo Remember to update GitHub Releases from CHANGELOG.md",
"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\"",
"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",
"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",
"test": "npm run lint && npm run test-unit",
"test-cov": "npm run lint && npm run test-unit-cov",
"test-unit": "node --throw-deprecation --unhandled-rejections=strict node_modules/mocha/bin/mocha.js --parallel --recursive test",
Expand Down
56 changes: 56 additions & 0 deletions scripts/preversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node
/**
* @copyright Copyright 2024 Kevin Locke <[email protected]>
* @license MIT
*/

/* eslint-disable no-console */

import { readdir, readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { debuglog } from 'node:util';

const debug = debuglog('openapi-transformers-preversion');

const packagePath = path.dirname(path.dirname(fileURLToPath(import.meta.url)));

const indexJsPath = path.join(packagePath, 'index.js');
const indexJs = await readFile(indexJsPath, { encoding: 'utf8' });

const packageJsonPath = path.join(packagePath, 'package.json');
const packageJsonStr = await readFile(packageJsonPath, { encoding: 'utf8' });
const packageJson = JSON.parse(packageJsonStr);

const readmePath = path.join(packagePath, 'README.md');
const readme = await readFile(readmePath, { encoding: 'utf8' });

let errors = 0;
const packageFiles = await readdir(packagePath);
for (const packageFile of packageFiles) {
if (packageFile.slice(-3) === '.js'
&& packageFile !== 'index.js') {
debug('Checking %s...', packageFile);

const dotPackageFile = `./${packageFile}`;

if (!indexJs.includes(dotPackageFile)) {
console.error('Error: %s not exported from index.js', dotPackageFile);
errors += 1;
}

if (packageJson.exports[dotPackageFile] !== dotPackageFile) {
console.error('Error: %s not in package.json#exports', dotPackageFile);
errors += 1;
}

if (!readme.includes(dotPackageFile)) {
console.error('Error: %s not in README.md', dotPackageFile);
errors += 1;
}
}
}

debug('preversion checks completed with %d errors', errors);

process.exitCode = errors > 0 ? 1 : 0;

0 comments on commit 33c81d0

Please sign in to comment.