Skip to content

Commit

Permalink
Merge pull request #4198 from Shopify/ah.allow-function-commands-outs…
Browse files Browse the repository at this point in the history
…ide-function-dir

Show prompt when running function command outside of function directory
  • Loading branch information
andrewhassan committed Jul 16, 2024
2 parents 336eb3e + 6d91161 commit 2d237c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
25 changes: 23 additions & 2 deletions packages/app/src/cli/services/function/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {testApp, testFunctionExtension} from '../../models/app/app.test-data.js'
import {AppInterface} from '../../models/app/app.js'
import {ExtensionInstance} from '../../models/extensions/extension-instance.js'
import {describe, vi, expect, beforeEach, test} from 'vitest'
import {renderFatalError} from '@shopify/cli-kit/node/ui'
import {renderAutocompletePrompt, renderFatalError} from '@shopify/cli-kit/node/ui'
import {joinPath} from '@shopify/cli-kit/node/path'
import {isTerminalInteractive} from '@shopify/cli-kit/node/context/local'

vi.mock('../../models/app/loader.js')
vi.mock('@shopify/cli-kit/node/ui')
vi.mock('@shopify/cli-kit/node/context/local')

let app: AppInterface
let ourFunction: ExtensionInstance
Expand All @@ -18,6 +20,8 @@ beforeEach(async () => {
app = testApp({allExtensions: [ourFunction]})
vi.mocked(loadApp).mockResolvedValue(app)
vi.mocked(renderFatalError).mockReturnValue('')
vi.mocked(renderAutocompletePrompt).mockResolvedValue(ourFunction)
vi.mocked(isTerminalInteractive).mockReturnValue(true)
})

describe('ensure we are within a function context', () => {
Expand All @@ -38,9 +42,26 @@ describe('ensure we are within a function context', () => {
expect(renderFatalError).not.toHaveBeenCalled()
})

test('displays an error when we are not inside a function directory', async () => {
test('displays function prompt when we are not inside a function directory', async () => {
// Given
const callback = vi.fn()

// When
await inFunctionContext({
path: 'random/dir',
callback,
})

// Then
expect(callback).toHaveBeenCalledOnce()
expect(renderAutocompletePrompt).toHaveBeenCalledOnce()
expect(renderFatalError).not.toHaveBeenCalled()
})

test('displays an error when terminal is not interactive and we are not inside a function directory', async () => {
// Given
let ranCallback = false
vi.mocked(isTerminalInteractive).mockReturnValue(false)

// When
await expect(
Expand Down
16 changes: 14 additions & 2 deletions packages/app/src/cli/services/function/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {FunctionConfigType} from '../../models/extensions/specifications/functio
import {resolvePath, cwd} from '@shopify/cli-kit/node/path'
import {AbortError} from '@shopify/cli-kit/node/error'
import {Flags} from '@oclif/core'
import {isTerminalInteractive} from '@shopify/cli-kit/node/context/local'
import {renderAutocompletePrompt} from '@shopify/cli-kit/node/ui'

export const functionFlags = {
path: Flags.string({
Expand All @@ -30,10 +32,20 @@ export async function inFunctionContext({
const specifications = await loadLocalExtensionsSpecifications()
const app: AppInterface = await loadApp({specifications, directory: path, userProvidedConfigName})

const allFunctions = app.allExtensions.filter((ext) => ext.isFunctionExtension)
const ourFunction = allFunctions.find((fun) => fun.directory === path) as ExtensionInstance<FunctionConfigType>
const allFunctions = app.allExtensions.filter(
(ext) => ext.isFunctionExtension,
) as ExtensionInstance<FunctionConfigType>[]
const ourFunction = allFunctions.find((fun) => fun.directory === path)

if (ourFunction) {
return callback(app, ourFunction)
} else if (isTerminalInteractive()) {
const selectedFunction = await renderAutocompletePrompt({
message: 'Which function?',
choices: allFunctions.map((shopifyFunction) => ({label: shopifyFunction.handle, value: shopifyFunction})),
})

return callback(app, selectedFunction)
} else {
throw new AbortError('Run this command from a function directory or use `--path` to specify a function directory.')
}
Expand Down

0 comments on commit 2d237c4

Please sign in to comment.