自己动手,从零开始写一个nrm工具
🌙
手机阅读
本文目录结构
列出需求
我们需要先列出需要实现的功能;
我们需要实现npm仓库源的切换,可以选择使用npm官方源,也可以选择淘宝源;
使用参考: npm 使用
整理的功能如下
命令 | 说明 |
---|---|
nrm ls |
显示所有可选仓库源 |
nrm current |
显示当前的注册表名称或URL |
nrm use |
选择一个仓库源 |
nrm add |
添加一个仓库源 |
nrm del |
删除一个仓库源 |
nrm test |
显示指定或所有仓库源的响应时间 |
nrm home |
浏览器打开指定仓库源的主页 |
其他命令 | 说明 |
---|---|
nrm login |
使用base64编码的字符串或用户名和密码设置自定义注册表的授权信息 |
nrm set-hosted-repo |
设置用于自定义仓库源的托管npm存储库以发布程序包 |
nrm set-scope |
将私有域与仓库源关联 |
nrm del-scope |
移除私有域 |
nrm set |
设置自定义仓库源属性 |
nrm rename |
设置自定义仓库源名称 |
nrm publish |
如果当前注册表是自定义注册表,则将程序包发布到当前注册表。 如果您不使用自定义注册表,则此命令将直接运行npm publish |
nrm help |
打印此帮助 如果要在卸载时清除NRM配置, 可以执行 npm uninstall nrm -g -C 或 npm uninstall nrm -g --clean |
项目初始化
Tag
Github: https://github.com/axihe/nrm/tree/0.0.1
功能
初始化 package.json
文件
{
"name": "@axihe/nrm",
"version": "1.0.0",
"description": "这是一个轮子项目,`nrm` 包是解决快速切换npm源问题的",
"bin": {
"nrm": "./cli.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/axihe/nrm.git"
},
"keywords": [
"nrm",
"npm registry"
],
"author": "anbang",
"license": "ISC",
"bugs": {
"url": "https://github.com/axihe/nrm/issues"
},
"homepage": "https://github.com/axihe/nrm#readme"
}
核心
- name属性
- bin属性
实现命令行参数
效果如下
代码
const program = require('commander');
const PKG = require('./package.json');
// 命令行
program
.version(PKG.version);
program
.command('ls')
.description('显示所有可选仓库源')
.action(demo);
program
.command('current')
.option('-u, --show-url', 'Show the registry URL instead of the name')
.description('显示当前的注册表名称或URL')
.action(demo);
program
.command('use <registry>')
.description('选择一个仓库源')
.action(demo);
program
.command('add <registry> <url> [home]')
.description('添加一个仓库源')
.action(demo);
program
.command('del <registry>')
.description('删除一个仓库源')
.action(demo);
program
.command('test [registry]')
.description('显示指定或所有仓库源的响应时间')
.action(demo);
program
.command('home <registry> [browser]')
.description('浏览器打开指定仓库源的主页')
.action(demo);
program
.command('login <registryName> [value]')
.option('-a, --always-auth', 'Set is always auth')
.option('-u, --username <username>', 'Your user name for this registry')
.option('-p, --password <password>', 'Your password for this registry')
.option('-e, --email <email>', 'Your email for this registry')
.description('使用base64编码的字符串或用户名和密码设置自定义注册表的授权信息')
.action(demo);
program
.command('set-hosted-repo <registry> <value>')
.description('设置用于自定义仓库源的托管npm存储库以发布程序包')
.action(demo);
program
.command('set-scope <scopeName> <value>')
.description('将私有域与仓库源关联')
.action(demo);
program
.command('del-scope <scopeName>')
.description('移除私有域')
.action(demo);
program
.command('set <registryName>')
.option('-a,--attr <attr>', 'set custorm registry attribute')
.option('-v,--value <value>', 'set custorm registry value')
.description('设置自定义仓库源属性')
.action(demo);
program
.command('rename <registryName> <newName>')
.description('设置自定义仓库源名称')
.action(demo);
program
.command('publish [<tarball>|<folder>]')
.option('-t, --tag [tag]', 'Add tag')
.option('-a, --access <public|restricted>', 'Set access')
.option('-o, --otp [otpcode]', 'Set otpcode')
.option('-dr, --dry-run', 'Set is dry run')
.description('如果当前注册表是自定义注册表,则将程序包发布到当前注册表。\n如果您不使用自定义注册表,则此命令将直接运行')
.action(demo);
program
.command('help', { isDefault: true })
.description('如果要在卸载时清除NRM配置,可以执行 "npm uninstall nrm -g -C" ')
.action(function () {
program.outputHelp();
});
program
.parse(process.argv);
if (process.argv.length === 2) {
program.outputHelp();
}
function demo (arg1, arg2, arg3) {
console.log(`
接收到的信息
第1参数: ${arg1}
第2参数: ${arg2}
第3参数: ${arg3}
`)
}