diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81c922c3b..4034be1c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/ci/typecheck.js b/ci/typecheck.js new file mode 100644 index 000000000..50fe8877d --- /dev/null +++ b/ci/typecheck.js @@ -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); +})(); diff --git a/package.json b/package.json index 63342724b..2e5362935 100644 --- a/package.json +++ b/package.json @@ -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",