fnum
Version:
Count the number and proportion of file extensions
2 lines (1 loc) • 1.69 kB
JavaScript
import a from"cac";import x from"node:path";import{globSync as d}from"tinyglobby";function c(e){let{dir:o="",patternExts:r=[],ignoreExts:t=[]}=e,n=r.map(i=>`**/*${i}`);n.length===0&&n.push("**/*");let s=t.map(i=>`**/*${i}`);return d(n,{cwd:x.resolve(process.cwd(),o),ignore:["node_modules/**/*",...s]})}function g(e){return e.map(t=>t.split(".").pop()).reduce((t,n)=>(n&&(t[n]=(t[n]||0)+1),t),{})}function u(e){let o=g(e);return Object.entries(o).reduce((t,[n,s])=>(t[n]=s/e.length,t[n]=Math.round(t[n]*10**2)/10**2,t[n]===0&&(t[n]=Math.round(t[n]*10**3)/10**3),t),{})}function l(){let e=c({dir:"src",patternExts:[".js","cjs",".mjs",".jsx",".ts",".tsx",".cts",".mts"]}),o=e.filter(s=>s.endsWith(".js")||s.endsWith(".jsx")||s.endsWith(".cjs")||s.endsWith(".mjs")),r=e.filter(s=>s.endsWith(".ts")||s.endsWith(".tsx")||s.endsWith(".cts")||s.endsWith(".mts")),t=Math.round(o.length/e.length*10**2)/10**2,n=Math.round(r.length/e.length*10**2)/10**2;return{total:e.length,jsPercent:t,tsPercent:n}}function P(){let e=a();e.command("statis","Count the number of files in the target directory").option("-d,--dir <dir>","Target Directory").option("-p,--patternExts <patternExts>","Pattern Extensions").option("-i,--ignoreExts <ignoreExts>","Ignore Extensions").action(o=>{console.log(o);let{dir:r,patternExts:t,ignoreExts:n}=o,s=c({dir:r,patternExts:t?.split(","),ignoreExts:n?.split(",")});console.log("File count:",s.length);let p=g(s);console.log("Extensions count:",p);let i=u(s);console.log("Extensions percent:",i)}),e.command("percent [language]","Count the number of files in the target directory").action(o=>{o==="ts"&&console.log(l())}),e.help(),e.parse(process.argv)}export{P as bootstrap};