Skip to content

Commit

Permalink
feat(cli): store metadata on template used with project
Browse files Browse the repository at this point in the history
When running 'sanity init' with the --template options, store on project.metadata.initialTemplate the template used.
  • Loading branch information
skogsmaskin committed Apr 25, 2022
1 parent 400c4e0 commit 41192c9
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/@sanity/cli/src/actions/init-project/bootstrapTemplate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import chalk from 'chalk'
import fse from 'fs-extra'
import {union, difference} from 'lodash'
import debug from '../../debug'
Expand All @@ -8,19 +9,19 @@ import {createPackageManifest, createSanityManifest} from './createManifest'
import templates from './templates'

export default async (opts, context) => {
const {output, cliRoot} = context
const {apiClient, cliRoot, output} = context
const templatesDir = path.join(cliRoot, 'templates')
const sourceDir = path.join(templatesDir, opts.template)
const outputDir = opts.outputDir
const {outputDir, projectId, template: templateName} = opts

// Check that we have a template info file (dependencies, plugins etc)
const template = templates[opts.template]
const template = templates[templateName]
if (!template) {
throw new Error(`Template "${opts.template}" not defined`)
throw new Error(`Template "${templateName}" not defined`)
}

// Copy template files
debug('Copying files from template "%s" to "%s"', opts.template, outputDir)
debug('Copying files from template "%s" to "%s"', templateName, outputDir)
let spinner = output.spinner('Bootstrapping files from template').start()
await fse.copy(sourceDir, outputDir, {
overwrite: false,
Expand Down Expand Up @@ -64,6 +65,21 @@ export default async (opts, context) => {
),
])

// Store template name metadata on project
try {
await apiClient({api: {projectId}}).request({
method: 'PATCH',
uri: `/projects/${projectId}`,
body: {metadata: {initialTemplate: `cli-${templateName}`}},
})
} catch (err) {
if (err.statusCode === 401) {
output.warn(`\n${chalk.yellow('⚠')} Unauthorized to update metadata for this project`)
} else {
output.warn(`\n${chalk.red('⚠')} ${err.message}`)
}
}

// Finish up by providing init process with template-specific info
spinner.succeed()
return template
Expand All @@ -74,7 +90,7 @@ export default async (opts, context) => {
await fse.writeFile(filePath, content, {flag: 'wx'})
} catch (err) {
if (err.code === 'EEXIST') {
output.print(`\n[WARN] File "${filePath}" already exists, skipping`)
output.warn(`\n${chalk.yellow('⚠')} File "${filePath}" already exists, skipping`)
} else {
throw err
}
Expand Down

0 comments on commit 41192c9

Please sign in to comment.