UNPKG

ludd

Version:

The Luddites CLI Client To Install And Publish Dependencies.

19 lines (17 loc) 4.09 kB
#!/usr/bin/env node 'use strict'; const fs = require('fs'); const u=(a,c,d,b=!1,g=!1)=>{const k=d?new RegExp(`^-(${d}|-${c})$`):new RegExp(`^--${c}$`);c=a.findIndex(f=>k.test(f));if(-1==c)return{argv:a};if(b)return{value:!0,index:c,length:1};b=a[c+1];if(!b||"string"==typeof b&&b.startsWith("--"))return{argv:a};g&&(b=parseInt(b,10));return{value:b,index:c,length:2}},v=a=>{const c=[];for(let d=0;d<a.length;d++){const b=a[d];if(b.startsWith("-"))break;c.push(b)}return c},z=()=>{var a=w;return Object.keys(a).reduce((c,d)=>{const b=a[d];if("string"==typeof b)return c[`-${b}`]= "",c;d=b.command?d:`--${d}`;b.short&&(d=`${d}, -${b.short}`);let g=b.description;b.default&&(g=`${g}\nDefault: ${b.default}.`);c[d]=g;return c},{})};const w={input:{description:"The path to the input file.",command:!0},output:{description:"Where to save the output. By default prints to stdout.",default:"-",short:"o"},init:{description:"Initialise in the current folder.",boolean:!0,short:"i"},help:{description:"Print the help information and exit.",boolean:!0,short:"h"},version:{description:"Show the version's number and exit.",boolean:!0,short:"v"}},A=function(a={},c=process.argv){let [,,...d]=c;const b=v(d);d=d.slice(b.length);a=Object.entries(a).reduce((f, [n,l])=>{f[n]="string"==typeof l?{short:l}:l;return f},{});const g=[];a=Object.entries(a).reduce((f,[n,l])=>{let e;try{const h=l.short,m=l.boolean,q=l.number,p=l.command,r=l.multiple;if(p&&r&&b.length)e=b;else if(p&&b.length)e=b[0];else{const t=u(d,n,h,m,q);({value:e}=t);const x=t.index,y=t.length;void 0!==x&&y&&g.push({index:x,length:y})}}catch(h){return f}return void 0===e?f:{...f,[n]:e}},{});let k=d;g.forEach(({index:f,length:n})=>{Array.from({length:n}).forEach((l,e)=>{k[f+e]=null})});k=k.filter(f=> null!==f);Object.assign(a,{a:k});return a}(w),B=A.input,C=A.output||"-",D=A.init,E=A.version;function F(){var a={description:"The Luddites CLI Client To Install And Publish Dependencies.",example:"ludd example.txt -o out.txt",line:"ludd input [-o output] [-ihv]",usage:z()};const {usage:c={},description:d,line:b,example:g}=a;a=Object.keys(c);const k=Object.values(c),[f]=a.reduce(([e=0,h=0],m)=>{const q=c[m].split("\n").reduce((p,r)=>r.length>p?r.length:p,0);q>h&&(h=q);m.length>e&&(e=m.length);return[e,h]},[]),n=(e,h)=>{h=" ".repeat(h-e.length);return`${e}${h}`};a=a.reduce((e,h,m)=>{m=k[m].split("\n");h=n(h,f);const [q,...p]=m;h=`${h}\t${q}`; const r=n("",f);m=p.map(t=>`${r}\t${t}`);return[...e,h,...m]},[]).map(e=>`\t${e}`);const l=[d,` ${b||""}`].filter(e=>e?e.trim():e).join("\n\n");a=`${l?`${l}\n`:""} ${a.join("\n")} `;return g?`${a} Example: ${g} `:a};const G=fs.readFileSync,H=fs.writeFileSync;/* diff package https://github.com/kpdecker/jsdiff BSD License Copyright (c) 2009-2015, Kevin Decker <kpdecker@gmail.com> */ const I={black:30,red:31,green:32,yellow:33,blue:34,magenta:35,cyan:36,white:37,grey:90};function J(a){const c=I.yellow;return c?`\x1b[${c}m${a}\x1b[0m`:a};async function K(a){const {interval:c=250,writable:d=process.stdout}={};a="function"==typeof a?a():a;const b=d.write.bind(d);var g=process.env.INDICATRIX_PLACEHOLDER;if(g&&"0"!=g)return b("Initialising the package<INDICATRIX_PLACEHOLDER>"),await a;let k=1,f=`${"Initialising the package"}${".".repeat(k)}`;b(f);g=setInterval(()=>{k=(k+1)%4;f=`${"Initialising the package"}${".".repeat(k)}`;b(`\r${" ".repeat(27)}\r`);b(f)},c);try{return await a}finally{clearInterval(g),b(`\r${" ".repeat(27)}\r`)}};async function L(){const a=new Promise(c=>setTimeout(c,2E3));await K(a)};async function M(a={}){const {shouldRun:c=!0,text:d=""}=a;if(!c)return"";console.log("ludd called with %s",J(d));return d};if(A.help){const a=F();console.log(a);process.exit(0)}else E&&(console.log(require("../../package.json").version),process.exit(0));(async()=>{try{if(D)return await L();if(!B)throw Error("Please pass an input file.");const a=G(B,"utf8"),c=await M({shouldRun:!0,text:a});"-"==C?console.log(c):H(C,c);console.error("File %s successfully processed.",J(B))}catch(a){process.env.DEBUG?console.error(a.stack):console.log(a.message)}})();