Skip to content

Commit add711f

Browse files
committed
Add validate-dts CI step.
1 parent 631a8a1 commit add711f

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

.buildkite/pipeline.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ steps:
1313
- echo "+++ Run tests"
1414
- yarn constraints
1515
- yarn run test:scripts
16+
- yarn run test:check-dts
17+
plugins:
18+
- ssh://[email protected]/segmentio/cache-buildkite-plugin#v2.0.0:
19+
key: "v1.1-cache-dev-{{ checksum 'yarn.lock' }}"
20+
paths: ['.yarn/cache/']
1621

1722
- label: '[Browser] Lint + Test'
1823
key: browser-lint-test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ reports/*
3333
/test-results/
3434
playwright-report/
3535
playwright/.cache/
36+
tmp.tsconfig.json

meta-tests/check-dts.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { exec } from 'child_process'
2+
import { promisify } from 'util'
3+
import path from 'path'
4+
import fs from 'fs'
5+
6+
/**
7+
* This script is for extra typechecking of the built .d.ts files in {package_name}/dist/types/*.
8+
* Occassionally, "internal" .dts errors can result from oddities in typescript configuration,
9+
* such as: https://github.com/segmentio/analytics-next/issues/748.
10+
* These errors would only surface for customers with `skipLibCheck` enabled.
11+
*/
12+
const execa = promisify(exec)
13+
14+
const allPublicPackageDirNames = ['browser', 'core', 'node'] as const
15+
16+
type PackageDirName = typeof allPublicPackageDirNames[number]
17+
18+
class Tsc {
19+
// e.g. packages/browser
20+
configPathDir: string
21+
// e.g. packages/browser/tsconfig.json
22+
configPath: string
23+
24+
private jsonConfig: string = JSON.stringify({
25+
extends: '../../tsconfig.json',
26+
include: ['./dist/types/**/*'],
27+
compilerOptions: {
28+
noEmit: true,
29+
skipLibCheck: false,
30+
},
31+
})
32+
33+
constructor(packageDirName: PackageDirName) {
34+
this.configPathDir = path.join('packages', packageDirName)
35+
this.configPath = path.join(this.configPathDir, 'tmp.tsconfig.json')
36+
}
37+
38+
typecheck() {
39+
const cmd = [
40+
`node_modules/.bin/tsc`,
41+
`--project ${this.configPath}`,
42+
`--pretty false`,
43+
].join(' ')
44+
return execa(cmd)
45+
}
46+
47+
deleteConfig() {
48+
fs.unlinkSync(this.configPath)
49+
}
50+
51+
writeConfig() {
52+
fs.writeFileSync(this.configPath, this.jsonConfig, {
53+
encoding: 'utf8',
54+
})
55+
}
56+
}
57+
58+
const checkDts = async (packageDirName: PackageDirName): Promise<void> => {
59+
const tsc = new Tsc(packageDirName)
60+
tsc.writeConfig()
61+
try {
62+
await tsc.typecheck()
63+
} catch (err: any) {
64+
if (!err || typeof err !== 'object' || !err.stdout) {
65+
throw err
66+
}
67+
const errors: string[] = err.stdout.toString().split('\n')
68+
const relevantErrors = errors.filter((msg) =>
69+
msg.includes(tsc.configPathDir)
70+
)
71+
if (relevantErrors.length) {
72+
throw relevantErrors
73+
}
74+
} finally {
75+
tsc.deleteConfig()
76+
}
77+
}
78+
79+
const main = async () => {
80+
let hasError = false
81+
for (const packageDirName of allPublicPackageDirNames) {
82+
try {
83+
console.log(`Checking "${packageDirName}/dist/types"...`)
84+
await checkDts(packageDirName)
85+
} catch (err) {
86+
console.error(err)
87+
hasError = true
88+
}
89+
}
90+
if (hasError) {
91+
console.log('\n Tests failed.')
92+
process.exit(1)
93+
} else {
94+
console.log('\n Tests passed.')
95+
process.exit(0)
96+
}
97+
}
98+
99+
void main()

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"scripts": {
1313
"test": "jest",
1414
"test:scripts": "jest --config scripts/jest.config.js",
15+
"test:check-dts": "yarn build && yarn ts-node meta-tests/check-dts.ts",
1516
"test:node-int": "turbo run --filter=node-integration-tests test",
1617
"lint": "yarn constraints && turbo run lint --continue",
1718
"build": "turbo run build --filter='./packages/*'",

0 commit comments

Comments
 (0)