-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.min.js
3 lines (3 loc) · 5.83 KB
/
cli.min.js
1
2
3
#!/usr/bin/env node
const F=require("child_process");var e=require("fs");const L={npm:{home:"https://www.npmjs.org",registry:"https://registry.npmjs.org/"},yarn:{home:"https://yarnpkg.com",registry:"https://registry.yarnpkg.com/"},taobao:{home:"https://npmmirror.com/",registry:"https://registry.npmmirror.com/"},cnpm:{home:"https://cnpmjs.org",registry:"https://r.cnpmjs.org/"},huawei:{home:"https://repo.huaweicloud.com/repository/npm/",registry:"https://repo.huaweicloud.com/repository/npm/"},tencent:{home:"https://mirrors.cloud.tencent.com/npm/",registry:"https://mirrors.cloud.tencent.com/npm/"},npmMirror:{home:"https://skimdb.npmjs.com/",registry:"https://skimdb.npmjs.com/registry/"},github:{home:"https://npm.pkg.github.com/",registry:"https://npm.pkg.github.com/"},ustc:{home:"https://mirrors.ustc.edu.cn/help/npm.html",registry:"https://npmreg.proxy.ustclug.org/"}},s={yarn:"yarn",pnpm:"pnpm",npm:"npm",y:"yarn",p:"pnpm",n:"npm"},i=console.log,r=e.writeFileSync,M=e.readFileSync;var T=require("path").resolve;const U=e.existsSync,n=F.execSync,o=Object.keys;var t,a,c,m=process.argv.slice(2),e=m.shift(),I=$();const H=T(__dirname,"./nru.json"),p={green:"[32m%s[0m",red:"[31m%s[0m",yellow:"[33m%s[0m",mb:"[34m",my:"[33m",end:"[0m"},g={red:function(e){i(p.red,e)},green:function(e){i(p.green,e)}},J={stdio:"inherit"},P=o(I).map(e=>e.length).sort((e,r)=>r-e)[0]+3,u=" No registries",l=" No registry named ";switch(e){case"current":case"cur":d=O(),Q=j(d),i(" Current manager: "+d),i(" Current registry: "+Q);break;case"l":case"ls":case"list":{const W=O(),_={n:j("n"),y:j("y"),p:j("p")},se=$(),ne=o(se),A=o(_);let s,n=!1;g.green("* "+W);for(const D of ne){s=se[D].registry;let r="",t;A.length>r.length&&A.forEach(e=>{_[e]===s&&(r+=e,e===W[0]?(t=p.green,n=!0):t=t||p.yellow)}),t?i(t,q(r.padStart(A.length," ").toUpperCase()+" ",D,s)):i(q(" ",D,s))}n||i(`
current registry: ${_[W]} not in list`)}break;case"use":case"u":V(m[0]);break;case"test":case"t":var d=m[0],h=$();if(d&&h[d])Y(h[d].registry,d);else{var z=j(O());let e,r=!1;for(const oe of o(h))e=h[oe].registry,r=e===z,Y(e,oe);r||Y(z,"current")}break;case"add":case"a":var y=m[0],B=m[1],G=m[2];if(L[y])g.red(" Registry named "+y+" already exists");else{const ie=x();if(U(ie)){const ae=C();if(ae[y]){const ce=require("readline").createInterface({input:process.stdin,output:process.stdout});ce.question(p.my+" Registry named "+y+" already exists, do you want to override it? [y/N] "+p.end,e=>{ce.close(),e.toLowerCase().startsWith("y")?K():g.red(" Aborted")})}else K();function K(){ae[y]={registry:ee(B),home:G},r(ie,N(ae)),g.green(" Registry "+y+" added")}}else r(ie,N({[y]:{registry:ee(B),home:G}}))}break;case"remove":case"delete":case"rm":case"del":case"d":var f=m;if(f&&f.length){var Q=x();const me=C();var b=o(me);b.length?(b.forEach(e=>{f.includes(e)&&delete me[e]}),r(Q,N(me)),g.green(" Registry "+f.join(", ")+" deleted")):g.red(u)}else g.red(u+" to delete");break;case"rename":case"ren":b=m[0],k=m[1],b&&k?(v=C(),o(v).length?v[b]?v[k]?g.red(" Registry named "+k+" already exists"):(v[k]=v[b],delete v[b],r(x(),N(v)),g.green(" Registry "+b+" renamed to "+k)):g.red(l+b):g.red(u)):g.red(u+" to rename");break;case"home":var v=m[0],k=$();if(k[v]){k=k[v].home;if(k){let e;switch(process.platform){case"darwin":e="open";break;case"win32":e="start";break;default:e="xdg-open"}i(p.yellow," Opening "+k+" in browser"),F.exec(e+" "+k)}else g.red(" No home for registry "+v)}else g.red(l+v);break;case"define":case"def":w=m[0],E=m[1],w=w.toLowerCase(),o(s).includes(w)&&(r(H,JSON.stringify({define:w})),g.green(" Current manager defined as "+w),E)&&V(E);break;case"login":case"lg":var w=m.shift(),E=m,S=["login"],R=(R=$())[R[w]?w:"npm"].registry;te(w=s[O()])&&(re("npm",R,S,E),S.unshift("npm")),re(w,R,S,E);break;case"publish":case"pub":X(m.shift(),m);break;case"unpublish":case"unpub":X(m.shift(),m,"un");break;case"set":R=m[0],S=m[1],a=m[0],(c=C())[R]?(c[R][S]=a,r(x(),N(c)),g.green(" Registry "+R+" set "+S+" to "+a)):g.red(l+R);break;case"set-scope":case"set-s":c=m[0],a=m[1],(t=$())[a]?n("npm config set @"+c+":registry "+t[a]):g.red(l+a);break;case"del-scope":case"del-s":t=m[0],n("npm config rm @"+t+":registry");default:i(M("./README.md","utf8").match(/Usage\s*```bash([\s\S]*?)\s*```/)[1])}function V(e="npm"){var r,t=$()[e];t?(r=O(),function(e,r){r=s[r];if(r)return n(r+" config set "+Z(r)+" "+e),1}(t.registry,r)&&i(p.green," Set registry to",p.mb,t.registry,p.end)):g.red(l+e)}function X(e,r,t=""){var s=$();re("npm",s[s[e]?e:"npm"].registry,[t+"publish"],r)}function Y(e,r){var t=e.startsWith("http:")?require("http"):require("https");const s=Date.now();let n;const o=t.get(e+"react",()=>{var e=Date.now();i(q(" ",r,e-s+"ms")),n&&clearTimeout(n)});o.on("error",e=>{g.red(q(" ",r,e.message))}),n=setTimeout(()=>{o.destroyed||o.destroy(new Error("Timeout"))},5e3)}function j(e){return ee(n(s[e]+" config get "+Z(s[e])).toString().trim())}function q(e,r,t){return""+e+(r+" ").padEnd(P,"-")+" "+t}function x(){return(process.env.HOME||process.env.USERPROFILE)+"/.nrmrc"}function $(){return Object.assign({},L,C())}function C(){var e=x();if(U(e)){e=M(e,"utf-8");const r=e.split(/\r\n|\r|\n/),n={};let t=null,s;return r.forEach(e=>{if(!/^\s*[;#]/.test(e))if(s=e.match(/^\s*\[([^\]]*)\]\s*$/))t=s[1];else if(s=e.match(/^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/)){var r=s[1];let e=s[2];e='"'===e.charAt(0)?e.replace(/\\(["\\])/g,"$1").replace(/^(["'])(.*)\1$/,"$2"):e.replace(/\\(\\)/g,"$1"),t=t||"default",n[t]||(n[t]={}),void(n[t][r]=e)}else 0===e.length&&(t=t&&null)}),n}return{}}function Z(e){return te(e)?"npmRegistryServer":"registry"}function ee(e){return e.endsWith("/")?e:e+"/"}function re(e,r,t,s){F.spawn(e,[...t,"--registry="+r,...s],J)}function O(){return require(H).define}function te(e){return"yarn"===e&&!n("yarn -v").toString().startsWith("1.")}function N(t){let s="";return o(t).forEach(r=>{s+="["+r+"]\n",o(t[r]).forEach(e=>{s+=e+"="+t[r][e]+"\n"}),s+="\n"}),s}