Skip to content

Commit

Permalink
fix(cli): project root resolution (#5712)
Browse files Browse the repository at this point in the history
* fix(cli): project root resolution

* test(cli): test basic command can be run from Studio subdirectory
  • Loading branch information
juice49 authored and rexxars committed Feb 14, 2024
1 parent 35d368e commit e30f45f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/@sanity/cli/src/util/resolveRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export function resolveRootDir(cwd: string): string {

function hasStudioConfig(basePath: string): boolean {
const buildConfigs = [
fileExists(path.join(basePath, 'studio.config.js')),
fileExists(path.join(basePath, 'studio.config.ts')),
fileExists(path.join(basePath, 'sanity.config.js')),
fileExists(path.join(basePath, 'sanity.config.ts')),
isSanityV2StudioRoot(basePath),
]

Expand Down
11 changes: 11 additions & 0 deletions packages/@sanity/cli/test/basics.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'node:path'

import {describeCliTest, testConcurrent} from './shared/describe'
import {getCliUserEmail, runSanityCmdCommand, studioVersions} from './shared/environment'

Expand All @@ -23,6 +25,15 @@ describeCliTest('CLI: basic commands', () => {
expect(result.code).toBe(0)
})

testConcurrent('help (from subdirectory)', async () => {
const result = await runSanityCmdCommand(version, ['help'], {
cwd: (cwd) => path.join(cwd, 'components'),
})
expect(result.stdout).toContain('Not in project directory')
expect(result.stdout).toMatch(/usage:/i)
expect(result.code).toBe(0)
})

testConcurrent('projects list', async () => {
const result = await runSanityCmdCommand(version, ['projects', 'list'])
expect(result.stdout).toContain('.sanity.studio')
Expand Down
6 changes: 4 additions & 2 deletions packages/@sanity/cli/test/shared/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ export const getTestRunArgs = (version: string) => {
export function runSanityCmdCommand(
version: string,
args: string[],
options: {env?: Record<string, string | undefined>} = {},
options: {env?: Record<string, string | undefined>; cwd?: (cwd: string) => string} = {},
): Promise<{
code: number | null
stdout: string
stderr: string
}> {
const cwd = options.cwd ?? ((currentCwd) => currentCwd)

return exec(process.argv[0], [cliBinPath, ...args], {
cwd: path.join(studiosPath, version),
cwd: cwd(path.join(studiosPath, version)),
env: {...sanityEnv, ...options.env},
})
}
Expand Down

0 comments on commit e30f45f

Please sign in to comment.