-
Notifications
You must be signed in to change notification settings - Fork 444
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
No changes to documentation |
Component Testing Report Updated Feb 12, 2024 11:53 AM (UTC)
|
packages/sanity/src/_internal/cli/commands/migration/createMigrationCommand.ts
Show resolved
Hide resolved
@@ -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(), |
There was a problem hiding this comment.
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()
])
There was a problem hiding this comment.
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.
🤔 I thought this was already being done here: sanity/packages/@sanity/cli/src/cli.ts Lines 50 to 56 in f9c67ea
(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.
f9ad24d
to
d627991
Compare
99898dc
to
8d878ad
Compare
@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 It's probably best to fix this behaviour and scrap my other changes. |
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
, andrun
).What to review
Testing
Run the
migration
commands (create
,list
, andrun
) 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.