Skip to content

Commit 17c02f9

Browse files
cngonzalezbinoy14runebrexxars
authored
feat(cli): use auto-updates flag in init (#7401)
* feat(cli): enable autoUpdates by default on new projects * feat(cli): add auto updates flag in init and use in template * refactor: add back early returns * Update packages/@sanity/cli/src/commands/init/initCommand.ts * refactor: replace boolean expressions with literals * refactor: change help text to include both enable/disable flags --------- Co-authored-by: Binoy Patel <[email protected]> Co-authored-by: Rune Botten <[email protected]> Co-authored-by: Espen Hovlandsdal <[email protected]>
1 parent 8b0eeff commit 17c02f9

File tree

8 files changed

+146
-8
lines changed

8 files changed

+146
-8
lines changed

packages/@sanity/cli/src/actions/init-project/bootstrapTemplate.ts

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export async function bootstrapTemplate(
127127
const cliConfig = await createCliConfig({
128128
projectId: variables.projectId,
129129
dataset: variables.dataset,
130+
autoUpdates: variables.autoUpdates,
130131
})
131132

132133
// Write non-template files to disc

packages/@sanity/cli/src/actions/init-project/createCliConfig.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,62 @@ export default defineCliConfig({
99
api: {
1010
projectId: '%projectId%',
1111
dataset: '%dataset%'
12-
}
12+
},
13+
/**
14+
* Enable auto-updates for studios.
15+
* Learn more at https://www.sanity.io/docs/cli#auto-updates
16+
*/
17+
autoUpdates: __BOOL__autoUpdates__,
1318
})
1419
`
1520

1621
export interface GenerateCliConfigOptions {
1722
projectId: string
1823
dataset: string
24+
autoUpdates: boolean
1925
}
2026

2127
export function createCliConfig(options: GenerateCliConfigOptions): string {
2228
const variables = options
2329
const template = defaultTemplate.trimStart()
2430
const ast = parse(template, {parser})
31+
2532
traverse(ast, {
2633
StringLiteral: {
2734
enter({node}) {
2835
const value = node.value
2936
if (!value.startsWith('%') || !value.endsWith('%')) {
3037
return
3138
}
32-
3339
const variableName = value.slice(1, -1) as keyof GenerateCliConfigOptions
3440
if (!(variableName in variables)) {
3541
throw new Error(`Template variable '${value}' not defined`)
3642
}
37-
38-
node.value = variables[variableName] || ''
43+
const newValue = variables[variableName]
44+
/*
45+
* although there are valid non-strings in our config,
46+
* they're not in StringLiteral nodes, so assume undefined
47+
*/
48+
node.value = typeof newValue === 'string' ? newValue : ''
49+
},
50+
},
51+
Identifier: {
52+
enter(path) {
53+
if (!path.node.name.startsWith('__BOOL__')) {
54+
return
55+
}
56+
const variableName = path.node.name.replace(
57+
/^__BOOL__(.+?)__$/,
58+
'$1',
59+
) as keyof GenerateCliConfigOptions
60+
if (!(variableName in variables)) {
61+
throw new Error(`Template variable '${variableName}' not defined`)
62+
}
63+
const value = variables[variableName]
64+
if (typeof value !== 'boolean') {
65+
throw new Error(`Expected boolean value for '${variableName}'`)
66+
}
67+
path.replaceWith({type: 'BooleanLiteral', value})
3968
},
4069
},
4170
})

packages/@sanity/cli/src/actions/init-project/createStudioConfig.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface GenerateConfigOptions {
3434
variables: {
3535
projectId: string
3636
dataset: string
37+
autoUpdates: boolean
3738
projectName?: string
3839
sourceName?: string
3940
sourceTitle?: string
@@ -60,8 +61,12 @@ export function createStudioConfig(options: GenerateConfigOptions): string {
6061
if (!(variableName in variables)) {
6162
throw new Error(`Template variable '${value}' not defined`)
6263
}
63-
64-
node.value = variables[variableName] || ''
64+
const newValue = variables[variableName]
65+
/*
66+
* although there are valid non-strings in our config,
67+
* they're not in this template, so assume undefined
68+
*/
69+
node.value = typeof newValue === 'string' ? newValue : ''
6570
},
6671
},
6772
})

packages/@sanity/cli/src/actions/init-project/initProject.ts

+7
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ export default async function initSanity(
567567
trace.log({step: 'useTypeScript', selectedOption: useTypeScript ? 'yes' : 'no'})
568568
}
569569

570+
// we enable auto-updates by default, but allow users to specify otherwise
571+
let autoUpdates = true
572+
if (typeof cliFlags['auto-updates'] === 'boolean') {
573+
autoUpdates = cliFlags['auto-updates']
574+
}
575+
570576
// Build a full set of resolved options
571577
const templateOptions: BootstrapOptions = {
572578
outputPath,
@@ -575,6 +581,7 @@ export default async function initSanity(
575581
schemaUrl,
576582
useTypeScript,
577583
variables: {
584+
autoUpdates,
578585
dataset: datasetName,
579586
projectId,
580587
projectName: displayName || answers.projectName,

packages/@sanity/cli/src/commands/init/initCommand.ts

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Options
2727
--coupon <name> Optionally select a coupon for a new project (cannot be used with --project-plan)
2828
--no-typescript Do not use TypeScript for template files
2929
--package-manager <name> Specify which package manager to use [allowed: ${allowedPackageManagersString}]
30+
--no-auto-updates Disable auto updates of studio versions
3031
3132
Examples
3233
# Initialize a new project, prompt for required information along the way
@@ -60,6 +61,7 @@ export interface InitFlags {
6061

6162
'visibility'?: string
6263
'typescript'?: boolean
64+
'auto-updates'?: boolean
6365
/**
6466
* Used for initializing a project from a server schema that is saved in the Journey API
6567
* Overrides `project` option.
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import fs from 'node:fs/promises'
2+
import path from 'node:path'
3+
4+
import {describe, expect} from '@jest/globals'
5+
6+
import templates from '../src/actions/init-project/templates'
7+
import {describeCliTest, testConcurrent} from './shared/describe'
8+
import {baseTestPath, cliProjectId, getTestRunArgs, runSanityCmdCommand} from './shared/environment'
9+
10+
describeCliTest('CLI: `sanity init v3`', () => {
11+
describe.each(Object.keys(templates))('for template %s', (template) => {
12+
testConcurrent('adds autoUpdates: true to cli config', async () => {
13+
const version = 'v3'
14+
const testRunArgs = getTestRunArgs(version)
15+
const outpath = `test-template-${template}-${version}`
16+
17+
await runSanityCmdCommand(version, [
18+
'init',
19+
'--y',
20+
'--project',
21+
cliProjectId,
22+
'--dataset',
23+
testRunArgs.dataset,
24+
'--template',
25+
template,
26+
'--output-path',
27+
`${baseTestPath}/${outpath}`,
28+
'--package-manager',
29+
'manual',
30+
])
31+
32+
const cliConfig = await fs.readFile(
33+
path.join(baseTestPath, outpath, 'sanity.cli.ts'),
34+
'utf-8',
35+
)
36+
37+
expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
38+
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
39+
expect(cliConfig).toContain(`autoUpdates: true`)
40+
})
41+
})
42+
43+
testConcurrent('adds autoUpdates: true to cli config for javascript projects', async () => {
44+
const version = 'v3'
45+
const testRunArgs = getTestRunArgs(version)
46+
const outpath = `test-template-${version}`
47+
48+
await runSanityCmdCommand(version, [
49+
'init',
50+
'--y',
51+
'--project',
52+
cliProjectId,
53+
'--dataset',
54+
testRunArgs.dataset,
55+
'--output-path',
56+
`${baseTestPath}/${outpath}`,
57+
'--package-manager',
58+
'manual',
59+
'--no-typescript',
60+
])
61+
62+
const cliConfig = await fs.readFile(path.join(baseTestPath, outpath, 'sanity.cli.js'), 'utf-8')
63+
64+
expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
65+
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
66+
expect(cliConfig).toContain(`autoUpdates: true`)
67+
})
68+
69+
testConcurrent('adds autoUpdates: false to cli config if flag provided', async () => {
70+
const version = 'v3'
71+
const testRunArgs = getTestRunArgs(version)
72+
const outpath = `test-template-${version}`
73+
74+
await runSanityCmdCommand(version, [
75+
'init',
76+
'--y',
77+
'--project',
78+
cliProjectId,
79+
'--dataset',
80+
testRunArgs.dataset,
81+
'--output-path',
82+
`${baseTestPath}/${outpath}`,
83+
'--package-manager',
84+
'manual',
85+
'--no-auto-updates',
86+
])
87+
88+
const cliConfig = await fs.readFile(path.join(baseTestPath, outpath, 'sanity.cli.ts'), 'utf-8')
89+
90+
expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
91+
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
92+
expect(cliConfig).toContain(`autoUpdates: false`)
93+
})
94+
})

packages/sanity/src/_internal/cli/commands/build/buildCommand.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {BuildSanityStudioCommandFlags} from '../../actions/build/buildAction'
44
const helpText = `
55
Options
66
--source-maps Enable source maps for built bundles (increases size of bundle)
7-
--auto-updates Enable auto updates of studio versions
7+
--auto-updates / --no-auto-updates Enable/disable auto updates of studio versions
88
--no-minify Skip minifying built JavaScript (speeds up build, increases size of bundle)
99
-y, --yes Unattended mode, answers "yes" to any "yes/no" prompt and otherwise uses defaults
1010

packages/sanity/src/_internal/cli/commands/deploy/deployCommand.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {type DeployStudioActionFlags} from '../../actions/deploy/deployAction'
99
const helpText = `
1010
Options
1111
--source-maps Enable source maps for built bundles (increases size of bundle)
12-
--auto-updates Enable auto updates of studio versions
12+
--auto-updates / --no-auto-updates Enable/disable auto updates of studio versions
1313
--no-minify Skip minifying built JavaScript (speeds up build, increases size of bundle)
1414
--no-build Don't build the studio prior to deploy, instead deploying the version currently in \`dist/\`
1515

0 commit comments

Comments
 (0)