Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): ensure CWD is project root when running migration commands #5684

Closed
wants to merge 9 commits into from

Conversation

juice49
Copy link
Contributor

@juice49 juice49 commented Feb 6, 2024

Description

This branch adds an ensureCwdIsProjectRoot function that can be used by CLI commands to improve UX when the Sanity CLI is mistakenly used outside of a Studio project root.

If no CLI configuration can be resolved, the CLI will now search parent directories for any Studio or CLI configuration file. If It finds one, it will infer that it is the project root directory and print a notice that the user should move there and try the command again.

This change doesn't affect the way the CLI configuration is resolved; it's only used to print a notice.

I've initially added this to the migration commands (create, list, and run).

Screenshot 2024-02-07 at 14 05 41

What to review

  • That the output printed makes sense.
  • That the change doesn't introduce unnecessary overhead (it shouldn't; we only search for config files if one could not already be resolved in the CWD).

Testing

Run the migration commands (create, list, and run) from a subdirectory of a Studio project. For example, the test Studio migrations directory (dev/test-studio/migrations). The CLI can be invoked here by running ../../.bin/sanity migration list etc.

Copy link

vercel bot commented Feb 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
performance-studio ✅ Ready (Inspect) Visit Preview Feb 12, 2024 11:51am
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2024 11:51am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Feb 12, 2024 11:51am

Copy link
Contributor

github-actions bot commented Feb 6, 2024

No changes to documentation

Copy link
Contributor

github-actions bot commented Feb 6, 2024

Component Testing Report Updated Feb 12, 2024 11:53 AM (UTC)

File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 33s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 6s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 12s 3 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 13s 4 2 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 33s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 18s 9 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 1s 18 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 13s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 19s 9 0 0

@@ -97,6 +98,7 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}
cliRoot: cliRoot,
workDir: workDir,
corePath: await getCoreModulePath(workDir, cliConfig),
projectRootPath: cliConfig ? workDir : await getProjectRoot(),
Copy link
Member

@bjoerge bjoerge Feb 7, 2024

Choose a reason for hiding this comment

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

a potential (although probably quite small) perf hit by awaiting these in sequence, so you could consider moving them up and do something like:

const [corePath, projectRootPath] = await Promise.all([
  getCoreModulePath(workDir, cliConfig),
  getProjectRoot()
])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha! Good catch. I'll see if this can be easily refactored.

@rexxars
Copy link
Member

rexxars commented Feb 8, 2024

🤔 I thought this was already being done here:

let workDir: string | undefined
try {
workDir = isInit ? process.cwd() : resolveRootDir(cwd)
} catch (err) {
console.error(chalk.red(err.message))
process.exit(1)
}

(Although it doesn't warn you)

If a CLI config is found in the working directory, the working directory will be provided as the
`projectRootPath`. Otherwise, walk up parent directories until the first Studio or CLI config file
is found, indicating the project root path has been found, and providing this as the `projectRootPath`.
When executing a command in a subdirectory of a Studio project, a warning will be printed
suggesting the user moves to the root Studio project directory.
@juice49 juice49 force-pushed the feat/sdx-999-migration-cli-cwd-warning branch from 99898dc to 8d878ad Compare February 12, 2024 11:40
@juice49
Copy link
Contributor Author

juice49 commented Feb 12, 2024

@rexxars I stumbled upon this code during my work, and was a little confused because it doesn't seem to do anything. From the looks of the code, the CLI should infer the project root automatically. It should also warn the user this inference has occurred.

In my testing, neither of these things happened. Only now have I realised why: the project root resolution code looks for studio.config.{ts,js} files, rather than sanity.config.{ts,js}. Therefore, it never finds the project root.

It's probably best to fix this behaviour and scrap my other changes.

@juice49 juice49 marked this pull request as draft February 12, 2024 12:04
@juice49 juice49 closed this Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants