<commit_before>import os from 'os'
import path from 'path'
import fse from 'fs-extra'
import resolveFrom from 'resolve-from'
import deburr from 'lodash/deburr'
import noop from 'lodash/noop'
import debug from '../../debug'
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'

export default async function initSanity(args, context) {
  const flags = args.extOptions
  const {output, prompt, workDir, apiClient, yarn, chalk} = context
  const unattended = flags.y || flags.yes
  const print = unattended ? noop : output.print

  const requiredForUnattended = ['project', 'dataset', 'output-path']
  if (unattended) {
    requiredForUnattended.forEach(flag => {
      if (!flags[flag]) {
        throw new Error(`\`--${flag}\` must be specified in unattended mode`)
      }
    })
  }

  print('This utility walks you through creating a Sanity installation.')
  print('Press ^C at any time to quit.\n')

  // If the user isn't already authenticated, make it so
  const userConfig = getUserConfig()
  const hasToken = userConfig.get('authToken')

  debug(hasToken ? 'User already has a token' : 'User has no token')

  if (hasToken) {
    print('Looks like you already have a Sanity-account. Sweet!\n')
  } else if (!unattended) {
    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 = 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
  debug('Prompting user to select or create a dataset')
  const {datasetName} = await getOrCreateDataset({
    projectId,
    displayName,
    dataset: flags.dataset
  })

  debug(`Dataset with name ${datasetName} selected`)

  // Gather project defaults based on environment
  const defaults = await getProjectDefaults(workDir, {isPlugin: false, context})

  // Prompt the user for required information
  let answers
  if (unattended) {
    answers = Object.assign({license: 'UNLICENSED'}, defaults, {
      outputPath: path.resolve(flags['output-path'])
    })
  } else {
    answers = {}
    answers.description = answers.description || defaults.description
    answers.gitRemote = defaults.gitRemote
    answers.author = defaults.author
    answers.license = 'UNLICENSED'

    const workDirIsEmpty = (await fse.readdir(workDir)).length === 0
    answers.outputPath = await prompt.single({
      type: 'input',
      message: 'Output path:',
      default: workDirIsEmpty ? workDir : path.join(workDir, sluggedName),
      validate: validateEmptyPath,
      filter: absolutify
    })
  }

  // Ensure we are using the output path provided by user
  const outputPath = answers.outputPath || workDir

  // Prompt for template to use
  const defaultTemplate = unattended ? flags.template || 'clean' : null
  const templateName =
    defaultTemplate ||
    (await prompt.single({
      message: 'Select project template',
      type: 'list',
      choices: [
        {
          value: 'moviedb',
          name: 'Movie database (schema + sample data)'
        },
        {
          value: 'clean',
          name: 'Clean, minimal project'
        }
      ]
    }))

  // Build a full set of resolved options
  const initOptions = {
    template: templateName,
    outputDir: outputPath,<commit_msg>[cli] Fix eslint error<commit_after>import os from 'os'
import path from 'path'
import fse from 'fs-extra'
import resolveFrom from 'resolve-from'
import deburr from 'lodash/deburr'
import noop from 'lodash/noop'
import debug from '../../debug'
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'

export default async function initSanity(args, context) {
  const flags = args.extOptions
  const {output, prompt, workDir, apiClient, yarn, chalk} = context
  const unattended = flags.y || flags.yes
  const print = unattended ? noop : output.print

  const requiredForUnattended = ['project', 'dataset', 'output-path']
  if (unattended) {
    requiredForUnattended.forEach(flag => {
      if (!flags[flag]) {
        throw new Error(`\`--${flag}\` must be specified in unattended mode`)
      }
    })
  }

  print('This utility walks you through creating a Sanity installation.')
  print('Press ^C at any time to quit.\n')

  // If the user isn't already authenticated, make it so
  const userConfig = getUserConfig()
  const hasToken = userConfig.get('authToken')

  debug(hasToken ? 'User already has a token' : 'User has no token')

  if (hasToken) {
    print('Looks like you already have a Sanity-account. Sweet!\n')
  } else if (!unattended) {
    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 = 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
  debug('Prompting user to select or create a dataset')
  const {datasetName} = await getOrCreateDataset({
    projectId,
    displayName,
    dataset: flags.dataset
  })

  debug(`Dataset with name ${datasetName} selected`)

  // Gather project defaults based on environment
  const defaults = await getProjectDefaults(workDir, {isPlugin: false, context})

  // Prompt the user for required information
  let answers
  if (unattended) {
    answers = Object.assign({license: 'UNLICENSED'}, defaults, {
      outputPath: path.resolve(flags['output-path'])
    })
  } else {
    answers = {}
    answers.description = answers.description || defaults.description
    answers.gitRemote = defaults.gitRemote
    answers.author = defaults.author
    answers.license = 'UNLICENSED'

    const workDirIsEmpty = (await fse.readdir(workDir)).length === 0
    answers.outputPath = await prompt.single({
      type: 'input',
      message: 'Output path:',
      default: workDirIsEmpty ? workDir : path.join(workDir, sluggedName),
      validate: validateEmptyPath,
      filter: absolutify
    })
  }

  // Ensure we are using the output path provided by user
  const outputPath = answers.outputPath || workDir

  // Prompt for template to use
  const defaultTemplate = unattended ? flags.template || 'clean' : null
  const templateName = defaultTemplate || (await prompt.single({
    message: 'Select project template',
    type: 'list',
    choices: [
      {
        value: 'moviedb',
        name: 'Movie database (schema + sample data)'
      },
      {
        value: 'clean',
        name: 'Clean, minimal project'
      }
    ]
  }))

  // Build a full set of resolved options
  const initOptions = {
    template: templateName,
    outputDir: outputPath,
{'fraction_non_alphanumeric': 0.11569806279225117, 'fraction_numerical': 0.0008016032064128256, 'mean_word_length': 3.388042203985932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6612700', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2131, 'n_words': 684}