|
| 1 | +import {functionFlags, inFunctionContext} from '../../../services/function/common.js' |
| 2 | +import {replay} from '../../../services/function/replay.js' |
| 3 | +import {appFlags} from '../../../flags.js' |
| 4 | +import {showApiKeyDeprecationWarning} from '../../../prompts/deprecation-warnings.js' |
| 5 | +import {environmentVariableNames} from '../../../constants.js' |
| 6 | +import Command from '@shopify/cli-kit/node/base-command' |
| 7 | +import {globalFlags} from '@shopify/cli-kit/node/cli' |
| 8 | +import {Flags} from '@oclif/core' |
| 9 | +import {getEnvironmentVariables} from '@shopify/cli-kit/node/environment' |
| 10 | +import {isTruthy} from '@shopify/cli-kit/node/context/utilities' |
| 11 | + |
| 12 | +export default class FunctionReplay extends Command { |
| 13 | + static hidden = true |
| 14 | + static summary = 'Replays a function run from an app log.' |
| 15 | + |
| 16 | + static descriptionWithMarkdown = `Runs the function from your current directory for [testing purposes](https://shopify.dev/docs/apps/functions/testing-and-debugging). To learn how you can monitor and debug functions when errors occur, refer to [Shopify Functions error handling](https://shopify.dev/docs/api/functions/errors).` |
| 17 | + |
| 18 | + static description = this.descriptionWithoutMarkdown() |
| 19 | + |
| 20 | + static flags = { |
| 21 | + ...globalFlags, |
| 22 | + ...appFlags, |
| 23 | + ...functionFlags, |
| 24 | + 'api-key': Flags.string({ |
| 25 | + hidden: true, |
| 26 | + description: "Application's API key", |
| 27 | + env: 'SHOPIFY_FLAG_API_KEY', |
| 28 | + exclusive: ['config'], |
| 29 | + }), |
| 30 | + 'client-id': Flags.string({ |
| 31 | + hidden: false, |
| 32 | + description: "Application's Client ID", |
| 33 | + env: 'SHOPIFY_FLAG_CLIENT_ID', |
| 34 | + exclusive: ['config'], |
| 35 | + }), |
| 36 | + export: Flags.string({ |
| 37 | + char: 'e', |
| 38 | + hidden: false, |
| 39 | + description: 'Name of the WebAssembly export to invoke.', |
| 40 | + default: '_start', |
| 41 | + env: 'SHOPIFY_FLAG_EXPORT', |
| 42 | + }), |
| 43 | + json: Flags.boolean({ |
| 44 | + char: 'j', |
| 45 | + hidden: false, |
| 46 | + description: 'Output the function run result as a JSON object.', |
| 47 | + env: 'SHOPIFY_FLAG_JSON', |
| 48 | + }), |
| 49 | + } |
| 50 | + |
| 51 | + public async run() { |
| 52 | + const env = getEnvironmentVariables() |
| 53 | + const logPollingEnabled = isTruthy(env[environmentVariableNames.enableAppLogPolling]) |
| 54 | + |
| 55 | + if (!logPollingEnabled) { |
| 56 | + throw new Error( |
| 57 | + 'This command is not released yet. You can experiment with it by setting SHOPIFY_CLI_ENABLE_APP_LOG_POLLING=1 in your env.', |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + const {flags} = await this.parse(FunctionReplay) |
| 62 | + if (flags['api-key']) { |
| 63 | + await showApiKeyDeprecationWarning() |
| 64 | + } |
| 65 | + const apiKey = flags['client-id'] || flags['api-key'] |
| 66 | + |
| 67 | + await inFunctionContext({ |
| 68 | + path: flags.path, |
| 69 | + userProvidedConfigName: flags.config, |
| 70 | + callback: async (app, ourFunction) => { |
| 71 | + await replay({ |
| 72 | + app, |
| 73 | + extension: ourFunction, |
| 74 | + apiKey, |
| 75 | + stdout: flags.stdout, |
| 76 | + path: flags.path, |
| 77 | + json: flags.json, |
| 78 | + export: flags.export, |
| 79 | + }) |
| 80 | + }, |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments