From 3fa192c49f0a2f30836b83575322a4922b96aae7 Mon Sep 17 00:00:00 2001 From: Espen Hovlandsdal Date: Tue, 9 Jan 2018 14:22:38 +0100 Subject: [PATCH] [cli] Show prompt on whether or not to import data for a template --- .../src/actions/init-project/initProject.js | 48 ++++++++++++++++++- .../actions/init-project/templates/moviedb.js | 21 +------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/packages/@sanity/cli/src/actions/init-project/initProject.js b/packages/@sanity/cli/src/actions/init-project/initProject.js index 951b9c2e961..d2cdcf6cf7e 100644 --- a/packages/@sanity/cli/src/actions/init-project/initProject.js +++ b/packages/@sanity/cli/src/actions/init-project/initProject.js @@ -4,7 +4,10 @@ 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' @@ -12,6 +15,12 @@ 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 @@ -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 { @@ -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?`) @@ -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) @@ -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) { diff --git a/packages/@sanity/cli/src/actions/init-project/templates/moviedb.js b/packages/@sanity/cli/src/actions/init-project/templates/moviedb.js index d1f8f5ea9cc..c43a4e1f161 100644 --- a/packages/@sanity/cli/src/actions/init-project/templates/moviedb.js +++ b/packages/@sanity/cli/src/actions/init-project/templates/moviedb.js @@ -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', @@ -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') -}