From aa5a1fc545c4e8c2f12be84522c8907f58213733 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Fri, 14 Jun 2024 12:24:46 -0700 Subject: [PATCH] build: remove generated ts glue --- package.json | 2 +- tools/gen-ts-glue.ts | 69 -------------------------------------------- tsconfig.base.json | 2 +- 3 files changed, 2 insertions(+), 71 deletions(-) delete mode 100644 tools/gen-ts-glue.ts diff --git a/package.json b/package.json index 2c8a28ef2..71e851d5c 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "test:fast": "npm run test -- --suite=fast", "test:slow": "npm run test -- --suite=slow", "test:clear": "ts-node tools/test-clear", - "postinstall": "rimraf node_modules/.bin/*.ps1 && ts-node ./tools/gen-tsconfigs.ts && ts-node ./tools/gen-ts-glue.ts", + "postinstall": "rimraf node_modules/.bin/*.ps1 && ts-node ./tools/gen-tsconfigs.ts", "prepare": "husky install", "preversion": "yarn build" }, diff --git a/tools/gen-ts-glue.ts b/tools/gen-ts-glue.ts deleted file mode 100644 index d7f4cee1e..000000000 --- a/tools/gen-ts-glue.ts +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Mixing glue with lint is probably something you never want to do in real - * life, but in this project it helps the linter do its job. And sometimes the - * test suite too. - * - * This script creates `index.ts` files in each package directory that simply - * re-export the exports of the real main file. This is necessary for when all - * the packages are run under a common context (instead of each individually). - * This is because the symlinks yarn makes for each package are descendants of - * the context node is running in and therefore aren't treated as modules, only - * as directories. Therefore package.json isn't considered and we need an index - * file (like index.ts) to redirect to the right file. - */ - -import { promises as fs } from 'fs'; -import path from 'path'; - -import { getPackageInfo } from './utils'; - -// NOTE: this interface only defines the fields in the package.json that are -// used in this script -interface PartialPackageManifest { - main?: string; -} - -/** - * A heuristic to convert the `main` value from the package.json from its output - * location to the source location (e.g. dist/ to src/ & .js to .ts). - */ -function convertMainToSrc(main: string): string { - return main.replace(/^dist\//, 'src/').replace(/\.js$/, '.ts'); -} - -(async () => { - const pkgs = await getPackageInfo(); - - // Run each package in parallel - await Promise.all( - pkgs.map(async (pkg) => { - // Extract the `main` field from the package.json - const { main } = pkg.manifest as PartialPackageManifest; - - // Skip packages that have no main (e.g. the cli as of writing) - if (main === undefined) { - return; - } - - // Read the main file - const srcMain = convertMainToSrc(main); - const srcMainFull = path.resolve(pkg.path, srcMain); - const srcMainContents = await fs.readFile(srcMainFull, { encoding: 'utf8' }); - - // Detect if the package has a default export - const hasDefault = /export\s+default/i.test(srcMainContents); - - // Write the facade entry-point file - const importTarget = './' + srcMain.replace(/\.ts$/, ''); - const facadeFilePath = path.resolve(pkg.path, 'index.ts'); - let facadeFileContents = - '// ⚠️ AUTOGENERATED ⚠️ AUTOGENERATED ⚠️ AUTOGENERATED ⚠️\n' + - '// This file was automatically generated by `tools/gen-ts-glue.ts`. Do not modify directly if you want to keep your changes.\n' + - `export * from "${importTarget}";\n`; - if (hasDefault) { - facadeFileContents += `import defaultExport from "${importTarget}";\n` + `export default defaultExport;\n`; - } - await fs.writeFile(facadeFilePath, facadeFileContents); - }) - ); -})().catch(console.error); diff --git a/tsconfig.base.json b/tsconfig.base.json index 82b15e594..b732cd1ea 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -13,5 +13,5 @@ "composite": true, "declarationMap": true }, - "exclude": ["node_modules", "dist", "test", "index.ts", "tmpl"] + "exclude": ["node_modules", "dist", "test", "tmpl"] }