Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【20170207】Node封装命令行工具的注意点 #71

Closed
zhongxia245 opened this issue Feb 7, 2017 · 0 comments
Closed

【20170207】Node封装命令行工具的注意点 #71

zhongxia245 opened this issue Feb 7, 2017 · 0 comments

Comments

@zhongxia245
Copy link
Owner

Node封装命令行工具

可以使用命令行封装一些常用的脚手架,来创建脚手架,效果好,并且B格高。

创建脚手架可以参考 create-react-app create-bfd-app 现成的命令行脚手架。

一、总结

使用Node开发命令行工具需要注意以下几点

1. 入口文件 end point ,需要在最顶部加上

#!/usr/bin/env node --harmony

如果在 入口文件里面使用了ES6的一些语法, 则后面要加上 --harmony , 否则可以不加。

2. 在package.json

在根节点下添加一个配置 bin

{
  "name": "npm-cli-demo01",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  // 记得添加这个配置, snippet 是安装后的命令名称
  "bin": {
    "snippet": "./index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chalk": "^1.1.3",
    "co": "^4.6.0",
    "co-prompt": "^1.0.0",
    "commander": "^2.9.0",
    "superagent": "^3.4.1"
  }
}

3. 安装命令测试

$ npm install -g
$ snippet
Hello, world!

$ which snippet
/usr/local/bin/snippet

$ readlink /usr/local/bin/snippet
../lib/node_modules/bitbucket-snippet/index.js

作用,实际上是将我们脚本链接到 path 变量的位置,所以我们能够在任何地方使用它。

二、其他

其他几点,则参照这边技术文章, 讲解的很详细。并且做了一个基本的命令行工具DEMO。

编写命令行工具,可以写出很多功能,比如发邮件,生成代码,生成脚手架, 一些命令行的增强等,什么功能,就看自己怎么想。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant