diff --git a/.changeset/lucky-clocks-love.md b/.changeset/lucky-clocks-love.md new file mode 100644 index 0000000000..f2475d3d8f --- /dev/null +++ b/.changeset/lucky-clocks-love.md @@ -0,0 +1,7 @@ +--- +"@latticexyz/cli": major +--- + +Removes `.mudbackup` file handling and `--backup`, `--restore`, and `--force` options from `mud set-version` command. + +To revert to a previous MUD version, use `git diff` to find the version that you changed from and want to revert to and run `pnpm mud set-version ` again. diff --git a/examples/minimal/package.json b/examples/minimal/package.json index b0a64194f2..f508783067 100644 --- a/examples/minimal/package.json +++ b/examples/minimal/package.json @@ -14,6 +14,7 @@ "test": "pnpm recursive run test" }, "devDependencies": { + "@latticexyz/cli": "link:../../packages/cli", "@typescript-eslint/eslint-plugin": "5.46.1", "@typescript-eslint/parser": "5.46.1", "concurrently": "^8.0.1", diff --git a/examples/minimal/pnpm-lock.yaml b/examples/minimal/pnpm-lock.yaml index d57f1c1dae..f8856f3c92 100644 --- a/examples/minimal/pnpm-lock.yaml +++ b/examples/minimal/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: devDependencies: + '@latticexyz/cli': + specifier: link:../../packages/cli + version: link:../../packages/cli '@typescript-eslint/eslint-plugin': specifier: 5.46.1 version: 5.46.1(@typescript-eslint/parser@5.46.1)(eslint@8.29.0)(typescript@5.1.6) diff --git a/packages/cli/src/commands/set-version.ts b/packages/cli/src/commands/set-version.ts index d1afe4a4ec..6bb18f6f52 100644 --- a/packages/cli/src/commands/set-version.ts +++ b/packages/cli/src/commands/set-version.ts @@ -1,11 +1,12 @@ import chalk from "chalk"; -import { existsSync, readFileSync, rmSync, writeFileSync } from "fs"; +import { readFileSync, writeFileSync } from "fs"; import path from "path"; import type { CommandModule } from "yargs"; import { MUDError } from "@latticexyz/common/errors"; import { logError } from "../utils/errors"; import localPackageJson from "../../package.json" assert { type: "json" }; import glob from "glob"; +import { mudPackages } from "../mudPackages"; type Options = { backup?: boolean; @@ -17,9 +18,6 @@ type Options = { link?: string; }; -const BACKUP_FILE = ".mudbackup"; -const MUD_PREFIX = "@latticexyz"; - const commandModule: CommandModule = { command: "set-version", @@ -27,12 +25,6 @@ const commandModule: CommandModule = { builder(yargs) { return yargs.options({ - backup: { type: "boolean", description: `Back up the current MUD versions to "${BACKUP_FILE}"` }, - force: { - type: "boolean", - description: `Backup fails if a "${BACKUP_FILE}" file is found, unless --force is provided`, - }, - restore: { type: "boolean", description: `Restore the previous MUD versions from "${BACKUP_FILE}"` }, mudVersion: { alias: "v", type: "string", description: "Set MUD to the given version" }, tag: { alias: "t", @@ -117,63 +109,39 @@ async function resolveVersion(options: Options) { } function updatePackageJson(filePath: string, options: Options): { workspaces?: string[] } { - const { restore, force, link } = options; - let { backup, mudVersion } = options; - - const backupFilePath = path.join(path.dirname(filePath), BACKUP_FILE); - const backupFileExists = existsSync(backupFilePath); - - // Create a backup file for previous MUD versions by default if linking to local MUD - if (link && !backupFileExists) backup = true; - - // If `backup` is true and force not set, check if a backup file already exists and throw an error if it does - if (backup && !force && backupFileExists) { - throw new MUDError( - `A backup file already exists at ${backupFilePath}.\nUse --force to overwrite it or --restore to restore it.` - ); - } + const { link } = options; + let { mudVersion } = options; const packageJson = readPackageJson(filePath); - - // Load .mudbackup if `restore` is true - const backupJson = restore ? readPackageJson(backupFilePath) : undefined; + const mudPackageNames = Object.keys(mudPackages); // Find all MUD dependencies const mudDependencies: Record = {}; - for (const key in packageJson.dependencies) { - if (key.startsWith(MUD_PREFIX)) { - mudDependencies[key] = packageJson.dependencies[key]; + for (const packageName in packageJson.dependencies) { + if (mudPackageNames.includes(packageName)) { + mudDependencies[packageName] = packageJson.dependencies[packageName]; } } // Find all MUD devDependencies const mudDevDependencies: Record = {}; - for (const key in packageJson.devDependencies) { - if (key.startsWith(MUD_PREFIX)) { - mudDevDependencies[key] = packageJson.devDependencies[key]; + for (const packageName in packageJson.devDependencies) { + if (mudPackageNames.includes(packageName)) { + mudDevDependencies[packageName] = packageJson.devDependencies[packageName]; } } - // Back up the current dependencies if `backup` is true - if (backup) { - writeFileSync( - backupFilePath, - JSON.stringify({ dependencies: mudDependencies, devDependencies: mudDevDependencies }, null, 2) - ); - console.log(chalk.green(`Backed up MUD dependencies from ${filePath} to ${backupFilePath}`)); - } - // Update the dependencies - for (const key in packageJson.dependencies) { - if (key.startsWith(MUD_PREFIX)) { - packageJson.dependencies[key] = resolveMudVersion(key, "dependencies"); + for (const packageName in packageJson.dependencies) { + if (mudPackageNames.includes(packageName)) { + packageJson.dependencies[packageName] = resolveMudVersion(packageName, "dependencies"); } } // Update the devDependencies - for (const key in packageJson.devDependencies) { - if (key.startsWith(MUD_PREFIX)) { - packageJson.devDependencies[key] = resolveMudVersion(key, "devDependencies"); + for (const packageName in packageJson.devDependencies) { + if (mudPackageNames.includes(packageName)) { + packageJson.devDependencies[packageName] = resolveMudVersion(packageName, "devDependencies"); } } @@ -184,17 +152,9 @@ function updatePackageJson(filePath: string, options: Options): { workspaces?: s logComparison(mudDependencies, packageJson.dependencies); logComparison(mudDevDependencies, packageJson.devDependencies); - // Remove the backup file if `restore` is true and `backup` is false - // because the old backup file is no longer needed - if (restore && !backup) { - rmSync(backupFilePath); - console.log(chalk.green(`Cleaned up ${backupFilePath}`)); - } - return packageJson; function resolveMudVersion(key: string, type: "dependencies" | "devDependencies") { - if (restore && backupJson) return backupJson[type][key]; if (link) mudVersion = resolveLinkPath(filePath, link, key); if (!mudVersion) return packageJson[type][key]; return mudVersion; @@ -225,10 +185,9 @@ function logComparison(prev: Record, curr: Record; diff --git a/packages/cli/src/mudPackages.ts b/packages/cli/src/mudPackages.ts new file mode 100644 index 0000000000..8392dac891 --- /dev/null +++ b/packages/cli/src/mudPackages.ts @@ -0,0 +1,24 @@ +import { ZodError, z } from "zod"; +import { MudPackages } from "./common"; + +const envSchema = z.object({ + MUD_PACKAGES: z.string().transform((value) => JSON.parse(value) as MudPackages), +}); + +function parseEnv(): z.infer { + try { + return envSchema.parse({ + // tsup replaces the env vars with their values at compile time + MUD_PACKAGES: process.env.MUD_PACKAGES, + }); + } catch (error) { + if (error instanceof ZodError) { + const { _errors, ...invalidEnvVars } = error.format(); + console.error(`\nMissing or invalid environment variables:\n\n ${Object.keys(invalidEnvVars).join("\n ")}\n`); + process.exit(1); + } + throw error; + } +} + +export const mudPackages = parseEnv().MUD_PACKAGES; diff --git a/packages/cli/tsup.config.ts b/packages/cli/tsup.config.ts index fee18561b4..8e74fd917d 100644 --- a/packages/cli/tsup.config.ts +++ b/packages/cli/tsup.config.ts @@ -1,4 +1,21 @@ import { defineConfig } from "tsup"; +import glob from "glob"; +import { readFileSync } from "node:fs"; +import path from "node:path"; +import { MudPackages } from "./src/common"; + +const mudWorkspace = path.normalize(`${__dirname}/../..`); + +const mudPackages: MudPackages = Object.fromEntries( + glob + .sync(path.join(mudWorkspace, `packages/*/package.json`)) + .map((filename) => [ + path.relative(mudWorkspace, path.dirname(filename)), + JSON.parse(readFileSync(filename, "utf8")), + ]) + .filter(([, packageJson]) => !packageJson.private) + .map(([localPath, packageJson]) => [packageJson.name, { localPath }]) +); export default defineConfig({ entry: ["src/index.ts", "src/mud.ts"], @@ -8,4 +25,7 @@ export default defineConfig({ sourcemap: true, clean: true, minify: true, + env: { + MUD_PACKAGES: JSON.stringify(mudPackages), + }, });