Skip to content

Commit

Permalink
[cli] Show prompt on whether or not to import data for a template
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Jan 10, 2018
1 parent 86a0d40 commit 3fa192c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
48 changes: 47 additions & 1 deletion packages/@sanity/cli/src/actions/init-project/initProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import fse from 'fs-extra'
import resolveFrom from 'resolve-from'
import deburr from 'lodash/deburr'
import noop from 'lodash/noop'
import {reduceConfig} from '@sanity/util'
import {loadJson} from '@sanity/util/lib/safeJson'
import debug from '../../debug'
import clientWrapper from '../../util/clientWrapper'
import getUserConfig from '../../util/getUserConfig'
import getProjectDefaults from '../../util/getProjectDefaults'
import createProject from '../project/createProject'
import login from '../login/login'
import dynamicRequire from '../../util/dynamicRequire'
import promptForDatasetName from './promptForDatasetName'
import bootstrapTemplate from './bootstrapTemplate'
import templates from './templates'

/* eslint-disable no-process-env */
const sanityEnv = process.env.SANITY_ENV
const environment = sanityEnv ? sanityEnv : process.env.NODE_ENV
/* eslint-enable no-process-env */

export default async function initSanity(args, context) {
const flags = args.extOptions
Expand Down Expand Up @@ -119,8 +128,16 @@ export default async function initSanity(args, context) {
...answers
}

const template = templates[templateName]
if (!template) {
throw new Error(`Template "${templateName}" not found`)
}

// If the template has a sample dataset, prompt the user whether or not we should import it
const shouldImport = template.datasetUrl && await promptForDatasetImport(template.importPrompt)

// Bootstrap Sanity, creating required project files, manifests etc
const template = await bootstrapTemplate(initOptions, context)
await bootstrapTemplate(initOptions, context)

// Now for the slow part... installing dependencies
try {
Expand All @@ -139,6 +156,22 @@ export default async function initSanity(args, context) {
})
)

// Prompt for dataset import (if a dataset is defined)
if (shouldImport) {
const manifestPath = path.join(outputPath, 'sanity.json')
const baseManifest = await loadJson(manifestPath)
const manifest = reduceConfig(baseManifest || {}, environment)

const importCmd = coreCommands.find(cmd => cmd.name === 'import' && cmd.group === 'dataset')
await importCmd.action(
{argsWithoutOptions: [template.datasetUrl, datasetName], extOptions: {}},
Object.assign({}, context, {
apiClient: clientWrapper(manifest, manifestPath),
workDir: outputPath
})
)
}

const isCurrentDir = outputPath === process.cwd()

print(`\n${chalk.green('Success!')} Now what?`)
Expand All @@ -151,6 +184,11 @@ export default async function initSanity(args, context) {
print(`▪ ${chalk.cyan('sanity docs')} for documentation`)
print(`▪ ${chalk.cyan('sanity manage')} to open the management tool`)

if (shouldImport) {
print('')
print(`If you want to delete the imported data, use ${chalk.cyan(`sanity dataset delete ${datasetName}`)}`)
}

// See if the template has a success message handler and print it
const successMessage = template.getSuccessMessage
? template.getSuccessMessage(initOptions, context)
Expand Down Expand Up @@ -279,6 +317,14 @@ export default async function initSanity(args, context) {
debug(`Returning selected dataset (${selected})`)
return {datasetName: selected}
}

function promptForDatasetImport(message) {
return prompt.single({
type: 'confirm',
message: message || 'This template includes a sample dataset, would you like to import it?',
default: true
})
}
}

async function validateEmptyPath(dir) {
Expand Down
21 changes: 2 additions & 19 deletions packages/@sanity/cli/src/actions/init-project/templates/moviedb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import path from 'path'

const datasetUrl = 'https://public.sanity.io/moviesdb.ndjson'
const indent = line => ` ${line}`
export const importPrompt = 'Import a sampling of sci-fi movies to go with your movie schema?'
export const datasetUrl = 'https://public.sanity.io/moviesdb.ndjson'

export const dependencies = {
'@sanity/google-maps-input': '^0.99.0',
Expand All @@ -11,18 +9,3 @@ export const generateSanityManifest = base => ({
...base,
plugins: base.plugins.concat(['@sanity/google-maps-input'])
})

export const getSuccessMessage = (opts, context) => {
const {chalk} = context
const cwd = process.cwd()
const inDir = opts.outputDir === cwd
const cdCmd = inDir ? '' : `cd ${path.relative(cwd, opts.outputDir)}`

return [
`${chalk.cyan('Note:')} If you want to import some sample data to your new project, run:`,
'',
chalk.cyan(cdCmd),
chalk.cyan(`sanity dataset import ${datasetUrl} ${opts.dataset}`),
''
].map(indent).join('\n')
}

0 comments on commit 3fa192c

Please sign in to comment.