Skip to content

Commit

Permalink
[cli] Clean up unused code paths (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored Sep 19, 2017
1 parent 2b79d41 commit 94c0048
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
createPluginManifest
} from './createManifest'

export function bootstrapPlugin(data, opts = {}) {
export default function bootstrapPlugin(data, opts = {}) {
const writeIfNotExists = partialRight(writeFileIfNotExists, opts.output)
const collect = {
pluginConfig: readTemplate('plugin-config'),
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/src/commands/init/initPlugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import addPluginToManifest from '@sanity/util/lib/addPluginToManifest'
import getProjectDefaults from '../../util/getProjectDefaults'
import {bootstrapPlugin} from './bootstrap'
import bootstrapPlugin from './bootstrapPlugin'
import gatherInput from './gatherInput'

export default async function initPlugin(args, context, initOpts = {}) {
Expand Down
120 changes: 48 additions & 72 deletions packages/@sanity/cli/src/commands/init/initSanity.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import resolveFrom from 'resolve-from'
import deburr from 'lodash/deburr'
import debug from '../../debug'
import getUserConfig from '../../util/getUserConfig'
import getProjectDefaults from '../../util/getProjectDefaults'
import createProvisionalUser from '../../actions/user/createProvisionalUser'
import createProject from '../../actions/project/createProject'
import login from '../../actions/login/login'
import promptForDatasetName from './promptForDatasetName'
Expand All @@ -17,26 +17,21 @@ export default async function initSanity(args, context) {
// If the user isn't already authenticated, make it so
const userConfig = getUserConfig()
const hasToken = userConfig.get('authToken')
const authType = userConfig.get('authType')
let isProvisional = authType === 'provisional'
debug(hasToken ? 'User already has a token' : 'User has no token')

if (hasToken && isProvisional) {
output.print("We found some temporary auth credentials in your Sanity config - we're gonna use")
output.print('those, but make sure to claim your account before it expires!\n')
} else if (hasToken) {
if (hasToken) {
output.print('Looks like you already have a Sanity-account. Sweet!\n')
}

if (!hasToken) {
const user = await getOrCreateUser()
isProvisional = user.isProvisional
} else {
await getOrCreateUser()
}

// We're authenticated, now lets create a project
debug('Prompting user to select or create a project')
const {projectId, displayName} = await getOrCreateProject()
const sluggedName = displayName.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9]/g, '')
const sluggedName = deburr(displayName.toLowerCase())
.replace(/\s+/g, '-')
.replace(/[^a-z0-9]/g, '')

debug(`Project with name ${displayName} selected`)

// Now let's pick or create a dataset
Expand All @@ -61,7 +56,6 @@ export default async function initSanity(args, context) {
displayName: displayName,
dataset: datasetName,
projectId: projectId,
provisionalToken: isProvisional && getUserConfig().get('authToken'),
...answers
}

Expand All @@ -78,94 +72,72 @@ export default async function initSanity(args, context) {
// Make sure we have the required configs
const coreCommands = require(resolveFrom(outputPath, '@sanity/core')).commands
const configCheckCmd = coreCommands.find(cmd => cmd.name === 'configcheck')
await configCheckCmd.action({extOptions: {quiet: true}}, Object.assign({}, context, {
workDir: outputPath
}))
await configCheckCmd.action(
{extOptions: {quiet: true}},
Object.assign({}, context, {
workDir: outputPath
})
)

// Check if we're currently in the output path, so we can give a better start message
if (outputPath === process.cwd()) {
output.print(`\n${chalk.green('Success!')} You can now run "${chalk.cyan('sanity start')}"`)
output.print(
`\n${chalk.green('Success!')} You can now run "${chalk.cyan(
'sanity start'
)}"`
)
} else {
output.print(`\n${chalk.green('Success!')} You can now change to directory "${chalk.cyan(outputPath)}" and run "${chalk.cyan('sanity start')}"`)
output.print(
`\n${chalk.green(
'Success!'
)} You can now change to directory "${chalk.cyan(
outputPath
)}" and run "${chalk.cyan('sanity start')}"`
)
}

// See if the template has a success message handler and print it
const successMessage = template.getSuccessMessage ? template.getSuccessMessage(initOptions, context) : ''
const successMessage = template.getSuccessMessage
? template.getSuccessMessage(initOptions, context)
: ''
if (successMessage) {
output.print(`\n${successMessage}`)
}

// Create a provisional user and store the token
async function getOrCreateUser() {
output.print("We can't find any auth credentials in your Sanity config - looks like you")
output.print(
"We can't find any auth credentials in your Sanity config - looks like you"
)
output.print("haven't used Sanity on this system before?\n")
//output.print("If you're looking to try out Sanity and haven't registered we can just set up")
//output.print('a temp account for testing. You can always claim it later.\n')

// Provide login options (temporarily disabled until claiming provisional accounts is in place)
/*
const authMethod = await prompt.single({
message: 'Create temporary account?',
type: 'list',
choices: [
{value: 'provisional', name: 'A temp account sounds great'},
{value: 'login', name: 'Sign in using a Google or Github account'},
{value: 'arrow', name: 'I took an arrow to the knee'}
]
})
*/
const authMethod = 'login'

if (authMethod === 'arrow') {
output.print("Cool story about the arrow, guess you're in a hurry. Here's a temp account.")
}

if (authMethod === 'arrow' || authMethod === 'provisional') {
try {
const {token} = await createProvisionalUser(apiClient)
getUserConfig().set({
authToken: token,
authType: 'provisional'
})

if (!token) {
throw new Error('No token received from server')
}
} catch (err) {
const userError = new Error('Failed to create provisional user')
userError.details = err
throw userError
}

output.print("Good stuff, you now have a token for a temporary account. You'll also need")
output.print('a project to keep your data sets and collaborators safe and snug')

return Promise.resolve({isProvisional: true})
}

// Provide login options (`sanity login`)
await login(args, context)

output.print("Good stuff, you're now authenticated. You'll need a project to keep your")
output.print(
"Good stuff, you're now authenticated. You'll need a project to keep your"
)
output.print('data sets and collaborators safe and snug.')
return Promise.resolve({isProvisional: false})
}

async function getOrCreateProject() {
let projects
try {
projects = await apiClient({requireProject: false}).projects.list()
} catch (err) {
throw new Error(`Failed to communicate with the Sanity API:\n${err.message}`)
throw new Error(
`Failed to communicate with the Sanity API:\n${err.message}`
)
}

if (projects.length === 0) {
debug('No projects found for user, prompting for name')
const projectName = await prompt.single({message: 'Informal name for your project'})
const projectName = await prompt.single({message: 'Project name'})
return createProject(apiClient, {displayName: projectName})
}

debug(`User has ${projects.length} project(s) already, showing list of choices`)
debug(
`User has ${projects.length} project(s) already, showing list of choices`
)

const projectChoices = projects.map(project => ({
value: project.id,
Expand All @@ -185,7 +157,9 @@ export default async function initSanity(args, context) {
if (selected === 'new') {
debug('User wants to create a new project, prompting for name')
return createProject(apiClient, {
displayName: await prompt.single({message: 'Informal name for your project'})
displayName: await prompt.single({
message: 'Informal name for your project'
})
})
}

Expand All @@ -211,7 +185,9 @@ export default async function initSanity(args, context) {
return {datasetName: name}
}

debug(`User has ${datasets.length} dataset(s) already, showing list of choices`)
debug(
`User has ${datasets.length} dataset(s) already, showing list of choices`
)
const datasetChoices = datasets.map(dataset => ({value: dataset.name}))

const selected = await prompt.single({
Expand Down

0 comments on commit 94c0048

Please sign in to comment.