Skip to content

Commit

Permalink
feat(cli): use auto-updates flag in init (#7401)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people authored Sep 12, 2024
1 parent 8b0eeff commit 17c02f9
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export async function bootstrapTemplate(
const cliConfig = await createCliConfig({
projectId: variables.projectId,
dataset: variables.dataset,
autoUpdates: variables.autoUpdates,
})

// Write non-template files to disc
Expand Down
37 changes: 33 additions & 4 deletions packages/@sanity/cli/src/actions/init-project/createCliConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,62 @@ export default defineCliConfig({
api: {
projectId: '%projectId%',
dataset: '%dataset%'
}
},
/**
* Enable auto-updates for studios.
* Learn more at https://www.sanity.io/docs/cli#auto-updates
*/
autoUpdates: __BOOL__autoUpdates__,
})
`

export interface GenerateCliConfigOptions {
projectId: string
dataset: string
autoUpdates: boolean
}

export function createCliConfig(options: GenerateCliConfigOptions): string {
const variables = options
const template = defaultTemplate.trimStart()
const ast = parse(template, {parser})

traverse(ast, {
StringLiteral: {
enter({node}) {
const value = node.value
if (!value.startsWith('%') || !value.endsWith('%')) {
return
}

const variableName = value.slice(1, -1) as keyof GenerateCliConfigOptions
if (!(variableName in variables)) {
throw new Error(`Template variable '${value}' not defined`)
}

node.value = variables[variableName] || ''
const newValue = variables[variableName]
/*
* although there are valid non-strings in our config,
* they're not in StringLiteral nodes, so assume undefined
*/
node.value = typeof newValue === 'string' ? newValue : ''
},
},
Identifier: {
enter(path) {
if (!path.node.name.startsWith('__BOOL__')) {
return
}
const variableName = path.node.name.replace(
/^__BOOL__(.+?)__$/,
'$1',
) as keyof GenerateCliConfigOptions
if (!(variableName in variables)) {
throw new Error(`Template variable '${variableName}' not defined`)
}
const value = variables[variableName]
if (typeof value !== 'boolean') {
throw new Error(`Expected boolean value for '${variableName}'`)
}
path.replaceWith({type: 'BooleanLiteral', value})
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface GenerateConfigOptions {
variables: {
projectId: string
dataset: string
autoUpdates: boolean
projectName?: string
sourceName?: string
sourceTitle?: string
Expand All @@ -60,8 +61,12 @@ export function createStudioConfig(options: GenerateConfigOptions): string {
if (!(variableName in variables)) {
throw new Error(`Template variable '${value}' not defined`)
}

node.value = variables[variableName] || ''
const newValue = variables[variableName]
/*
* although there are valid non-strings in our config,
* they're not in this template, so assume undefined
*/
node.value = typeof newValue === 'string' ? newValue : ''
},
},
})
Expand Down
7 changes: 7 additions & 0 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ export default async function initSanity(
trace.log({step: 'useTypeScript', selectedOption: useTypeScript ? 'yes' : 'no'})
}

// we enable auto-updates by default, but allow users to specify otherwise
let autoUpdates = true
if (typeof cliFlags['auto-updates'] === 'boolean') {
autoUpdates = cliFlags['auto-updates']
}

// Build a full set of resolved options
const templateOptions: BootstrapOptions = {
outputPath,
Expand All @@ -575,6 +581,7 @@ export default async function initSanity(
schemaUrl,
useTypeScript,
variables: {
autoUpdates,
dataset: datasetName,
projectId,
projectName: displayName || answers.projectName,
Expand Down
2 changes: 2 additions & 0 deletions packages/@sanity/cli/src/commands/init/initCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options
--coupon <name> Optionally select a coupon for a new project (cannot be used with --project-plan)
--no-typescript Do not use TypeScript for template files
--package-manager <name> Specify which package manager to use [allowed: ${allowedPackageManagersString}]
--no-auto-updates Disable auto updates of studio versions
Examples
# Initialize a new project, prompt for required information along the way
Expand Down Expand Up @@ -60,6 +61,7 @@ export interface InitFlags {

'visibility'?: string
'typescript'?: boolean
'auto-updates'?: boolean
/**
* Used for initializing a project from a server schema that is saved in the Journey API
* Overrides `project` option.
Expand Down
94 changes: 94 additions & 0 deletions packages/@sanity/cli/test/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import fs from 'node:fs/promises'
import path from 'node:path'

import {describe, expect} from '@jest/globals'

import templates from '../src/actions/init-project/templates'
import {describeCliTest, testConcurrent} from './shared/describe'
import {baseTestPath, cliProjectId, getTestRunArgs, runSanityCmdCommand} from './shared/environment'

describeCliTest('CLI: `sanity init v3`', () => {
describe.each(Object.keys(templates))('for template %s', (template) => {
testConcurrent('adds autoUpdates: true to cli config', async () => {
const version = 'v3'
const testRunArgs = getTestRunArgs(version)
const outpath = `test-template-${template}-${version}`

await runSanityCmdCommand(version, [
'init',
'--y',
'--project',
cliProjectId,
'--dataset',
testRunArgs.dataset,
'--template',
template,
'--output-path',
`${baseTestPath}/${outpath}`,
'--package-manager',
'manual',
])

const cliConfig = await fs.readFile(
path.join(baseTestPath, outpath, 'sanity.cli.ts'),
'utf-8',
)

expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
expect(cliConfig).toContain(`autoUpdates: true`)
})
})

testConcurrent('adds autoUpdates: true to cli config for javascript projects', async () => {
const version = 'v3'
const testRunArgs = getTestRunArgs(version)
const outpath = `test-template-${version}`

await runSanityCmdCommand(version, [
'init',
'--y',
'--project',
cliProjectId,
'--dataset',
testRunArgs.dataset,
'--output-path',
`${baseTestPath}/${outpath}`,
'--package-manager',
'manual',
'--no-typescript',
])

const cliConfig = await fs.readFile(path.join(baseTestPath, outpath, 'sanity.cli.js'), 'utf-8')

expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
expect(cliConfig).toContain(`autoUpdates: true`)
})

testConcurrent('adds autoUpdates: false to cli config if flag provided', async () => {
const version = 'v3'
const testRunArgs = getTestRunArgs(version)
const outpath = `test-template-${version}`

await runSanityCmdCommand(version, [
'init',
'--y',
'--project',
cliProjectId,
'--dataset',
testRunArgs.dataset,
'--output-path',
`${baseTestPath}/${outpath}`,
'--package-manager',
'manual',
'--no-auto-updates',
])

const cliConfig = await fs.readFile(path.join(baseTestPath, outpath, 'sanity.cli.ts'), 'utf-8')

expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
expect(cliConfig).toContain(`autoUpdates: false`)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {BuildSanityStudioCommandFlags} from '../../actions/build/buildAction'
const helpText = `
Options
--source-maps Enable source maps for built bundles (increases size of bundle)
--auto-updates Enable auto updates of studio versions
--auto-updates / --no-auto-updates Enable/disable auto updates of studio versions
--no-minify Skip minifying built JavaScript (speeds up build, increases size of bundle)
-y, --yes Unattended mode, answers "yes" to any "yes/no" prompt and otherwise uses defaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {type DeployStudioActionFlags} from '../../actions/deploy/deployAction'
const helpText = `
Options
--source-maps Enable source maps for built bundles (increases size of bundle)
--auto-updates Enable auto updates of studio versions
--auto-updates / --no-auto-updates Enable/disable auto updates of studio versions
--no-minify Skip minifying built JavaScript (speeds up build, increases size of bundle)
--no-build Don't build the studio prior to deploy, instead deploying the version currently in \`dist/\`
Expand Down

0 comments on commit 17c02f9

Please sign in to comment.