-
Notifications
You must be signed in to change notification settings - Fork 86
Add update notifier to CLI #3096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
79fa700
Add update notifier to CLI
youknowriad 13beb40
Address PR review feedback for update notifier
youknowriad bd59655
Store update check cache in cli.json instead of separate file
youknowriad f9d9e48
Await update notifier and show banner on first run
youknowriad 44614db
Fix lint error: await async setupUpdateNotifier in test
youknowriad fa295fd
Guard against negative rightPad in update banner
youknowriad 0eebd17
Disable update notifier for bundled CLI
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add update notifier to CLI
Show a banner at the top of CLI output when a newer version of wp-studio is available on npm. Includes a changelog link to the Studio docs page. The check is cached for 24 hours and suppressed in IPC mode or with --json. Fixes STU-1449 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Loading branch information
commit 79fa700c0b4e54a2ffc1d2f284ad690e91f7dafc
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* eslint-disable no-control-regex */ | ||
| import { type MockInstance, vi } from 'vitest'; | ||
| import { formatUpdateBanner, setupUpdateNotifier } from 'cli/lib/update-notifier'; | ||
|
|
||
| describe( 'formatUpdateBanner', () => { | ||
| it( 'should include version numbers', () => { | ||
| const banner = formatUpdateBanner( '1.7.8', '1.8.0' ); | ||
| const plain = banner.replace( /\u001B\[[0-9;]*m/g, '' ); | ||
| expect( plain ).toContain( '1.7.8' ); | ||
| expect( plain ).toContain( '1.8.0' ); | ||
| } ); | ||
|
|
||
| it( 'should include the npm update command', () => { | ||
| const banner = formatUpdateBanner( '1.7.8', '1.8.0' ); | ||
| const plain = banner.replace( /\u001B\[[0-9;]*m/g, '' ); | ||
| expect( plain ).toContain( 'npm update -g wp-studio' ); | ||
| } ); | ||
|
|
||
| it( 'should include the changelog URL', () => { | ||
| const banner = formatUpdateBanner( '1.7.8', '1.8.0' ); | ||
| const plain = banner.replace( /\u001B\[[0-9;]*m/g, '' ); | ||
| expect( plain ).toContain( | ||
| 'https://developer.wordpress.com/docs/developer-tools/studio/changelog/' | ||
| ); | ||
| } ); | ||
|
|
||
| it( 'should be wrapped in a box', () => { | ||
| const banner = formatUpdateBanner( '1.0.0', '2.0.0' ); | ||
| const plain = banner.replace( /\u001B\[[0-9;]*m/g, '' ); | ||
| expect( plain ).toContain( '╭' ); | ||
| expect( plain ).toContain( '╰' ); | ||
| expect( plain ).toContain( '│' ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'setupUpdateNotifier', () => { | ||
| const originalSend = process.send; | ||
| let stderrWriteSpy: MockInstance; | ||
|
|
||
| beforeEach( () => { | ||
| stderrWriteSpy = vi.spyOn( process.stderr, 'write' ).mockImplementation( () => true ); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| process.send = originalSend; | ||
| stderrWriteSpy.mockRestore(); | ||
| } ); | ||
|
|
||
| it( 'should not show banner when in IPC mode', () => { | ||
| process.send = vi.fn(); | ||
| setupUpdateNotifier( '1.0.0' ); | ||
| expect( stderrWriteSpy ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { getConfigDirectory } from '@studio/common/lib/well-known-paths'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import chalk from 'chalk'; | ||
| import semver from 'semver'; | ||
|
|
||
| const UPDATE_CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours | ||
| const FETCH_TIMEOUT_MS = 5000; | ||
| const NPM_REGISTRY_URL = 'https://registry.npmjs.org/wp-studio/latest'; | ||
| const CHANGELOG_URL = 'https://developer.wordpress.com/docs/developer-tools/studio/changelog/'; | ||
| const CACHE_FILE_NAME = 'cli-update-check.json'; | ||
|
|
||
| interface UpdateCheckCache { | ||
| lastChecked: number; | ||
| latestVersion: string; | ||
| } | ||
|
|
||
| function getCacheFilePath(): string { | ||
| return path.join( getConfigDirectory(), CACHE_FILE_NAME ); | ||
| } | ||
|
|
||
| function readCache(): UpdateCheckCache | null { | ||
| try { | ||
| const content = fs.readFileSync( getCacheFilePath(), 'utf8' ); | ||
| const data = JSON.parse( content ); | ||
| if ( typeof data.lastChecked === 'number' && typeof data.latestVersion === 'string' ) { | ||
| return data as UpdateCheckCache; | ||
| } | ||
| } catch { | ||
| // Cache doesn't exist or is invalid | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function writeCache( cache: UpdateCheckCache ): void { | ||
| try { | ||
| const configDir = getConfigDirectory(); | ||
| if ( ! fs.existsSync( configDir ) ) { | ||
| fs.mkdirSync( configDir, { recursive: true } ); | ||
| } | ||
| fs.writeFileSync( getCacheFilePath(), JSON.stringify( cache ), 'utf8' ); | ||
| } catch { | ||
| // Non-critical, ignore write failures | ||
| } | ||
| } | ||
|
|
||
| async function fetchLatestVersion(): Promise< string | null > { | ||
| try { | ||
| const controller = new AbortController(); | ||
| const timeout = setTimeout( () => controller.abort(), FETCH_TIMEOUT_MS ); | ||
|
youknowriad marked this conversation as resolved.
|
||
|
|
||
| const response = await fetch( NPM_REGISTRY_URL, { | ||
| signal: controller.signal, | ||
| headers: { Accept: 'application/json' }, | ||
| } ); | ||
| clearTimeout( timeout ); | ||
|
|
||
| if ( ! response.ok ) { | ||
| return null; | ||
| } | ||
|
|
||
| const data = ( await response.json() ) as { version?: string }; | ||
| return typeof data.version === 'string' ? data.version : null; | ||
|
youknowriad marked this conversation as resolved.
Outdated
|
||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function hasJsonFlag(): boolean { | ||
| return process.argv.includes( '--json' ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks for available updates and displays a banner at the top of output. | ||
| * | ||
| * Reads from a local cache file synchronously so the banner prints immediately, | ||
| * before any command output. A background fetch refreshes the cache for the next | ||
| * invocation when it is stale or missing (same pattern as npm's update-notifier). | ||
| * | ||
| * The banner is suppressed in IPC mode or when --json flag is used. | ||
| */ | ||
| export function setupUpdateNotifier( currentVersion: string ): void { | ||
| if ( Boolean( process.send ) || hasJsonFlag() ) { | ||
| return; | ||
| } | ||
|
|
||
| const cache = readCache(); | ||
| const now = Date.now(); | ||
|
|
||
| // Start a background fetch if cache is stale or missing | ||
| if ( ! cache || now - cache.lastChecked >= UPDATE_CHECK_INTERVAL_MS ) { | ||
| // Fire and forget -- the result will be cached for the next invocation | ||
| void fetchLatestVersion().then( ( version ) => { | ||
| if ( version ) { | ||
| writeCache( { lastChecked: now, latestVersion: version } ); | ||
| } | ||
| } ); | ||
| } | ||
|
|
||
| // Show the banner immediately from cache (before any command output). | ||
| // On the first run the cache won't exist yet, so the banner won't show | ||
| // until the next invocation. | ||
| if ( | ||
| cache && | ||
| semver.valid( cache.latestVersion ) && | ||
| semver.valid( currentVersion ) && | ||
| semver.gt( cache.latestVersion, currentVersion ) | ||
| ) { | ||
| process.stderr.write( formatUpdateBanner( currentVersion, cache.latestVersion ) ); | ||
| } | ||
| } | ||
|
|
||
| export function formatUpdateBanner( currentVersion: string, latestVersion: string ): string { | ||
| const updateLine = sprintf( | ||
| /* translators: 1: current version, 2: latest version */ | ||
| __( 'Update available: %1$s → %2$s' ), | ||
| chalk.dim( currentVersion ), | ||
| chalk.green( latestVersion ) | ||
| ); | ||
| const commandLine = sprintf( | ||
| /* translators: %s is the npm command to run */ | ||
| __( 'Run %s to update' ), | ||
| chalk.cyan( 'npm update -g wp-studio' ) | ||
| ); | ||
| const changelogLine = sprintf( | ||
| /* translators: %s is the changelog URL */ | ||
| __( 'Changelog: %s' ), | ||
| chalk.cyan( CHANGELOG_URL ) | ||
| ); | ||
|
|
||
| const lines = [ '', updateLine, commandLine, '', changelogLine, '' ]; | ||
|
|
||
| // Calculate box width based on longest line (strip ANSI for measurement) | ||
| // eslint-disable-next-line no-control-regex | ||
| const ansiPattern = new RegExp( '\u001B\\[[0-9;]*m', 'g' ); | ||
| const stripAnsi = ( str: string ) => str.replace( ansiPattern, '' ); | ||
| const maxLen = Math.max( ...lines.map( ( l ) => stripAnsi( l ).length ) ); | ||
| const padding = 2; | ||
| const innerWidth = maxLen + padding * 2; | ||
|
|
||
| const top = chalk.yellow( `╭${ '─'.repeat( innerWidth ) }╮` ); | ||
| const bottom = chalk.yellow( `╰${ '─'.repeat( innerWidth ) }╯` ); | ||
| const side = chalk.yellow( '│' ); | ||
|
|
||
| const paddedLines = lines.map( ( line ) => { | ||
| const visibleLen = stripAnsi( line ).length; | ||
| const rightPad = innerWidth - padding - visibleLen; | ||
|
youknowriad marked this conversation as resolved.
Outdated
|
||
| return `${ side }${ ' '.repeat( padding ) }${ line }${ ' '.repeat( rightPad ) }${ side }`; | ||
| } ); | ||
|
|
||
| return [ '', top, ...paddedLines, bottom, '' ].join( '\n' ); | ||
| } | ||
|
youknowriad marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.