|
| 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() |
0 commit comments