From caced3b7b27fb3cfb587b89e4138d5253f181309 Mon Sep 17 00:00:00 2001 From: Ryan Bonial <4294665+ryanbonial@users.noreply.github.com> Date: Sat, 5 Oct 2024 09:11:45 -0600 Subject: [PATCH] fix(cli): do not throw during migrate if project ID is passed through flag (#7594) ### Description This PR sets the `apiClient`'s `requireProject` to `false` and instead only throws an error if both the cli config and the `--project` option are missing ### What to review Verify that the change is as expected and that the wording of the error is correct. ### Testing * create a simple migration script with sanity, `sanity migration create` and follow the prompt make sure there is no `sanity.cli.js` in your codebase Next steps: * Open /code/migrations/migration_id/index.ts in your code editor and write the code for your migration. * Dry run the migration with: `sanity migration run migration_id --project= --dataset` * Run the migration against a dataset with: `sanity migration run migration_id --project= --dataset --no-dry-run` * edit the script and run it as: `sanity migration run migration_id --project=real_project_id --dataset real_dataset` you should NOT get an error. Test again without specifying a `--project` project ID, you should see an error. ### Notes for release None --------- Co-authored-by: Espen Hovlandsdal --- .../cli/commands/migration/runMigrationCommand.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/sanity/src/_internal/cli/commands/migration/runMigrationCommand.ts b/packages/sanity/src/_internal/cli/commands/migration/runMigrationCommand.ts index ab0a33ca8c3..196087823dd 100644 --- a/packages/sanity/src/_internal/cli/commands/migration/runMigrationCommand.ts +++ b/packages/sanity/src/_internal/cli/commands/migration/runMigrationCommand.ts @@ -164,9 +164,15 @@ const runMigrationCommand: CliCommandDefinition = { const projectConfig = apiClient({ requireUser: true, - requireProject: true, + requireProject: false, }).config() + if (!project && !projectConfig.projectId) { + throw new Error( + 'sanity.cli.js does not contain a project identifier ("api.projectId") and no --project option was provided.', + ) + } + const apiConfig = { dataset: dataset ?? projectConfig.dataset!, projectId: project ?? projectConfig.projectId!,