因为专注,所以专业
2005 - 2020

使用nodejs编写命令行工具_编写自己的cli工具

时间:2020-02-05 16:56:34    作者:八叔模板堂

准备

开始

$ mkdir my-cli && cd my-cli
$ npm init
{
  "name": "my-cli",
  "version": "0.0.1",
  "description": "Auto generate project template",
  "main": "index.js",
  "bin": {
    "my-cli": "./index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/hlme/my-cli.git"
  },
  "keywords": [
    "cli"
  ],
  "author": "798400626@qq.com",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/hlme/my-cli/issues"
  },
  "homepage": "https://github.com/hlme/my-cli#readme"
}


编写可执行文件

"bin": {
  "my-cli": "./index.js"
}

全局安装你的包

用fs模块快速生成项目模板

var fs = require('fs');
var path = require('path');

function copyTemplate (from, to) {
  from = path.join(__dirname, 'templates', from);
  write(to, fs.readFileSync(from, 'utf-8'))
}
function write (path, str, mode) {
  fs.writeFileSync(path, str)
}
function mkdir (path, fn) {
  fs.mkdir(path, function (err) {
    fn && fn()
  })
}
mkdir(PATH + '/src', function () {
  mkdir(PATH + '/src/scss',function () {
    copyTemplate("scss/main.scss",PATH + '/src/scss/main.scss');
    mkdir(PATH + '/src/scss/base',function () {
      copyTemplate("scss/base/_common.scss",PATH + '/src/scss/base/_common.scss');
      copyTemplate("scss/base/_reset.scss",PATH + '/src/scss/base/_reset.scss');
      copyTemplate("scss/base/_variables.scss",PATH + '/src/scss/base/_variables.scss/')
    });
    mkdir(PATH + '/src/scss/component');
    mkdir(PATH + '/src/scss/helper');
    mkdir(PATH + '/src/scss/layout');
    mkdir(PATH + '/src/scss/page');
    mkdir(PATH + '/src/scss/theme')
  })
});

接收命令行参数

$ my-cli -j -s -v -b
//-j :添加jQuery
//-s :添加Swiper
//-v :添加Vue
//-b :添加Bootstrap
var config = {};
process.argv.slice(2).forEach( function (item) {
  switch (item) {
    case "-j":
      config.jquery = true;
      break;
    case "-s":
      config.swiper = true;
      break;
    case "-v":
      config.vue = true;
      break;
    case "-b":
      config.bootstrap = true;
      break;
  }
});
mkdir(PATH + '/public', function () {
  mkdir(PATH + '/public/css',function () {
    copyTemplate("css/main.css",PATH + '/public/css/main.css');
    config.bootstrap && copyTemplate("css/bootstrap.min.css",PATH + '/public/css/bootstrap.min.css');
    config.swiper && copyTemplate("css/swiper-3.4.1.min.css",PATH + '/public/css/swiper-3.4.1.min.css')
  });
  mkdir(PATH + '/public/iconfont');
  mkdir(PATH + '/public/img');
  mkdir(PATH + '/public/js',function () {
    copyTemplate("js/main.js",PATH + '/public/js/main.js');
    config.bootstrap && copyTemplate("js/bootstrap.min.js",PATH + '/public/js/bootstrap.min.js');
    config.vue && copyTemplate("js/vue.min.js",PATH + '/public/js/vue.min.js');
    if(config.jquery) {
      copyTemplate("js/jquery-1.8.3.min.js",PATH + '/public/js/jquery-1.8.3.min.js');
      copyTemplate("js/jquery-3.1.1.min.js",PATH + '/public/js/jquery-3.1.1.min.js');
    }
    config.bootstrap && copyTemplate("js/bootstrap.min.js",PATH + '/public/js/bootstrap.min.js');
    if(config.swiper){
      if(config.jquery) {
        copyTemplate("js/swiper-3.4.1.jquery.min.js", PATH + '/public/js/swiper-3.4.1.jquery.min.js')
      } else {
        copyTemplate("js/swiper-3.4.2.min.js",PATH + '/public/js/swiper-3.4.2.min.js')
      }
    }
  })
});

本地运行

发布到npm仓库

上传到github

结束语

赞(0
免费拨打  111-1111-1111
免费拨打  111-1111-1111免费拨打 111-1111-1111
微信二维码
手机二维码
返回顶部
返回顶部返回顶部