diff --git a/build-settings/check-type-definitions.ts b/build-settings/check-type-definitions.ts new file mode 100755 index 00000000..460e4df6 --- /dev/null +++ b/build-settings/check-type-definitions.ts @@ -0,0 +1,59 @@ +#!/usr/bin/env -S node -r @swc-node/register +/* eslint-disable no-console */ +/** + * Check type definition files for anything that doesn't look right. + * + * If our packages don't export all the types that other packages reference, + * even indirectly, then their type definitions will import them from the source + * files instead. Since we don't want to ship source code in every package, + * we want to guard against this. + * + * This script should be run after `yarn build:types`. It will scan the type + * definitions of each package for any types that are being incorrectly + * imported from other the source code of other packages, and flag them, + * exiting with a non-zero status code if any are found. + */ +import * as fs from "fs"; +import * as path from "path"; +import * as fglob from "fast-glob"; + +const rootDir = path.join(__dirname, ".."); +const packagesDir = path.join(rootDir, "packages"); + +// Find all the type definition files in the packages dist directories. +const typeDefinitionFiles = fglob.sync("**/*.d.ts", { + cwd: packagesDir, + onlyFiles: true, +}); + +let foundErrors = false; +// Scan each one for any imports of types from source. +for (const typeDefinitionFile of typeDefinitionFiles) { + const regexpImportSrc = + /import\(".+\/(wonder-stuff-.+)\/src\/.+"\)\.([a-zA-Z]+)/g; + const content = fs.readFileSync( + path.join(packagesDir, typeDefinitionFile), + "utf-8", + ); + const lines = content.split("\n"); + let match; + for (let line = 0; line < lines.length; line++) { + while ((match = regexpImportSrc.exec(lines[line]))) { + foundErrors = true; + const position = match.index; + const lineNo = line + 1; + const refPath = path.join("packages", typeDefinitionFile); + console.error(`${refPath}:${lineNo}:${position}`); + console.error( + ` Incorrectly imported type ${match[2]} from ${match[1]} source`, + ); + console.error( + ` Update the package ${match[1]} to export the type ${match[2]}\n`, + ); + } + } +} + +if (foundErrors) { + process.exit(1); +} diff --git a/package.json b/package.json index 6eeee9e3..2e3bb875 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "rollup": "rollup -c build-settings/rollup.config.mjs", "build": "yarn rollup", "build:prodsizecheck": "yarn rollup --configPlatforms='browser' --configFormats='esm' --configEnvironment='production'", - "build:types": "yarn tsc --build --verbose tsconfig-build.json && ./build-settings/remove-files-not-to-be-packaged.ts", + "build:types": "yarn tsc --build --verbose tsconfig-build.json && ./build-settings/remove-files-not-to-be-packaged.ts && ./build-settings/check-type-definitions.ts", "build:docs": "typedoc", "watch": "yarn rollup --watch", "clean": "rm -rf packages/wonder-stuff-*/dist && rm -rf packages/wonder-stuff-*/node_modules && rm -f packages/*/*.tsbuildinfo",