Skip to content

Commit bdf3c4b

Browse files
annezclaude
andcommitted
fix(cli): support --yes flag for unattended dataset import
When using `sanity dataset import` with a non-existing target dataset, the command previously always prompted for confirmation to create it, even when the --yes flag was provided. This change: - Adds -y/--yes flags to the import command's flag interface - Auto-confirms dataset creation when --yes is provided - Throws an error if --yes is used without specifying a target dataset - Documents the flag in the help text Fixes SAPP-3387 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent e85a815 commit bdf3c4b

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

packages/@sanity/cli/test/exportImport.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,19 @@ describeCliTest('CLI: `sanity dataset export` / `import`', () => {
8080
expect(result.stdout).toMatch(/done!/i)
8181
expect(result.code).toBe(0)
8282
})
83+
84+
testConcurrent('import with --yes creates non-existing dataset', async () => {
85+
const newDataset = `${testRunArgs.dataset}-import-yes`
86+
const result = await runSanityCmdCommand(studioName, [
87+
'dataset',
88+
'import',
89+
testRunArgs.importTarballPath,
90+
newDataset,
91+
'--missing',
92+
'--yes',
93+
])
94+
expect(result.stdout).toMatch(/done!/i)
95+
expect(result.code).toBe(0)
96+
})
8397
})
8498
})

packages/sanity/src/_internal/cli/commands/dataset/importDatasetCommand.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Options
2222
--allow-failing-assets Skip assets that cannot be fetched/uploaded
2323
--replace-assets Skip reuse of existing assets
2424
--skip-cross-dataset-references Skips references to other datasets
25+
-y, --yes Unattended mode, auto-confirm dataset creation prompts
2526
2627
Rarely used options (should generally not be used)
2728
--allow-assets-in-different-dataset Allow asset documents to reference different project/dataset
@@ -53,6 +54,8 @@ interface ImportFlags {
5354
'allow-system-documents'?: boolean
5455
'replace'?: boolean
5556
'missing'?: boolean
57+
'yes'?: boolean
58+
'y'?: boolean
5659
}
5760

5861
interface ParsedImportFlags {
@@ -136,7 +139,8 @@ const importDatasetCommand: CliCommandDefinition = {
136139
)
137140
}
138141

139-
const targetDataset = await determineTargetDataset(target, context)
142+
const unattended = Boolean(args.extOptions.yes || args.extOptions.y)
143+
const targetDataset = await determineTargetDataset(target, context, unattended)
140144
debug(`Target dataset has been set to "${targetDataset}"`)
141145

142146
const isUrl = /^https?:\/\//i.test(file)
@@ -304,7 +308,11 @@ const importDatasetCommand: CliCommandDefinition = {
304308
},
305309
}
306310

307-
async function determineTargetDataset(target: string, context: CliCommandContext) {
311+
async function determineTargetDataset(
312+
target: string,
313+
context: CliCommandContext,
314+
unattended: boolean,
315+
) {
308316
const {apiClient, output, prompt} = context
309317
const client = apiClient()
310318

@@ -322,17 +330,23 @@ async function determineTargetDataset(target: string, context: CliCommandContext
322330

323331
let targetDataset = target ? `${target}` : null
324332
if (!targetDataset) {
333+
if (unattended) {
334+
throw new Error('Target dataset must be specified when using --yes flag')
335+
}
325336
targetDataset = await chooseDatasetPrompt(context, {
326337
message: 'Select target dataset',
327338
allowCreation: true,
328339
})
329340
} else if (!datasets.find((dataset) => dataset.name === targetDataset)) {
330341
debug('Target dataset does not exist, prompting for creation')
331-
const shouldCreate = await prompt.single({
332-
type: 'confirm',
333-
message: `Dataset "${targetDataset}" does not exist, would you like to create it?`,
334-
default: true,
335-
})
342+
let shouldCreate = unattended
343+
if (!unattended) {
344+
shouldCreate = await prompt.single({
345+
type: 'confirm',
346+
message: `Dataset "${targetDataset}" does not exist, would you like to create it?`,
347+
default: true,
348+
})
349+
}
336350

337351
if (!shouldCreate) {
338352
throw new Error(`Dataset "${targetDataset}" does not exist`)

0 commit comments

Comments
 (0)