Skip to content

Commit

Permalink
feat(cli): add auto updates flag in init and use in template
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed Aug 21, 2024
1 parent b03d043 commit 2acba13
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 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
24 changes: 16 additions & 8 deletions packages/@sanity/cli/src/actions/init-project/createCliConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,42 @@ export default defineCliConfig({
* Enable auto-updates for studios.
* Learn more at https://www.sanity.io/docs/cli#auto-updates
*/
autoUpdates: true,
autoUpdates: %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
enter(path) {
if (path.node.type === 'StringLiteral') {
const value = path.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 variableValue = variables[variableName]
if (typeof variableValue === 'boolean') {
// For boolean values, replace the entire string with a raw boolean
path.replaceWithSourceString(variableValue.toString())
} else if (typeof variableValue === 'string') {
// For strings, just update the value
path.node.value = variableValue
} else {
throw new Error(`Unsupported variable type for '${variableName}'`)
}
}
},
})

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 @@ -49,20 +50,27 @@ export function createStudioConfig(options: GenerateConfigOptions): string {
const template = (options.template || defaultTemplate).trimStart()
const ast = parse(template, {parser})
traverse(ast, {
StringLiteral: {
enter({node}) {
const value = node.value
enter(path) {
if (path.node.type === 'StringLiteral') {
const value = path.node.value
if (!value.startsWith('%') || !value.endsWith('%')) {
return
}

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

node.value = variables[variableName] || ''
},
const variableValue = variables[variableName]
if (typeof variableValue === 'boolean') {
// For boolean values, replace the entire string with a raw boolean
path.replaceWithSourceString(variableValue.toString())
} else if (typeof variableValue === 'string') {
// For strings, just update the value
path.node.value = variableValue
} else {
throw new Error(`Unsupported variable type for '${variableName}'`)
}
}
},
})

Expand Down
16 changes: 13 additions & 3 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,14 @@ export default async function initSanity(
}
const {chosen} = await getPackageManagerChoice(workDir, {interactive: false})
trace.log({step: 'selectPackageManager', selectedOption: chosen})
const packages = ['@sanity/vision@3', 'sanity@3', '@sanity/image-url@1', 'styled-components@6']
if (templateToUse === 'blog') {
packages.push('@sanity/icons')
}
await installNewPackages(
{
packageManager: chosen,
packages: ['@sanity/vision@3', 'sanity@3', '@sanity/image-url@1', 'styled-components@6'],
packages,
},
{
output: context.output,
Expand Down Expand Up @@ -530,6 +534,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 @@ -540,6 +550,7 @@ export default async function initSanity(
variables: {
dataset: datasetName,
projectId,
autoUpdates,
projectName: displayName || answers.projectName,
},
}
Expand Down Expand Up @@ -1277,8 +1288,7 @@ export default async function initSanity(
'# Warning: Do not add secrets (API keys and similar) to this file, as it source controlled!',
'# Use `.env.local` for any secrets, and ensure it is not added to source control',
].join('\n')
const shouldPrependWarning = !existingEnv.includes(warningComment)
// prepend warning comment to the env vars if one does not exist
const shouldPrependWarning = filename !== '.env.local' && !existingEnv.includes(warningComment)
if (shouldPrependWarning) {
await fs.writeFile(fileOutputPath, `${warningComment}\n\n${updatedEnv}`, {
encoding: 'utf8',
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 import maps that keep your studio up-to-date with the latest Sanity 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
3 changes: 3 additions & 0 deletions packages/@sanity/cli/test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describeCliTest('CLI: `sanity init v3`', () => {
'utf-8',
)

console.log(cliConfig)

expect(cliConfig).toContain(`projectId: '${cliProjectId}'`)
expect(cliConfig).toContain(`dataset: '${testRunArgs.dataset}'`)
expect(cliConfig).toContain(`autoUpdates: true`)
Expand All @@ -60,6 +62,7 @@ describeCliTest('CLI: `sanity init v3`', () => {
])

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

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

0 comments on commit 2acba13

Please sign in to comment.