Skip to content

Commit

Permalink
fix(cli-cache): Remove RW CLI cache on upgrade (#11135)
Browse files Browse the repository at this point in the history
This change removes the redwood cli plugin cache in
`.redwood/commandCache.json` on running `rw upgrade`.

This prevents the redwood CLI from using outdated versions of CLI
plugins, and is particularly important when they same alias.

**Before**
running `yarn rw sb` -> would use the outdated storybook-cli package,
and error out.

**After**
with no commandCache, it'll create a new command cache, and attempt to
install the new vite CLI
  • Loading branch information
dac09 authored and Josh-Walker-GM committed Aug 9, 2024
1 parent d8fdd07 commit ec97d4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .changesets/11135.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- fix(cli-cache): Remove RW CLI cache on upgrade (#11135) by @dac09

This change removes the redwood cli plugin cache in `.redwood/commandCache.json` on running `rw upgrade`.

This prevents the redwood CLI from using outdated versions of CLI plugins, and is particularly important when they same alias.

**Before**
running `yarn rw sb` -> would use the outdated storybook-cli package, and error out.

**After**
with no commandCache, it'll create a new command cache, and attempt to install the new vite CLI
24 changes: 24 additions & 0 deletions packages/cli/src/commands/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getConfig } from '@redwoodjs/project-config'
import { getPaths } from '../lib'
import c from '../lib/colors'
import { generatePrismaClient } from '../lib/generatePrismaClient'
import { PLUGIN_CACHE_FILENAME } from '../lib/plugin'

export const command = 'upgrade'
export const description = 'Upgrade all @redwoodjs packages via interactive CLI'
Expand Down Expand Up @@ -116,6 +117,10 @@ export const handler = async ({ dryRun, tag, verbose, dedupe }) => {
task: (ctx) => downloadYarnPatches(ctx, { dryRun, verbose }),
enabled: (ctx) => ctx.versionToUpgradeTo?.includes('canary'),
},
{
title: 'Removing CLI cache',
task: (ctx) => removeCliCache(ctx, { dryRun, verbose }),
},
{
title: 'Running yarn install',
task: (ctx) => yarnInstall(ctx, { dryRun, verbose }),
Expand Down Expand Up @@ -206,6 +211,25 @@ async function yarnInstall({ verbose }) {
}
}

/**
* Removes the CLI plugin cache. This prevents the CLI from using outdated versions of the plugin,
* when the plugins share the same alias. e.g. `rw sb` used to point to `@redwoodjs/cli-storybook` but now points to `@redwoodjs/cli-storybook-vite`
*/
async function removeCliCache(ctx, { dryRun, verbose }) {
const cliCacheDir = path.join(
getPaths().generated.base,
PLUGIN_CACHE_FILENAME,
)

if (verbose) {
console.log('Removing CLI cache at: ', cliCacheDir)
}

if (!dryRun) {
fs.removeSync(cliCacheDir)
}
}

async function setLatestVersionToContext(ctx, tag) {
try {
const foundVersion = await latestVersion(
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { Select } = require('enquirer')
/**
* The file inside .redwood which will contain cached plugin command mappings
*/
const PLUGIN_CACHE_FILENAME = 'commandCache.json'
export const PLUGIN_CACHE_FILENAME = 'commandCache.json'

/**
* A cache of yargs information for redwood commands that are available from plugins.
Expand Down

0 comments on commit ec97d4b

Please sign in to comment.