From 134d65a0cbcf692ac6bd7571466d4497a69a4acb Mon Sep 17 00:00:00 2001 From: Josef Leventon Date: Wed, 20 Mar 2024 16:19:28 -0400 Subject: [PATCH] Remove unused cjs script --- scripts/cjs.ts | 145 ------------------------------------------------- 1 file changed, 145 deletions(-) delete mode 100644 scripts/cjs.ts diff --git a/scripts/cjs.ts b/scripts/cjs.ts deleted file mode 100644 index 242814c2..00000000 --- a/scripts/cjs.ts +++ /dev/null @@ -1,145 +0,0 @@ -import type { Package } from '@manypkg/get-packages' -import { getPackagesSync } from '@manypkg/get-packages' -import { execa } from 'execa' -import { readJsonSync, writeJsonSync } from 'fs-extra' - -import path from 'path' - -type PackageJson = Package['packageJson'] -type PreparedPackage = Package & { - oldPackageJson: PackageJson -} - -const packagesDir = [ - path.join(__dirname, '..', 'packages'), - path.join(__dirname, '..', 'references', 'packages'), -] -const referencesDir = [path.join(__dirname, '..', 'references', 'packages')] - -/** Script to generate & release a CJS escape hatch bundle. */ -export async function main() { - const { packages: packages_ } = getPackagesSync(path.join(__dirname, '..')) - const packages = packages_.filter((x) => !x.dir.includes('plugins')) - - // 1. Prepare package package.jsons into CJS format. - const preparedPackages = prepare({ packages }) - - // 2. Bundle into CJS. - await build() - - // 3. Retrieve the changed packages from the output of - // the changesets GH Action (if used). Otherwise, just - // use prepared packages. - const changedPackageJsons: PackageJson[] = process.argv[2] - ? JSON.parse(process.argv[2]) - : preparedPackages.map(({ packageJson }) => packageJson) - const changedPackages: Package[] = preparedPackages.filter( - ({ packageJson }) => - changedPackageJsons.some(({ name }) => name === packageJson.name), - ) - - // 4. Version packages w/ "-cjs" suffix. - version({ changedPackages, packages }) - - // 5. Publish packages under "cjs" tag. - await publish({ changedPackages }) - - // 6. Revert package.jsons to original state. - postPublish({ preparedPackages }) -} - -main() - -////////////////////////////////////////////////////////////////////////// -// Prepare - -function prepare({ packages }: { packages: Package[] }) { - const packageDirs = packages - .filter(({ dir }) => packagesDir.some((i) => dir.startsWith(i))) - .map(({ dir }) => dir) - - const preparedPackages: PreparedPackage[] = [] - for (const packageDir of packageDirs) { - const packageJsonPath = path.join(packageDir, 'package.json') - const packageJson = readJsonSync(packageJsonPath) - const oldPackageJson = { ...packageJson } - - delete packageJson.type - writeJsonSync(packageJsonPath, packageJson, { spaces: 2 }) - - preparedPackages.push({ dir: packageDir, oldPackageJson, packageJson }) - } - return preparedPackages -} - -////////////////////////////////////////////////////////////////////////// -// Build - -async function build() { - await execa('pnpm', ['build'], { - env: { - FORMAT: 'cjs', - NODE_ENV: 'production', - }, - cwd: path.join(__dirname, '..'), - stdout: process.stdout, - }) -} - -////////////////////////////////////////////////////////////////////////// -// Version - -function version({ - changedPackages, - packages, -}: { - changedPackages: Package[] - packages: Package[] -}) { - const referencesPackages = packages.filter(({ dir }) => - referencesDir.some((i) => dir.startsWith(i)), - ) - - for (const { dir, packageJson } of changedPackages) { - const newPackageJson = { ...packageJson } - - newPackageJson.version = `${packageJson.version}-cjs` - for (const { packageJson } of referencesPackages) { - if (newPackageJson.dependencies?.[packageJson.name]) { - newPackageJson.dependencies[ - packageJson.name - ] = `${packageJson.version}-cjs` - } - } - - writeJsonSync(path.join(dir, 'package.json'), newPackageJson, { spaces: 2 }) - } -} - -////////////////////////////////////////////////////////////////////////// -// Publish - -async function publish({ changedPackages }: { changedPackages: Package[] }) { - for (const { dir } of changedPackages) { - if (dir.includes('cli')) continue - if (referencesDir.some((i) => dir.startsWith(i))) return - await execa('pnpm', ['publish', '--tag', 'cjs', '--no-git-checks'], { - cwd: dir, - stdout: process.stdout, - }) - } -} - -////////////////////////////////////////////////////////////////////////// -// Post-publish - -function postPublish({ - preparedPackages, -}: { - preparedPackages: PreparedPackage[] -}) { - // Restore package.jsons - for (const { oldPackageJson, dir } of preparedPackages) { - writeJsonSync(path.join(dir, 'package.json'), oldPackageJson, { spaces: 2 }) - } -}