Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/app-config-validate-client-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/app': patch
---

Add a `--client-id` option to `app config validate`.
43 changes: 43 additions & 0 deletions packages/app/src/cli/commands/app/config/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function mockHealthyProject() {
}

describe('app config validate command', () => {
test('keeps --client-id mutually exclusive with --config', () => {
expect(Validate.flags['client-id']?.exclusive).toEqual(['config'])
})

test('calls validateApp with json: false by default', async () => {
const app = testAppLinked()
mockHealthyProject()
Expand Down Expand Up @@ -72,6 +76,45 @@ describe('app config validate command', () => {
await expectValidationMetadataCalls({cmd_app_validate_json: true})
})

test('skips active config prompts when --client-id is passed', async () => {
const app = testAppLinked()
mockHealthyProject()
vi.mocked(linkedAppContext).mockResolvedValue({app} as Awaited<ReturnType<typeof linkedAppContext>>)
vi.mocked(validateApp).mockResolvedValue()

await Validate.run(['--client-id', 'api-key'], import.meta.url)

expect(selectActiveConfig).toHaveBeenCalledWith(expect.anything(), undefined, {
clientId: 'api-key',
skipPrompts: true,
})
expect(linkedAppContext).toHaveBeenCalledWith({
directory: expect.any(String),
clientId: 'api-key',
forceRelink: false,
userProvidedConfigName: undefined,
unsafeTolerateErrors: true,
})
expect(validateApp).toHaveBeenCalledWith(app, {json: false})
await expectValidationMetadataCalls({cmd_app_validate_json: false})
})

test('keeps active config prompts enabled when --client-id is not passed', async () => {
const app = testAppLinked()
mockHealthyProject()
vi.mocked(linkedAppContext).mockResolvedValue({app} as Awaited<ReturnType<typeof linkedAppContext>>)
vi.mocked(validateApp).mockResolvedValue()

await Validate.run([], import.meta.url)

expect(selectActiveConfig).toHaveBeenCalledWith(expect.anything(), undefined, {
clientId: undefined,
skipPrompts: false,
})
expect(validateApp).toHaveBeenCalledWith(app, {json: false})
await expectValidationMetadataCalls({cmd_app_validate_json: false})
})

test('outputs JSON issues when active config has TOML parse errors', async () => {
vi.mocked(Project.load).mockResolvedValue({errors: []} as unknown as Project)
vi.mocked(selectActiveConfig).mockResolvedValue({file: new TomlFile('shopify.app.toml', {})} as any)
Expand Down
5 changes: 4 additions & 1 deletion packages/app/src/cli/commands/app/config/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export default class Validate extends AppLinkedCommand {
// Stage 2: Select active config and check for TOML parse errors scoped to it
let activeConfig
try {
activeConfig = await selectActiveConfig(project, flags.config)
activeConfig = await selectActiveConfig(project, flags.config, {
clientId: flags['client-id'],
skipPrompts: Boolean(flags['client-id']),
})
} catch (err) {
if (err instanceof AbortError && flags.json) {
await recordValidationFailure(1, 1)
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/cli/models/app/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ type ConfigurationLoaderResult<
export async function getAppConfigurationContext(
workingDirectory: string,
userProvidedConfigName?: string,
options?: {skipPrompts?: boolean},
options?: {clientId?: string; skipPrompts?: boolean},
): Promise<{project: Project; activeConfig: ActiveConfig}> {
const project = await Project.load(workingDirectory)
const activeConfig = await selectActiveConfig(project, userProvidedConfigName, options)
Expand Down
22 changes: 22 additions & 0 deletions packages/app/src/cli/models/project/active-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ describe('selectActiveConfig', () => {
})
})

test('selects config by client ID when cached config is stale and prompts are skipped', async () => {
await inTemporaryDirectory(async (dir) => {
await writeFile(joinPath(dir, 'shopify.app.2.toml'), 'client_id = "matching-client-id"')
await writeFile(joinPath(dir, 'shopify.app.3.toml'), 'client_id = "other-client-id"')
const project = await Project.load(dir)

vi.mocked(getCachedAppInfo).mockReturnValue({
directory: dir,
configFile: 'shopify.app.toml',
})

const config = await selectActiveConfig(project, undefined, {
clientId: 'matching-client-id',
skipPrompts: true,
})

expect(basename(config.file.path)).toBe('shopify.app.2.toml')
expect(config.file.content.client_id).toBe('matching-client-id')
expect(config.source).toBe('flag')
})
})

test('falls back to default shopify.app.toml when no flag or cache', async () => {
await inTemporaryDirectory(async (dir) => {
await writeFile(joinPath(dir, 'shopify.app.toml'), 'client_id = "default-id"')
Expand Down
13 changes: 9 additions & 4 deletions packages/app/src/cli/models/project/active-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ export interface ActiveConfig {
export async function selectActiveConfig(
project: Project,
userProvidedConfigName?: string,
options?: {skipPrompts?: boolean},
options?: {clientId?: string; skipPrompts?: boolean},
): Promise<ActiveConfig> {
let configName = userProvidedConfigName
const clientIdConfig = options?.clientId ? project.appConfigByClientId(options.clientId) : undefined

// Check cache for previously selected config
const cachedConfigName = getCachedAppInfo(project.directory)?.configFile
Expand All @@ -76,21 +77,25 @@ export async function selectActiveConfig(
configName = await use({directory: project.directory, warningContent, shouldRenderSuccess: false})
}

if (!configName && clientIdConfig) {
return buildActiveConfig(project, clientIdConfig, 'flag')
}

// Don't fall back to stale cached name — it points to a non-existent file
configName = configName ?? (cacheIsStale ? undefined : cachedConfigName)
const resolvedConfigName = configName ?? (cacheIsStale ? undefined : cachedConfigName)

// Determine source after resolution so it reflects the actual selection path
let source: ConfigSource
if (userProvidedConfigName) {
source = 'flag'
} else if (configName) {
} else if (resolvedConfigName) {
source = 'cached'
} else {
source = 'default'
}

// Resolve the config file name and look it up in the project's pre-loaded files
const configurationFileName = getAppConfigurationFileName(configName)
const configurationFileName = getAppConfigurationFileName(resolvedConfigName)
const file = project.appConfigByName(configurationFileName)
if (!file) {
throw new AbortError(
Expand Down
5 changes: 4 additions & 1 deletion packages/app/src/cli/services/app-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export async function linkedAppContext({
project = reloaded.project
activeConfig = reloaded.activeConfig
} else {
const loaded = await getAppConfigurationContext(directory, userProvidedConfigName)
const loaded = await getAppConfigurationContext(directory, userProvidedConfigName, {
clientId,
skipPrompts: Boolean(clientId),
})
project = loaded.project
activeConfig = loaded.activeConfig

Expand Down
Loading