cli本质是一种用户操做界面,根据一些指令和参数来与程序完成交互并获得相应的反馈,好的cli还提供帮助信息,咱们常常使用的vue-cli就是一个很好的例子。本文将使用nodejs从头开发并发布一款cli工具,用来查询天气。javascript
项目效果图以下: vue
初始化一个项目:npm init -y
编写入口文件index.js:java
module.exports = function(){
console.log('welcome to Anderlaw weather')
}
复制代码
bin目录下建立index:node
#!/usr/bin/env node
require('../')()
复制代码
package.json配置bin信息ios
{
"name": "weather",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"weather": "bin/index"
}
}
复制代码
而后在根目录(package.json同级)运行npm link
,该操做会将该项目的模块信息和bin指令信息以软连接的形式添加到npm全局环境中:ajax
C:\Users\mlamp\AppData\Roaming\npm\node_modules
下多了一个模块连接C:\Users\mlamp\AppData\Roaming\npm
下多了个名为weather
的cmd文件。好处是能够更方便地进行本地调试。 而后咱们打开终端输入:weather
就会看到welcome to Anderlaw weather
的log信息vue-cli
此处咱们使用minimist
库来解析如:npm --save ,npm install 的参数。npm
安装依赖库 npm i -S minimist
json
使用process.argv
获取完整的输入信息axios
使用minimist
解析,例如:
weather today === args:{_:['today']}
weather -h === args:{ h:true }
weather --location 'china' === args:{location:'china'}
复制代码
首先咱们要实现查询今天和明天的天气,执行weather today[tomorrow]
const minimist = require('minimist');
module.exports = ()=>{
const args = minimist(process.argv.slice(2));//前两个是编译器相关路径信息,能够忽略
let cmd = args._[0];
switch(cmd){
case 'today':
console.log('今每天气不错呢,暖心悦目!');
break;
case 'tomorrow':
console.log('明天下大雨,注意带雨伞!');
break;
}
}
复制代码
以上,若是咱们输入weather
就会报错,由于没有取到参数.并且还没添加版本信息,所以咱们还须要改善代码
const minimist = require('minimist')
const edition = require('./package.json').version
module.exports = ()=>{
const args = minimist(process.argv.slice(2));//前两个是编译器相关路径信息,能够忽略
let cmd = args._[0] || 'help';
if(args.v || args.version){
cmd = 'version';//查询版本优先!
}
switch(cmd){
case 'today':
console.log('今每天气不错呢,暖心悦目!');
break;
case 'tomorrow':
console.log('明天下大雨,注意带雨伞!');
break;
case 'version':
console.log(edition)
break;
case 'help':
console.log(` weather [command] <options> today .............. show weather for today tomorrow ............show weather for tomorrow version ............ show package version help ............... show help menu for a command `)
}
}
复制代码
截止目前工做顺利进行,咱们还只是手工输入的一些mock信息,并无真正的实现天气的查询。 要想实现天气查询,必须借助第三方的API工具,咱们使用心知天气提供的免费数据服务接口。
须要先行注册,获取API key和id 发送请求咱们使用axios库(http客户请求库)
安装依赖:npm i -S axios
封装http模块
///ajax.js
const axios = require('axios')
module.exports = async (location) => {
const results = await axios({
method: 'get',
url: 'https://api.seniverse.com/v3/weather/daily.json',
params:{
key:'wq4aze9osbaiuneq',
language:'zh-Hans',
unit:'c',
location
}
})
return results.data
}
复制代码
该模块接收一个地理位置信息返回今天、明天、后台的天气信息。
例如查询上海今天的天气:weather today --location shanghai
修改入口文件,添加async标志
const minimist = require('minimist')
const ajax = require('./ajax.js')
const edition = require('./package.json').version
module.exports = async ()=>{
const args = minimist(process.argv.slice(2));//前两个是编译器相关路径信息,能够忽略
let cmd = args._[0] || 'help';
if(args.v || args.version){
cmd = 'version';//查询版本优先!
}
let location = args.location || '北京';
let data = await ajax(location);
data = data.results[0];
let posotion= data.location;
let daily = data.daily;
switch(cmd){
case 'today':
//console.log('今每天气不错呢,暖心悦目!');
console.log(`${posotion.timezone_offset}时区,${posotion.name}天气,${posotion.country}`)
console.log(`今天${daily[0].date}:白天${daily[0].text_day}夜晚${daily[0].text_night}`)
break;
case 'tomorrow':
//console.log('明天下大雨,注意带雨伞!');
console.log(`${posotion.timezone_offset}时区,${posotion.name}天气,${posotion.country}`)
console.log(`今天${daily[1].date}:白天${daily[1].text_day}夜晚${daily[1].text_night}`)
break;
case 'version':
console.log(edition)
break;
case 'help':
console.log(` weather [command] <options> today .............. show weather for today tomorrow ............show weather for tomorrow version ............ show package version help ............... show help menu for a command `)
}
}
复制代码
咱们输入 weather today --location shanghai
,发现有结果了:
截止当前,基本完成了功能开发,后续有一些小问题须要弥补一下,首先是一个进度提示,使用起来就更加可感知,咱们使用ora
库.
其次咱们须要当用户输入无匹配指令时给予一个引导,提供一个默认的log提示。
安装依赖npm i -S ora
编写loading模块:
const ora = require('ora')
module.exports = ora()
// method start and stop will be use
复制代码
修改入口文件
const minimist = require('minimist')
const ajax = require('./ajax.js')
const loading = require('./loading')
const edition = require('./package.json').version
module.exports = async ()=>{
const args = minimist(process.argv.slice(2));//前两个是编译器相关路径信息,能够忽略
let cmd = args._[0] || 'help';
if(args.v || args.version){
cmd = 'version';//查询版本优先!
}
let location = args.location || '北京';
loading.start();
let data = await ajax(location);
data = data.results[0];
let posotion= data.location;
let daily = data.daily;
switch(cmd){
case 'today':
//console.log('今每天气不错呢,暖心悦目!');
console.log(`${posotion.timezone_offset}时区,${posotion.name}天气,${posotion.country}`)
console.log(`今天${daily[0].date}:白天${daily[0].text_day}夜晚${daily[0].text_night}`)
loading.stop();
break;
case 'tomorrow':
//console.log('明天下大雨,注意带雨伞!');
console.log(`${posotion.timezone_offset}时区,${posotion.name}天气,${posotion.country}`)
console.log(`今天${daily[1].date}:白天${daily[1].text_day}夜晚${daily[1].text_night}`)
loading.stop();
break;
case 'version':
console.log(edition)
loading.stop();
break;
case 'help':
console.log(` weather [command] <options> today .............. show weather for today tomorrow ............show weather for tomorrow version ............ show package version help ............... show help menu for a command `)
loading.stop();
default:
console.log(`你输入的命令无效:${cmd}`)
loading.stop();
}
}
复制代码
发布至npm仓库以后 能够直接以npm i -g weather
全局方式安装咱们发布的cli,并在任何地方输入weather命令查询天气了哟!
若是不清楚如何发布可查看个人另外一篇文章发布一款npm包帮助理解npm
本文完!