Skip to content

ci: Add type definition check #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
with:
cache: npm
- run: npm ci
- name: Build Types
run: npm run build:types
- name: Type Definition Check
run: npm run ci:typecheck
- name: Test Types
run: npm run test:types 2>&1 | tee silent.txt;
check-docs:
Expand Down
35 changes: 35 additions & 0 deletions ci/typecheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs').promises;
const { exec } = require('child_process');
const util = require('util');

async function getTypes(files) {
const types = {};
const promise = files.map((file) => {
if (file.includes('.d.ts')) {
return fs.readFile(`./types/${file}`, 'utf8').then((content) => {
types[file] = content;
});
}
});
await Promise.all(promise);
return types;
}

(async () => {
const execute = util.promisify(exec);
const currentFiles = await fs.readdir('./types');
const currentTypes = await getTypes(currentFiles);
await execute('npm run build:types');
const newFiles = await fs.readdir('./types');
const newTypes = await getTypes(newFiles);
for (const file of newFiles) {
if (currentTypes[file] !== newTypes[file]) {
console.error(
'\x1b[31m%s\x1b[0m',
'Type definitions files cannot be updated manually. Use `npm run build:types` to generate type definitions.'
);
process.exit(1);
}
}
process.exit(0);
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
},
"scripts": {
"build": "node build_releases.js",
"build:types": "tsc && prettier --write 'types/{**/*,*}.ts' && npm run lint:fix",
"build:types": "tsc && prettier --write 'types/{**/*,*}.ts'",
"ci:typecheck": "node ./ci/typecheck.js",
"release": "node build_releases.js && npm publish",
"test": "cross-env PARSE_BUILD=node jest",
"test:mongodb": "npm run test:mongodb:runnerstart && npm run integration",
Expand Down
Loading