nrms
Version:
##### ssf ls 查看目前源
2 lines • 4.63 kB
JavaScript
const {program}=require('commander');const PKG=require('../package.json');const registries=require('../registries.json');const inquirer=require('inquirer');const {exec,execSync}=require('child_process');const ping=require('node-http-ping');const fs=require('fs');const chalk=require('chalk');const path=require('path');program['version'](PKG['version']);const whiteList=['npm','yarn','tencent','cnpm','taobao','npmMirror'];const getOrigin=async()=>{return await execSync('npm\x20get\x20registry',{'encoding':'utf-8'});};program['command']('ls')['description']('查看镜像')['action'](async()=>{const _0x937eb4=await getOrigin();const _0x20c117=Object['keys'](registries);const _0x239e91=[];const _0x14f803=Math['max'](..._0x20c117['map'](_0x4bfdb5=>_0x4bfdb5['length']))+(0xf3b19^0xf3b1a);_0x20c117['forEach'](_0x3b1c81=>{const _0x5e0282=registries[_0x3b1c81]['registry']==_0x937eb4['trim']()?'*\x20'+_0x3b1c81:'\x20\x20'+_0x3b1c81;const _0x4b2e31=new Array(..._0x5e0282);_0x4b2e31['length']=_0x14f803;const _0x31a781=Array['from'](_0x4b2e31)['map'](_0x2c2f89=>_0x2c2f89?_0x2c2f89:'-')['join']('');_0x239e91['push'](_0x31a781+'\x20\x20'+registries[_0x3b1c81]['registry']);});console['log'](_0x239e91['join']('\x0a'));});program['command']('use')['description']('请选择镜像')['action'](()=>{inquirer['prompt']([{'type':'list','name':'sel','message':'请选择镜像','choices':Object['keys'](registries)}])['then'](_0x6f763f=>{const _0x30b78b=registries[_0x6f763f['sel']]['registry'];exec('npm\x20config\x20set\x20registry\x20'+_0x30b78b,null,(_0x4ae077,_0x30b166,_0xc3307f)=>{if(_0x4ae077){console['error']('切换错误',_0x4ae077);}else{console['log']('切换成功');}});});});program['command']('current')['description']('查看当前源')['action'](async()=>{const _0x5306db=await getOrigin();const _0x155e31=Object['keys'](registries)['find'](_0x55a9e9=>{if(registries[_0x55a9e9]['registry']===_0x5306db['trim']()){return _0x55a9e9;}});console['log'](chalk['blue']('当前源:',_0x155e31));});program['command']('ping')['description']('测试镜像地址速度')['action'](()=>{inquirer['prompt']([{'type':'list','name':'sel','message':'请选择镜像','choices':Object['keys'](registries)}])['then'](_0xe23fbf=>{const _0x13c4e0=registries[_0xe23fbf['sel']]['ping']['trim']();ping(_0x13c4e0)['then'](_0x3ce984=>console['log'](chalk['blue']('响应时长:\x20'+_0x3ce984+'ms')))['catch'](()=>console['log'](chalk['red']('GG','timeout')));});});program['command']('add')['description']('自定义镜像')['action'](()=>{inquirer['prompt']([{'type':'input','name':'name','message':'请输入镜像名称','validate'(_0x4d53e5){const _0x583709=Object['keys'](registries);if(_0x583709['includes'](_0x4d53e5)){return'不能起名'+_0x4d53e5+'跟保留字冲突';}if(!_0x4d53e5){return'名称不能为空';}return!![];}},{'type':'input','name':'url','message':'请输入镜像地址','validate'(_0x245e4f){if(!_0x245e4f){return'url不能为空';}return!![];}}])['then'](_0x448fc5=>{const _0x323404=_0x4c07a3=>{const _0x5ea2e8=_0x4c07a3['split']('');return _0x5ea2e8[_0x5ea2e8['length']-(0x4691a^0x4691b)]=='/'?_0x5ea2e8['pop']()&&_0x5ea2e8['join'](''):_0x5ea2e8['join']('');};registries[_0x448fc5['name']]={'home':_0x448fc5['url']['trim'](),'registry':_0x448fc5['url']['trim'](),'ping':_0x323404(_0x448fc5['url']['trim']())};try{fs['writeFileSync'](path['join'](__dirname,'../registries.json'),JSON['stringify'](registries,null,0xf1ff1^0xf1ff5));console['log'](chalk['blue']('添加完成'));}catch(_0x41b2ad){console['log'](chalk['red'](err));}});});program['command']('delete')['description']('删除自定义的源')['action'](()=>{const _0x2d8dca=Object['keys'](registries);if(_0x2d8dca['length']===whiteList['length']){return console['log'](chalk['red']('当前无自定义源可以删除'));}else{const _0x33a70b=_0x2d8dca['filter'](_0x1d13d7=>!whiteList['includes'](_0x1d13d7));inquirer['prompt']([{'type':'list','name':'sel','message':'请选择删除的镜像','choices':_0x33a70b}])['then'](async _0x1572db=>{const _0x3e1f10=await getOrigin();const _0x2970cc=registries[_0x1572db['sel']];if(_0x3e1f10['trim']()==_0x2970cc['registry']['trim']()){console['log'](chalk['red']('当前还在使用该镜像'+registries[_0x1572db['sel']]['registry']+',请切换其他镜像删除'));}else{try{delete registries[_0x1572db['sel']];fs['writeFileSync'](path['join'](__dirname,'../registries.json'),JSON['stringify'](registries,null,0xba1de^0xba1da));console['log'](chalk['green']('SUCCESS\x20操作完成'));}catch(_0x5dcc15){console['log'](chalk['red'](err));}}});}});program['parse'](process['argv']);