npm 創建cli項目 原

package.json

{
  "name": "js-w4-v2",
  "version": "1.0.15",
  "description": "Auto webpack4 + vue2 project template",
  "main": "index.js",
  "bin": {
    "js-w4-v2": "./index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/jack0888/js-w4-v2.git"
  },
  "keywords": [
    "js-w4-v2"
  ],
  "author": "[email protected]",
  "dependencies": {
    "commander": "^2.17.1",
    "git-clone": "^0.1.0",
    "shelljs": "^0.8.2",
    "tracer": "^0.9.1"
  },
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/jack0888//issues"
  },
  "homepage": "https://github.com/jack0888/#readme"
}

index.js

#!/usr/bin/env node

const clone = require('git-clone')
const program = require('commander')
const shell = require('shelljs');
const log = require('tracer').colorConsole()


program
    .version('1.0.15')
    .description('Auto webpack4 + vue2 project template')
program
    .command('* init')
    .action(function() {
        log.info('目前js-w4-v2只有一種模板,示例:js-w4-v2 init')
        let pwd = shell.pwd()
        let url = `https://github.com/jack0888/js-w4-v2.git`;
        log.info(`正在${url}拉取模板代碼 ...`)
        clone(url, pwd + ``, null, function() {
            shell.rm('-rf', pwd + `/.git`)
            log.info('模板工程建立完成')
        })
    })
program.parse(process.argv)

ps: 傳參寫法

program
    .version('1.0.15')
    .description('Auto webpack4 + vue2 project template')
program
    .command('* init <project> <tpl>')
    .action(function(tpl, project) {
        log.info('目前js-w4-v2支持amd和webpack兩種模板,示例:js-w4-v2 init myproject --amd | --webpack')
        if (tpl && project) {
            let pwd = shell.pwd()
            let url;
            if(tpl == '--amd'){
                url = `https://github.com/jack0888/js-w4-v2.git`;
            }else{
                url = `https://github.com/jack0888/js-w4-v2.git`;
            }
            log.info(`正在${url}拉取模板代碼 ...`)
            clone(url, pwd + `/${project}`, null, function() {
                shell.rm('-rf', pwd + `/${project}/.git`)
                log.info('模板工程建立完成')
            })
        } else {
            log.error('正確命令例子:js-w4-v2 init myproject --amd | --webpack')
        }
    })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章