text: const path = require('path')
  const metadata = require('read-metadata')
  const exists = require('fs').existsSync
  const getGitUser = require('./git-user')
  // 校验名称是否为一个有效的npm报名
  const validateName = require('validate-npm-package-name')
  
  /**
   * Read prompts metadata.
   *
   * @param {String} dir
   * @return {Object}
   */
  
  module.exports = function options (name, dir) {
    const opts = getMetadata(dir)
  
    // 设置opts中的name字段的默认值为name值
    setDefault(opts, 'name', name)
    // 判断opts.prompts.name是否为一个有效的npm包名
    setValidateName(opts)
  
    // 获取git账号
    const author = getGitUser()
    if (author) {
      // 设置到opts中
      setDefault(opts, 'author', author)
    }
  
    return opts
  }
  
  /**
   * Gets the metadata from either a meta.json or meta.js file.
   *
   * @param  {String} dir
   * @return {Object}
   */
  
  function getMetadata (dir) {
    const json = path.join(dir, 'meta.json')
    const js = path.join(dir, 'meta.js')
    let opts = {}
  
    if (exists(json)) {
      opts = metadata.sync(json)
    } else if (exists(js)) {
      const req = require(path.resolve(js))
      if (req !== Object(req)) {
        throw new Error('meta.js needs to expose an object')
      }
      opts = req
    }
  
    return opts
  }
  
  /**
   * Set the default value for a prompt question
   * opts.prompts[key].default = val
   *
   * @param {Object} opts
   * @param {String} key: opts中对应的字段
   * @param {String} val:
   */
  
  function setDefault (opts, key, val) {
    if (opts.schema) {
      opts.prompts = opts.schema
      delete opts.schema
    }
    const prompts = opts.prompts || (opts.prompts = {})
    // 如果key对应的字段不存在，或者不是一个对象，则设置key字段的值，
    // 并设置其中的default为val
    if (!prompts[key] || typeof prompts[key] !== 'object') {
      prompts[key] = {
        'type': 'string',
        'default': val
      }
    } else {
      // 设置默认值
      prompts[key]['default'] = val
    }
  }
  
  function setValidateName (opts) {
    const name = opts.prompts.name
    const customValidate = name.validate
    name.validate = name => {
      const its = validateName(name)
      if (!its.validForNewPackages) {
        const errors = (its.errors || []).concat(its.warnings || [])
        return 'Sorry, ' + errors.join(' and ') + '.'
      }
      if (typeof customValidate === 'function') return customValidate(name)
      return true
    }
  }
  