UNPKG

@freik/node-utils

Version:

My personal set of utilities for NodeJS

3 lines (2 loc) 968 B
import{chkFieldType as y,chkObjectOf as O,chkOneOf as x,isFunction as c,isRegex as u}from"@freik/typechk";import{exec as G}from"node:child_process";import{promisify as h}from"node:util";const F=h(G),E=y("groups",O(x(c,u)));async function M(g){const s=g||{},l=s.filter==="all"?"git ls-files":s.filter==="staged"?"git diff --diff-filter=ACMR --cached --name-only":"git diff HEAD --diff-filter=d --name-only",f=s.cwd?{encoding:"utf8",cwd:s.cwd}:{encoding:"utf8"},{stdout:a}=await F(l,f),n=a.toString().split(` `).slice(0,-1);if(E(s)){const i=s.groups,p=Object.keys(i),d=new Map([...p.map(t=>[t,i[t]])]),r={groups:new Map(p.map(t=>[t,[]])),remaining:[]};return n.forEach(t=>{let e="";if(d.forEach((o,m)=>{e===""&&(u(o)&&o.test(t)||c(o)&&o(t))&&(e=m)}),e==="")r.remaining.push(t);else{const o=r.groups.get(e);o&&o.push(t)}}),[...r.groups.keys()].filter(t=>{const e=r.groups.get(t);return!e||e.length===0}).forEach(t=>r.groups.delete(t)),r}else return n}export{M as files};