|
| 1 | +#!/usr/bin/env -S node -r @swc-node/register |
| 2 | +/* eslint-disable no-console */ |
| 3 | +/** |
| 4 | + * Check type definition files for anything that doesn't look right. |
| 5 | + * |
| 6 | + * If our packages don't export all the types that other packages reference, |
| 7 | + * even indirectly, then their type definitions will import them from the source |
| 8 | + * files instead. Since we don't want to ship source code in every package, |
| 9 | + * we want to guard against this. |
| 10 | + * |
| 11 | + * This script should be run after `yarn build:types`. It will scan the type |
| 12 | + * definitions of each package for any types that are being incorrectly |
| 13 | + * imported from other the source code of other packages, and flag them, |
| 14 | + * exiting with a non-zero status code if any are found. |
| 15 | + */ |
| 16 | +import * as fs from "fs"; |
| 17 | +import * as path from "path"; |
| 18 | +import * as fglob from "fast-glob"; |
| 19 | + |
| 20 | +const rootDir = path.join(__dirname, ".."); |
| 21 | +const packagesDir = path.join(rootDir, "packages"); |
| 22 | + |
| 23 | +// Find all the type definition files in the packages dist directories. |
| 24 | +const typeDefinitionFiles = fglob.sync("**/*.d.ts", { |
| 25 | + cwd: packagesDir, |
| 26 | + onlyFiles: true, |
| 27 | +}); |
| 28 | + |
| 29 | +let foundErrors = false; |
| 30 | +// Scan each one for any imports of types from source. |
| 31 | +for (const typeDefinitionFile of typeDefinitionFiles) { |
| 32 | + const regexpImportSrc = |
| 33 | + /import\(".+\/(wonder-stuff-.+)\/src\/.+"\)\.([a-zA-Z]+)/g; |
| 34 | + const content = fs.readFileSync( |
| 35 | + path.join(packagesDir, typeDefinitionFile), |
| 36 | + "utf-8", |
| 37 | + ); |
| 38 | + const lines = content.split("\n"); |
| 39 | + let match; |
| 40 | + for (let line = 0; line < lines.length; line++) { |
| 41 | + while ((match = regexpImportSrc.exec(lines[line]))) { |
| 42 | + foundErrors = true; |
| 43 | + const position = match.index; |
| 44 | + const lineNo = line + 1; |
| 45 | + const refPath = path.join("packages", typeDefinitionFile); |
| 46 | + console.error(`${refPath}:${lineNo}:${position}`); |
| 47 | + console.error( |
| 48 | + ` Incorrectly imported type ${match[2]} from ${match[1]} source`, |
| 49 | + ); |
| 50 | + console.error( |
| 51 | + ` Update the package ${match[1]} to export the type ${match[2]}\n`, |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +if (foundErrors) { |
| 58 | + process.exit(1); |
| 59 | +} |
0 commit comments