Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Prompts default #2

Closed
zce opened this issue Aug 24, 2017 · 0 comments
Closed

Prompts default #2

zce opened this issue Aug 24, 2017 · 0 comments

Comments

@zce
Copy link
Owner

zce commented Aug 24, 2017

const path = require('path')
const { execSync } = require('child_process')
const validateName = require('validate-npm-package-name')
const semver = require('semver')
const rc = require('rc')
const util = require('./util')

const options = {}

const defaultPrompts = {
  name: { type: 'input', message: 'name' }
}

const defaultValues = {
  get name () {
    return path.basename(options.dest)
  },

  get author () {
    const npmrc = rc('npm', {
      'init-author-name': '',
      'init-author-email': '',
      'init-author-url': ''
    })
    const email = npmrc['init-author-email']
    const url = npmrc['init-author-url']
    return npmrc['init-author-name'] + (email ? ` <${email}>` : '') + (url ? ` (${url})` : '')
  },

  get version () {
    const npmrc = rc('npm', {
      'init-version': '0.1.0'
    })
    return npmrc['init-version']
  },

  get license () {
    const npmrc = rc('npm', {
      'init-license': 'MIT'
    })
    return npmrc['init-license']
  },

  get repository () {
    if (!util.execSync(options.dest)) return
    try {
      return execSync(`cd ${options.dest} && git config --local --get remote.origin.url`).toString().trim()
    } catch (e) {}
  }
}

const defaultValidates = {
  name: input => {
    const result = validateName(input)
    if (result.validForNewPackages) return true
    return `Sorry, ${(result.errors || []).concat(result.warnings || []).join(' and ')}.`
  },

  version: input => {
    const result = semver.valid(input)
    if (result) return true
    return `Sorry, The '${input}' is not a semantic version.`
  }
}

/**
 * Convert all questions
 * @param  {Object}  prompts  Prompts
 * @return {String}  dest     Destination path
 * @return {Array}            All questions
 */
module.exports = (dest, prompts) => {
  // globally dest
  options.dest = dest

  prompts = Object.assign({}, defaultPrompts, prompts)

  return Object.keys(prompts).map(key => {
    const item = Object.assign({ name: key }, prompts[key])

    // default value
    if (typeof item.default === 'string' && item.default.startsWith('$')) {
      item.default = defaultValues[item.default.substr(1)]
    }

    // default validate
    const builtIn = defaultValidates[key]
    if (builtIn) {
      const custom = item.validate
      item.validate = input => {
        const result = builtIn(input)
        if (result !== true) return result
        return typeof custom !== 'function' ? true : custom(input)
      }
    }

    return item
  })
}
@zce zce closed this as completed Apr 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant