//index.js文件
#!/usr/bin/env node
const clone = require('git-clone-promise')
const program = require('commander')
const shell = require('shelljs')
const path = require('path')
const log = require('tracer').colorConsole({
format: ">> {{message}}"
});
program
.version('1.6.5', '-v, --version')
.description('xserver中间件应用模板工程的cli')
.on('--help', function () {
log.info('tel: 15857009524')
log.info('author: 徐横峰 <564297479@qq.com>')
})
.option('-p, --platform', '选择开发平台 web/mobile/ie8', /^(web|mobile|ie8)$/i, 'mobile')
.option('-f, --framework', '选择开发框架 vue/react/jQuery/angular', /^(vue|react|jQuery|angular)$/i, 'vue')
program
.command('* <tpl> <project>')
.action(function (tpl, project) {
log.info('目前xhfcli支持以下模板:')
log.info('使用例子:xhfcli nuxtBlog myproject')
if (tpl && project) {
let pwd = shell.pwd()
let localpath = path.join(pwd.toString(), projectName)
log.info(`正在拉取模板代码,下载位置: ${pwd}\\${project} ...`)
clone(`https://github.com/Xuhengfeng/${tpl}.git`, localpath).then(res => {
shell.rm('-rf', path.join(localpath, '.git'))
log.info('徐横峰欢迎你!')
log.info('模板工程建立完成!!!')
})
} else {
log.error('正确命令例子:xhfcli nuxtBlog myproject')
}
})
program.parse(process.argv)
//package.json文件
{
"name": "@xuhengfeng/xhfcli",
"version": "1.2.0",
"description": "自定义快速搭建模板cli工具",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"xhfcli": "./index.js"
},
"keywords": [
"xhfcli",
"cli"
],
"author": "徐横峰 <564297479@qq.com>",
"license": "ISC",
"dependencies": {
"commander": "^2.19.0",
"git-clone-promise": "^1.0.0",
"shelljs": "^0.8.3",
"tracer": "^0.9.8"
}
}
最后一步正常的安装依赖
本地安装测试npm install -g
构建模板命令: xhfcli create xxx
通过之后发布到npm
然后再使用时先全局安装: npm install @xuhengfeng/xhfcli -g
构建模板命令: xhfcli create xxx