GithubHelp home page GithubHelp logo

zce / zce-cli Goto Github PK

View Code? Open in Web Editor NEW
37.0 5.0 14.0 1.48 MB

A CLI tool for my personal productivity, Scaffolding tools move to zce/caz 👉

Home Page: https://github.com/zce/caz

License: MIT License

JavaScript 100.00%
zce zce-cli cli generator productivity

zce-cli's Introduction

zce-cli's People

Contributors

dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar zce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

zce-cli's Issues

Plugin validate

wrapper.js

/**
 * Wrapper plugin
 * @param  {Function} plugin Plugin function
 * @return {Function}        Plugin function
 */
const defaultPlugin = (files, app, next) => next()

module.exports = plugin => {
  if (typeof plugin !== 'function') return defaultPlugin

  const fnStr = plugin.toString()
  const matches = /\(\s*([\s\S]*?)\s*\)/.exec(fnStr)

  if (!matches) return defaultPlugin

  const params = matches[1].split(/\s*,\s*/)

  if (params.length !== 3) return defaultPlugin

  // https://github.com/goatslacker/get-parameter-names/blob/master/index.js
  const code = fnStr.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, '')

  const regexp = new RegExp(`${params[2]}\\(\\s*\\)`)
  return regexp.test(code) ? plugin : defaultPlugin
}

Prompts default

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
  })
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.