Skip to content
Merged
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/json-implies-unattended.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sanity/cli-core": minor
---

feat(cli): treat `--json` as unattended mode, so a command emitting machine-readable output never stops at a prompt a caller can't answer
5 changes: 3 additions & 2 deletions packages/@sanity/cli-core/src/SanityCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ export abstract class SanityCommand<T extends typeof Command>
*
* Most commands should take an explicit `--yes` flag to enable unattended mode, but
* some commands may also be run in unattended mode if `process.stdin` is not a TTY
* (eg when running in a CI environment).
* (eg when running in a CI environment), or when `--json` asks for machine-readable
* output (a caller parsing JSON can't answer a prompt).
*/
public isUnattended(): boolean {
return this.flags.yes || !this.resolveIsInteractive()
return this.flags.yes || this.flags.json || !this.resolveIsInteractive()
Comment thread
cursor[bot] marked this conversation as resolved.
}

/**
Expand Down
22 changes: 5 additions & 17 deletions packages/@sanity/cli/src/commands/tokens/__tests__/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,6 @@ describe('#tokens:add', () => {
})

test('outputs JSON when --json flag is used', async () => {
const mockRoles = [
{
appliesToRobots: true,
appliesToUsers: true,
description: 'Can read documents',
isCustom: false,
name: 'viewer',
projectId: 'test-project',
title: 'Viewer',
},
]

const mockToken = {
id: 'token-json',
key: 'sk_test_json1234',
Expand All @@ -177,11 +165,7 @@ describe('#tokens:add', () => {
],
}

mockApi({
apiVersion: TOKENS_API_VERSION,
uri: '/projects/test-project/roles',
}).reply(200, mockRoles)

// --json is unattended, so the role defaults to viewer without a roles prompt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we assert that select/input is not called as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍🏼 Added in 41138e8

mockApi({
apiVersion: TOKENS_API_VERSION,
method: 'post',
Expand All @@ -194,6 +178,8 @@ describe('#tokens:add', () => {

const parsedOutput = JSON.parse(stdout)
expect(parsedOutput).toEqual(mockToken)
expect(mockedSelect).not.toHaveBeenCalled()
expect(mockedInput).not.toHaveBeenCalled()
})

test('works in unattended mode with --yes flag', async () => {
Expand Down Expand Up @@ -223,6 +209,8 @@ describe('#tokens:add', () => {

expect(stdout).toContain('Token created successfully!')
expect(stdout).toContain('Label: Unattended Token')
expect(mockedSelect).not.toHaveBeenCalled()
expect(mockedInput).not.toHaveBeenCalled()
})

test('handles invalid role error', async () => {
Expand Down
Loading