Skip to content

Commit

Permalink
feat(cli): misc copy changes (#8003)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjørge Næss <bjoerge@gmail.com>
  • Loading branch information
RostiMelk and bjoerge authored Dec 12, 2024
1 parent 2982082 commit a61c4ee
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 64 deletions.
79 changes: 28 additions & 51 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import pFilter from 'p-filter'
import resolveFrom from 'resolve-from'
import semver from 'semver'
import {evaluate, patch} from 'silver-fleece'
import which from 'which'

import {CLIInitStepCompleted} from '../../__telemetry__/init.telemetry'
import {type InitFlags} from '../../commands/init/initCommand'
Expand Down Expand Up @@ -121,7 +120,8 @@ export default async function initSanity(
const cliFlags = args.extOptions
const unattended = cliFlags.y || cliFlags.yes
const print = unattended ? noop : output.print
const warn = (msg: string) => output.warn(chalk.yellow.bgBlack(msg))
const success = output.success
const warn = output.warn

const intendedPlan = cliFlags['project-plan']
const intendedCoupon = cliFlags.coupon
Expand Down Expand Up @@ -258,25 +258,17 @@ export default async function initSanity(
if (hasToken) {
trace.log({step: 'login', alreadyLoggedIn: true})
const user = await getUserData(apiClient)
print('')
print(
`${chalk.gray(" 👤 You're logged in as %s using %s")}`,
user.name,
getProviderName(user.provider),
)
print('')
success('You are logged in as %s using %s', user.email, getProviderName(user.provider))
} else if (!unattended) {
trace.log({step: 'login'})
await getOrCreateUser()
}

let introMessage = "Let's get you started with a new project"
let introMessage = 'Fetching existing projects'
if (cliFlags.quickstart) {
introMessage = "Let's get you started with remote Sanity project"
} else if (remoteTemplateInfo) {
introMessage = "Let's get you started with a remote Sanity template"
introMessage = "Eject your existing project's Sanity configuration"
}
print(` ➡️ ${chalk.gray(introMessage)}`)
success(introMessage)
print('')

const flags = await prepareFlags()
Expand All @@ -289,7 +281,8 @@ export default async function initSanity(

// If user doesn't want to output any template code
if (bareOutput) {
print(`\n${chalk.green('Success!')} Below are your project details:\n`)
success('Below are your project details')
print('')
print(`Project ID: ${chalk.cyan(projectId)}`)
print(`Dataset: ${chalk.cyan(datasetName)}`)
print(
Expand Down Expand Up @@ -659,13 +652,11 @@ export default async function initSanity(
context,
})

if (await hasGlobalCli()) {
print('')
print('If you want to delete the imported data, use')
print(` ${chalk.cyan(`sanity dataset delete ${datasetName}`)}`)
print('and create a new clean dataset with')
print(` ${chalk.cyan(`sanity dataset create <name>`)}\n`)
}
print('')
print('If you want to delete the imported data, use')
print(` ${chalk.cyan(`npx sanity dataset delete ${datasetName}`)}`)
print('and create a new clean dataset with')
print(` ${chalk.cyan(`npx sanity dataset create <name>`)}\n`)
}

const devCommandMap: Record<PackageManager, string> = {
Expand All @@ -687,12 +678,10 @@ export default async function initSanity(
print(`Then: ${chalk.cyan(devCommand)} - to run Sanity Studio\n`)
}

if (await hasGlobalCli()) {
print(`Other helpful commands`)
print(`sanity docs - to open the documentation in a browser`)
print(`sanity manage - to open the project settings in a browser`)
print(`sanity help - to explore the CLI manual`)
}
print(`Other helpful commands`)
print(`npx sanity docs - to open the documentation in a browser`)
print(`npx sanity manage - to open the project settings in a browser`)
print(`npx sanity help - to explore the CLI manual`)

const sendInvite =
isFirstProject &&
Expand All @@ -717,16 +706,13 @@ export default async function initSanity(
trace.complete()

async function getOrCreateUser() {
print(`We can't find any auth credentials in your Sanity config`)
print('- log in or create a new account\n')
warn('No authentication credentials found in your Sanity config')
print('')

// Provide login options (`sanity login`)
const {extOptions, ...otherArgs} = args
const loginArgs: CliCommandArguments<LoginFlags> = {...otherArgs, extOptions: {}}
await login(loginArgs, {...context, telemetry: trace.newContext('login')})

print("Good stuff, you're now authenticated. You'll need a project to keep your")
print('datasets and collaborators safe and snug.')
}

async function getProjectDetails(): Promise<{
Expand Down Expand Up @@ -880,7 +866,7 @@ export default async function initSanity(
}))

const selected = await prompt.single({
message: 'Select project to use',
message: 'Create a new project or select an existing one',
type: 'list',
choices: [
{value: 'new', name: 'Create new project'},
Expand Down Expand Up @@ -1059,20 +1045,20 @@ export default async function initSanity(
type: 'list',
choices: [
{
value: 'moviedb',
name: 'Movie project (schema + sample data)',
},
{
value: 'shopify',
name: 'E-commerce (Shopify)',
value: 'clean',
name: 'Clean project with no predefined schema types',
},
{
value: 'blog',
name: 'Blog (schema)',
},
{
value: 'clean',
name: 'Clean project with no predefined schema types',
value: 'shopify',
name: 'E-commerce (Shopify)',
},
{
value: 'moviedb',
name: 'Movie project (schema + sample data)',
},
],
})
Expand Down Expand Up @@ -1560,12 +1546,3 @@ function getImportCommand(
!isCommandGroup(cmd) && cmd.name === 'import' && cmd.group === 'dataset',
)
}

async function hasGlobalCli(): Promise<boolean> {
try {
const globalCliPath = await which('sanity')
return Boolean(globalCliPath)
} catch (err) {
return false
}
}
5 changes: 2 additions & 3 deletions packages/@sanity/cli/src/actions/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import http, {type Server} from 'node:http'
import os from 'node:os'

import {type SanityClient} from '@sanity/client'
import chalk from 'chalk'
import open from 'open'

import {debug as debugIt} from '../../debug'
Expand Down Expand Up @@ -146,7 +145,7 @@ export async function login(
})
}

output.print(chalk.green('Login successful'))
output.success('Login successful')
trace.complete()
}

Expand Down Expand Up @@ -335,7 +334,7 @@ async function promptProviders(

const provider = await prompt.single({
type: 'list',
message: 'Login type',
message: 'Please log in or create a new account',
choices: providers.map((choice) => choice.title),
})

Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/src/commands/logout/logoutCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const logoutCommand: CliCommandDefinition = {
// Clear cached telemetry consent
cfg.delete(TELEMETRY_CONSENT_CONFIG_KEY)

output.print(chalk.green('Logged out'))
output.success('Logged out')
},
}

Expand Down
23 changes: 18 additions & 5 deletions packages/@sanity/cli/src/outputters/cliOutputter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@
import chalk from 'chalk'
import ora, {type Options, type Ora} from 'ora'

const SYMBOL_CHECK = chalk.green('✓')
const SYMBOL_WARN = chalk.yellow('⚠')
const SYMBOL_FAIL = chalk.red('✗')

let isFirstClear = true

export default {
print(...args: unknown[]): void {
console.log(...args)
},

success(...args: unknown[]): void {
console.log(`${SYMBOL_CHECK} ${args.join(' ')}`)
},

warn(...args: unknown[]): void {
console.warn(...args)
console.warn(`${SYMBOL_WARN} ${args.join(' ')}`)
},

error(...args: unknown[]): void {
if (args[0] instanceof Error) {
console.error(chalk.red(args[0].stack))
console.error(`${SYMBOL_FAIL} ${chalk.red(args[0].stack)}`)
} else {
console.error(...args)
console.error(`${SYMBOL_FAIL} ${args.join(' ')}`)
}
},

Expand All @@ -28,7 +36,12 @@ export default {
isFirstClear = false
},

spinner(options: Options | string): Ora {
return ora(options)
spinner(options: Options): Ora {
const spinner = ora({...options, spinner: 'dots'})
// Override the default status methods to use custom symbols instead of emojis
spinner.succeed = (text?: string) => spinner.stopAndPersist({text, symbol: SYMBOL_CHECK})
spinner.warn = (text?: string) => spinner.stopAndPersist({text, symbol: SYMBOL_WARN})
spinner.fail = (text?: string) => spinner.stopAndPersist({text, symbol: SYMBOL_FAIL})
return spinner
},
}
1 change: 1 addition & 0 deletions packages/@sanity/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export interface CommandRunnerOptions {

export interface CliOutputter {
print: (...args: unknown[]) => void
success: (...args: unknown[]) => void
warn: (...args: unknown[]) => void
error: (...args: unknown[]) => void
clear: () => void
Expand Down
11 changes: 7 additions & 4 deletions packages/@sanity/cli/src/util/generateCommandsDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ export function generateCommandsDocumentation(
const prefix = group === 'default' ? '' : ` ${group}`

const rows = [
`usage: sanity${prefix} [--default] [-v|--version] [-d|--debug] [-h|--help] <command> [<args>]`,
`usage: npx sanity${prefix} [--default] [-v|--version] [-d|--debug] [-h|--help] <command> [<args>]`,
'',
'Commands:',
]
.concat(commands.map((cmd) => ` ${padEnd(cmd.name, cmdLength + 1)} ${cmd.description}`))
.concat(['', `See 'sanity help${prefix} <command>' for specific information on a subcommand.`])
.concat([
'',
`See 'npx sanity help${prefix} <command>' for specific information on a subcommand.`,
])

return rows.join('\n')
}
Expand All @@ -43,14 +46,14 @@ export function generateCommandDocumentation(
if (!command) {
throw new Error(
subCommand
? `"${subCommand}" is not a subcommand of "${group}". See 'sanity help ${group}'`
? `"${subCommand}" is not a subcommand of "${group}". See 'npx sanity help ${group}'`
: getNoSuchCommandText(group || command),
)
}

const cmdParts = [group || command.name, subCommand].filter(Boolean).join(' ')
return [
`usage: sanity ${cmdParts} ${command.signature}`,
`usage: npx sanity ${cmdParts} ${command.signature}`,
'',
` ${command.description}`,
'',
Expand Down

0 comments on commit a61c4ee

Please sign in to comment.