Skip to content

Commit 98e144d

Browse files
Fetch notifications as part of the list command and run that in background
1 parent 276b93c commit 98e144d

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

packages/cli-kit/assets/fetch-notifications.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/cli-kit/src/public/node/hooks/postrun.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {reportAnalyticsEvent} from '../analytics.js'
33
import {outputDebug} from '../../../public/node/output.js'
44
import BaseCommand from '../base-command.js'
55
import * as metadata from '../../../public/node/metadata.js'
6-
import {exec} from '../system.js'
6+
import {fetchNotificationsInBackground} from '../notifications-system.js'
77
import {Command, Hook} from '@oclif/core'
88

99
// This hook is called after each successful command run. More info: https://oclif.io/docs/hooks
1010
export const hook: Hook.Postrun = async ({config, Command}) => {
1111
await detectStopCommand(Command as unknown as typeof Command)
1212
await reportAnalyticsEvent({config, exitMode: 'ok'})
13-
await exec('node', [require.resolve('@shopify/cli-kit/assets/fetch-notifications.js')], {background: true})
13+
fetchNotificationsInBackground()
1414
deprecationsHook(Command)
1515

1616
const command = Command.id.replace(/:/g, ' ')

packages/cli-kit/src/public/node/notifications-system.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ import {zod} from './schema.js'
66
import {AbortSilentError} from './error.js'
77
import {isTruthy} from './context/utilities.js'
88
import {jsonOutputEnabled} from './environment.js'
9+
import {exec} from './system.js'
910
import {CLI_KIT_VERSION} from '../common/version.js'
1011
import {NotificationKey, NotificationsKey, cacheRetrieve, cacheStore} from '../../private/node/conf-store.js'
12+
import {fetch} from '@shopify/cli-kit/node/http'
1113

1214
const URL = 'https://cdn.shopify.com/static/cli/notifications.json'
1315
const EMPTY_CACHE_MESSAGE = 'Cache is empty'
1416

15-
/**
16-
* Returns the URL to retrieve the notifications.
17-
*
18-
* @returns - The value from SHOPIFY_CLI_NOTIFICATIONS_URL or the default URL (https://cdn.shopify.com/static/cli/notifications.json).
19-
*/
20-
export function notificationsUrl(): string {
17+
function url(): string {
2118
return process.env.SHOPIFY_CLI_NOTIFICATIONS_URL ?? URL
2219
}
2320

@@ -112,18 +109,55 @@ async function renderNotifications(notifications: Notification[]) {
112109
}
113110

114111
/**
115-
* Get notifications list from cache, that is updated in the background from the fetch-notifications.json script.
112+
* Get notifications list from cache, that is updated in the background from bin/fetch-notifications.json.
116113
*
117114
* @returns A Notifications object.
118115
*/
119116
export async function getNotifications(): Promise<Notifications> {
120-
const cacheKey: NotificationsKey = `notifications-${notificationsUrl()}`
117+
const cacheKey: NotificationsKey = `notifications-${url()}`
121118
const rawNotifications = cacheRetrieve(cacheKey)?.value as unknown as string
122119
if (!rawNotifications) throw new Error(EMPTY_CACHE_MESSAGE)
123120
const notifications: object = JSON.parse(rawNotifications)
124121
return NotificationsSchema.parse(notifications)
125122
}
126123

124+
/**
125+
* Fetch notifications from the CDN and chache them.
126+
*
127+
* @returns A string with the notifications.
128+
*/
129+
export async function fetchNotifications(): Promise<Notifications> {
130+
outputDebug(`Fetching notifications...`)
131+
const response = await fetch(url(), {signal: AbortSignal.timeout(3 * 1000)})
132+
if (response.status !== 200) throw new Error(`Failed to fetch notifications: ${response.statusText}`)
133+
const rawNotifications = await response.text()
134+
await cacheNotifications(rawNotifications)
135+
const notifications: object = JSON.parse(rawNotifications)
136+
return NotificationsSchema.parse(notifications)
137+
}
138+
139+
/**
140+
* Store the notifications in the cache.
141+
*
142+
* @param notifications - String with the notifications to cache.
143+
* @returns A Notifications object.
144+
*/
145+
async function cacheNotifications(notifications: string): Promise<void> {
146+
cacheStore(`notifications-${url()}`, notifications)
147+
outputDebug(`Notifications from ${url()} stored in the cache`)
148+
}
149+
150+
/**
151+
* Fetch notifications in background as a detached process.
152+
*/
153+
export function fetchNotificationsInBackground(): void {
154+
// eslint-disable-next-line no-void
155+
void exec('shopify', ['notifications', 'list'], {
156+
background: true,
157+
env: {...process.env, SHOPIFY_CLI_NO_ANALYTICS: '1'},
158+
})
159+
}
160+
127161
/**
128162
* Filters notifications based on the version of the CLI.
129163
*

packages/cli/src/cli/services/commands/notifications.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Notification,
77
stringifyFilters,
88
getNotifications,
9+
fetchNotifications,
910
} from '@shopify/cli-kit/node/notifications-system'
1011
import {outputInfo} from '@shopify/cli-kit/node/output'
1112
import {renderSelectPrompt, renderTextPrompt, renderSuccess, renderTable, TableColumn} from '@shopify/cli-kit/node/ui'
@@ -95,7 +96,7 @@ export async function generate() {
9596
}
9697

9798
export async function list() {
98-
const notifications: Notifications = await getNotifications()
99+
const notifications = await fetchNotifications()
99100

100101
const columns: TableColumn<{type: string; title: string; message: string; filters: string}> = {
101102
type: {header: 'Type', color: 'dim'},
@@ -107,7 +108,7 @@ export async function list() {
107108
const rows = notifications.notifications.map((notification: Notification) => {
108109
return {
109110
type: notification.type,
110-
title: notification.title || '',
111+
title: notification.title ?? '',
111112
message: notification.message,
112113
filters: stringifyFilters(notification),
113114
}

0 commit comments

Comments
 (0)