Skip to content

Commit c83021e

Browse files
authored
feat(cli): respect --no-auto-updates flag in CLI config (#7396)
* feat: respect no-auto-updates flag in cli config * test: add unit tests
1 parent f53cd0e commit c83021e

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

packages/sanity/src/_internal/cli/actions/build/buildAction.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {BuildTrace} from './build.telemetry'
1616
import {buildVendorDependencies} from '../../server/buildVendorDependencies'
1717
import {compareStudioDependencyVersions} from '../../util/compareStudioDependencyVersions'
1818
import {getAutoUpdateImportMap} from '../../util/getAutoUpdatesImportMap'
19+
import {shouldAutoUpdate} from '../../util/shouldAutoUpdate'
1920

2021
const rimraf = promisify(rimrafCallback)
2122

@@ -58,9 +59,7 @@ export default async function buildSanityStudio(
5859
return {didCompile: false}
5960
}
6061

61-
const autoUpdatesEnabled =
62-
flags['auto-updates'] ||
63-
(cliConfig && 'autoUpdates' in cliConfig && cliConfig.autoUpdates === true)
62+
const autoUpdatesEnabled = shouldAutoUpdate({flags, cliConfig})
6463

6564
// Get the version without any tags if any
6665
const coercedSanityVersion = semver.coerce(installedSanityVersion)?.version

packages/sanity/src/_internal/cli/actions/deploy/deployAction.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import zlib from 'node:zlib'
55
import {type CliCommandArguments, type CliCommandContext} from '@sanity/cli'
66
import tar from 'tar-fs'
77

8+
import {shouldAutoUpdate} from '../../util/shouldAutoUpdate'
89
import buildSanityStudio, {type BuildSanityStudioCommandFlags} from '../build/buildAction'
910
import {
1011
checkDir,
@@ -29,10 +30,8 @@ export default async function deployStudioAction(
2930
const flags = {build: true, ...args.extOptions}
3031
const customSourceDir = args.argsWithoutOptions[0]
3132
const sourceDir = path.resolve(process.cwd(), customSourceDir || path.join(workDir, 'dist'))
32-
const isAutoUpdating =
33-
flags['auto-updates'] ||
34-
(cliConfig && 'autoUpdates' in cliConfig && cliConfig.autoUpdates === true) ||
35-
false
33+
const isAutoUpdating = shouldAutoUpdate({flags, cliConfig})
34+
3635
const installedSanityVersion = await getInstalledSanityVersion()
3736
const configStudioHost = cliConfig && 'studioHost' in cliConfig && cliConfig.studioHost
3837

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {describe, expect, it} from '@jest/globals'
2+
import {type CliConfig} from '@sanity/cli'
3+
4+
import {type BuildSanityStudioCommandFlags} from '../../actions/build/buildAction'
5+
import {shouldAutoUpdate} from '../shouldAutoUpdate'
6+
7+
describe('shouldAutoUpdate', () => {
8+
it('should return true when flags["auto-updates"] is true', () => {
9+
const flags: BuildSanityStudioCommandFlags = {'auto-updates': true}
10+
expect(shouldAutoUpdate({flags})).toBe(true)
11+
})
12+
13+
it('should return false when flags["auto-updates"] is false', () => {
14+
const flags: BuildSanityStudioCommandFlags = {'auto-updates': false}
15+
expect(shouldAutoUpdate({flags})).toBe(false)
16+
})
17+
18+
it('should return true when cliConfig.autoUpdates is true and flags["auto-updates"] is not set', () => {
19+
const flags: BuildSanityStudioCommandFlags = {}
20+
const cliConfig: CliConfig = {autoUpdates: true}
21+
expect(shouldAutoUpdate({flags, cliConfig})).toBe(true)
22+
})
23+
24+
it('should return false when cliConfig.autoUpdates is false and flags["auto-updates"] is not set', () => {
25+
const flags: BuildSanityStudioCommandFlags = {}
26+
const cliConfig: CliConfig = {autoUpdates: false}
27+
expect(shouldAutoUpdate({flags, cliConfig})).toBe(false)
28+
})
29+
30+
it('should return false when both flags["auto-updates"] and cliConfig.autoUpdates are not set', () => {
31+
const flags: BuildSanityStudioCommandFlags = {}
32+
expect(shouldAutoUpdate({flags})).toBe(false)
33+
})
34+
35+
it('should prioritize flags over cliConfig when both are set', () => {
36+
const flags: BuildSanityStudioCommandFlags = {'auto-updates': false}
37+
const cliConfig: CliConfig = {autoUpdates: true}
38+
expect(shouldAutoUpdate({flags, cliConfig})).toBe(false)
39+
})
40+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {type CliConfig} from '@sanity/cli'
2+
3+
import {type BuildSanityStudioCommandFlags} from '../actions/build/buildAction'
4+
5+
interface AutoUpdateSources {
6+
flags: BuildSanityStudioCommandFlags
7+
cliConfig?: CliConfig
8+
}
9+
10+
/**
11+
* Compares parameters from various sources to determine whether or not to auto-update
12+
* @param sources - The sources of the auto-update parameter, including CLI flags and the CLI config
13+
* @returns boolean
14+
* @internal
15+
*/
16+
export function shouldAutoUpdate({flags, cliConfig}: AutoUpdateSources): boolean {
17+
// cli flags (for example, '--no-auto-updates') should take precedence
18+
if ('auto-updates' in flags) {
19+
return Boolean(flags['auto-updates'])
20+
}
21+
22+
if (cliConfig && 'autoUpdates' in cliConfig) {
23+
return Boolean(cliConfig.autoUpdates)
24+
}
25+
26+
return false
27+
}

0 commit comments

Comments
 (0)