Skip to content

Commit fef72e3

Browse files
authored
ci: Add type definition check (#2462)
1 parent fbd5610 commit fef72e3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ jobs:
2727
with:
2828
cache: npm
2929
- run: npm ci
30-
- name: Build Types
31-
run: npm run build:types
30+
- name: Type Definition Check
31+
run: npm run ci:typecheck
3232
- name: Test Types
3333
run: npm run test:types 2>&1 | tee silent.txt;
3434
check-docs:

ci/typecheck.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require('fs').promises;
2+
const { exec } = require('child_process');
3+
const util = require('util');
4+
5+
async function getTypes(files) {
6+
const types = {};
7+
const promise = files.map((file) => {
8+
if (file.includes('.d.ts')) {
9+
return fs.readFile(`./types/${file}`, 'utf8').then((content) => {
10+
types[file] = content;
11+
});
12+
}
13+
});
14+
await Promise.all(promise);
15+
return types;
16+
}
17+
18+
(async () => {
19+
const execute = util.promisify(exec);
20+
const currentFiles = await fs.readdir('./types');
21+
const currentTypes = await getTypes(currentFiles);
22+
await execute('npm run build:types');
23+
const newFiles = await fs.readdir('./types');
24+
const newTypes = await getTypes(newFiles);
25+
for (const file of newFiles) {
26+
if (currentTypes[file] !== newTypes[file]) {
27+
console.error(
28+
'\x1b[31m%s\x1b[0m',
29+
'Type definitions files cannot be updated manually. Use `npm run build:types` to generate type definitions.'
30+
);
31+
process.exit(1);
32+
}
33+
}
34+
process.exit(0);
35+
})();

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
},
9898
"scripts": {
9999
"build": "node build_releases.js",
100-
"build:types": "tsc && prettier --write 'types/{**/*,*}.ts' && npm run lint:fix",
100+
"build:types": "tsc && prettier --write 'types/{**/*,*}.ts'",
101+
"ci:typecheck": "node ./ci/typecheck.js",
101102
"release": "node build_releases.js && npm publish",
102103
"test": "cross-env PARSE_BUILD=node jest",
103104
"test:mongodb": "npm run test:mongodb:runnerstart && npm run integration",

0 commit comments

Comments
 (0)