Skip to content

Commit af940d5

Browse files
committed
refactor to add logs to app model
1 parent 9c5ce8e commit af940d5

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

packages/app/src/cli/models/app/app.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export interface AppInterface<
318318
updateHiddenConfig: (values: Partial<AppHiddenConfig>) => Promise<void>
319319
setDevApplicationURLs: (devApplicationURLs: ApplicationURLs) => void
320320
generateExtensionTypes(): Promise<void>
321+
getLogsDir(): string
321322
}
322323

323324
type AppConstructor<
@@ -445,6 +446,14 @@ export class App<
445446
return this._hiddenConfig
446447
}
447448

449+
getLogsDir() {
450+
// Create directory name using app name + partial client_id for uniqueness
451+
const clientId = String(this.configuration.client_id || 'unknown')
452+
const shortClientId = clientId.substring(0, 8)
453+
const dirName = `${this.name}-${shortClientId}`
454+
return joinPath(this.directory, '.shopify', 'logs', dirName)
455+
}
456+
448457
async updateHiddenConfig(values: Partial<AppHiddenConfig>) {
449458
if (!this.configuration.client_id) return
450459
this._hiddenConfig = deepMergeObjects(this.hiddenConfig, values)

packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,21 @@ import {Writable} from 'stream'
2323
export const pollAppLogs = async ({
2424
stdout,
2525
appLogsFetchInput: {jwtToken, cursor},
26-
apiKey,
2726
developerPlatformClient,
2827
resubscribeCallback,
2928
storeName,
3029
organizationId,
3130
abortSignal,
32-
appDirectory,
31+
logsDir,
3332
}: {
3433
stdout: Writable
3534
appLogsFetchInput: AppLogsOptions
36-
apiKey: string
3735
developerPlatformClient: DeveloperPlatformClient
3836
resubscribeCallback: () => Promise<string>
3937
storeName: string
4038
organizationId: string
4139
abortSignal?: AbortSignal
42-
appDirectory: string
40+
logsDir: string
4341
}) => {
4442
if (abortSignal?.aborted) {
4543
return
@@ -71,10 +69,9 @@ export const pollAppLogs = async ({
7169
const logFile = await writeAppLogsToFile({
7270
appLog: log,
7371
appLogPayload: payload,
74-
apiKey,
7572
stdout,
7673
storeName,
77-
appDirectory,
74+
logsDir,
7875
})
7976
stdout.write(
8077
outputContent`${outputToken.gray('└ ')}${outputToken.link(
@@ -120,13 +117,12 @@ export const pollAppLogs = async ({
120117
jwtToken: nextJwtToken,
121118
cursor: responseCursor || cursor,
122119
},
123-
apiKey,
124120
developerPlatformClient,
125121
resubscribeCallback,
126122
storeName,
127123
organizationId,
128124
abortSignal,
129-
appDirectory,
125+
logsDir,
130126
}).catch((error) => {
131127
outputDebug(`Unexpected error during polling: ${error}}\n`)
132128
})
@@ -144,13 +140,12 @@ export const pollAppLogs = async ({
144140
jwtToken,
145141
cursor: undefined,
146142
},
147-
apiKey,
148143
developerPlatformClient,
149144
resubscribeCallback,
150145
storeName,
151146
organizationId,
152147
abortSignal,
153-
appDirectory,
148+
logsDir,
154149
}).catch((error) => {
155150
outputDebug(`Unexpected error during polling: ${error}}\n`)
156151
})

packages/app/src/cli/services/app-logs/dev/write-app-logs.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,25 @@ interface AppLogFile {
1313
export const writeAppLogsToFile = async ({
1414
appLog,
1515
appLogPayload,
16-
apiKey,
1716
stdout,
1817
storeName,
19-
appDirectory,
18+
logsDir,
2019
}: {
2120
appLog: AppLogData
2221
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2322
appLogPayload: any
24-
apiKey: string
2523
stdout: Writable
2624
storeName: string
27-
appDirectory: string
25+
logsDir: string
2826
}): Promise<AppLogFile> => {
2927
const identifier = randomUUID().substring(0, 6)
3028

3129
const formattedTimestamp = formatTimestampToFilename(appLog.log_timestamp)
3230
const fileName = `${formattedTimestamp}_${appLog.source_namespace}_${appLog.source}_${identifier}.json`
3331
const logContent = toFormattedAppLogJson({appLog, appLogPayload, prettyPrint: true, storeName})
3432

35-
// Write to app's .shopify/logs directory
36-
const fullOutputPath = joinPath(appDirectory, '.shopify', 'logs', apiKey, fileName)
33+
// Write to the provided logs directory
34+
const fullOutputPath = joinPath(logsDir, fileName)
3735

3836
try {
3937
// Ensure parent directory exists and write the file

packages/app/src/cli/services/dev/processes/app-logs-polling.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {subscribeToAppLogs} from '../../app-logs/utils.js'
66
import {AppLinkedInterface} from '../../../models/app/app.js'
77
import {AppEventWatcher, AppEvent} from '../app-events/app-event-watcher.js'
88

9-
import {createLogsDir} from '@shopify/cli-kit/node/logs'
9+
import {mkdir} from '@shopify/cli-kit/node/fs'
1010
import {outputDebug} from '@shopify/cli-kit/node/output'
1111

1212
function hasFunctionExtensions(app: AppLinkedInterface): boolean {
@@ -76,21 +76,20 @@ export const subscribeAndStartPolling: DevProcessFunction<SubscribeAndStartPolli
7676
stdout,
7777
)
7878

79-
const apiKey = appLogsSubscribeVariables.apiKey
80-
await createLogsDir(apiKey)
79+
const logsDir = localApp.getLogsDir()
80+
await mkdir(logsDir)
8181

8282
await pollAppLogs({
8383
stdout,
8484
appLogsFetchInput: {jwtToken},
85-
apiKey,
8685
resubscribeCallback: () => {
8786
return subscribeToAppLogs(developerPlatformClient, appLogsSubscribeVariables, organizationId)
8887
},
8988
developerPlatformClient,
9089
storeName,
9190
organizationId,
9291
abortSignal,
93-
appDirectory: localApp.directory,
92+
logsDir,
9493
})
9594
}
9695

packages/app/src/cli/services/function/replay.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ export async function replay(options: ReplayOptions) {
5252
const abortController = new AbortController()
5353

5454
try {
55-
const apiKey = options.app.configuration.client_id
56-
const functionRunsDir = joinPath(app.directory, '.shopify', 'logs', apiKey)
55+
const functionRunsDir = app.getLogsDir()
5756

5857
const selectedRun = options.log
5958
? await getRunFromIdentifier(functionRunsDir, extension.handle, options.log)

0 commit comments

Comments
 (0)