一些脚手架相关的 npm 包用法汇总

2020-03-04  本文已影响0人  风之化身呀

commander

nodejs 命令行解决方案

// 基本用法
const program = require('commander')
program
  .version('1.0.0')
  .name("my-command")
  .usage('<command> [options]')
  .description('测试commander')
  .option('-f, --float <number>', 'float argument')
  .parse(process.argv) // 将option传入的值挂在 pragram 上,如上面的 --float 参数被挂在 program.float

// command 的两种用法
// 1、command 接受一个参数 + action
program
  .command('rm <dir>')
  .option('-r, --recursive', 'Remove recursively')
  .action(function (dir, cmdObj) {
    console.log('remove ' + dir + (cmdObj.recursive ? ' recursively' : ''))
  })
program.parse(process.argv)
// 2、command 接受两个参数(Git风格的子命令)。Commander 将会尝试在入口脚本(例如 ./examples/pm)的目录中搜索 program-command 形式的可执行文件,例如 pm-install, pm-search。
const program = require('commander');
program
  .version('0.1.0')
  .command('install [name]', 'install one or more packages')
  .command('search [query]', 'search with optional query')
  .command('update', 'update installed packages', {executableFile: 'myUpdateSubCommand'})
  .command('list', 'list packages installed', {isDefault: true})
  .parse(process.argv);

inquirer

nodejs 交互命令行解决方案

const inquirer = require('inquirer')

const requireLetterAndNumber = value => {
  if (/\w/.test(value) && /\d/.test(value)) {
    return true
  }
  return 'Password need to have at least a letter and a number'
}

inquirer
  .prompt([{
    type: 'password',
    message: 'Enter a password',
    name: 'password1',
    validate: requireLetterAndNumber
  },
  {
    type: 'password',
    message: 'Enter a masked password',
    name: 'password2',
    mask: '*',
    validate: requireLetterAndNumber
  }
  ])
  .then(answers => console.log(JSON.stringify(answers, null, '  ')))

chalk

通过颜色来高亮 console.log 打印出来的信息

console.log(
        '  ' + chalk.yellow('★') +
        '  ' + chalk.blue(repo.name) +
        ' - ' 

download-git-repo

用于下载远程仓库至本地 支持GitHub、GitLab、Bitbucket

// download(repository, destination, options, callback)
// 以克隆模式将 flippidippi/download-git-repo-fixture 下载到本地 test/tmp 目录下
download('flippidippi/download-git-repo-fixture', 'test/tmp', {clone:true},function (err) {
  console.log(err ? 'Error' : 'Success')
})

ora

用于命令行上的加载效果

const ora=require('ora')
const spinner = ora('downloading template')  
spinner.start()//显示加载状态
spinner.stop() //隐藏加载状态

user-home

用于获取用户电脑的根目录

const home=require('user-home')
path.join(home, '.vue-templates')

tildify

将绝对路径转换成带波浪符的路径

const tildify = require('tildify');
tildify('/Users/sindresorhus/dev');   //=> '~/dev'

metalsmith

插件式的静态网站生成器

// 插件格式
function <function name> {
  return (files,metalsmith,done)=>{
    //逻辑代码
    ...
  }
}

handlebars

模版引擎,渲染字符串级别

var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
             "{{kids.length}} kids:</p>" ;
var template = Handlebars.compile(source);
var data = { "name": "Alan", "hometown": "Somewhere, TX"}
var result = template(data);

consolidate

支持各种模板引擎的渲染,渲染文件级别

var cons = require('consolidate');
cons.handlebars.render('views/page.html', { user: 'tobi' })
  .then(function (html) {
    console.log(html);
  })
  .catch(function (err) {
    throw err;
  });

multimatch

支持多个条件的匹配

// multimatch(paths, patterns, [options]
const multimatch = require('multimatch');
multimatch(['unicorn','.git' ,'cake', 'rainbows'], ['*', '!cake'],{dot:true});
//=> ['unicorn','.git', 'rainbows']
上一篇下一篇

猜你喜欢

热点阅读