UNPKG

@anolilab/lint-staged-config

Version:
6 lines (3 loc) 6.93 kB
var E=Object.defineProperty;var d=(t,e)=>E(t,"name",{value:e,configurable:!0});import{existsSync as _}from"node:fs";import{hasPackageJsonAnyDependency as a,parsePackageJsonSync as P,findPackageManagerSync as S}from"@visulima/package";import{dirname as y,join as A}from"node:path";import{findUpSync as T,isAccessibleSync as O}from"@visulima/fs";import{ESLint as C}from"eslint";import{quote as j}from"shell-quote";import{platform as N,env as R}from"node:process";var q=Object.defineProperty,$=d((t,e)=>q(t,"name",{value:e,configurable:!0}),"o$1");const G=$(t=>{const e=T("package.json",{cwd:t});return e&&y(e)},"packageDirectorySync"),L=$(t=>{const e=G(t??process.cwd());if(e?.startsWith("/"))return e;throw new Error(`Cannot determine the nearest root of the package for the file: ${t}!`)},"getNearestPackageRootPath"),D=$(t=>{const e=A(...t);if(e.startsWith("/"))return e;throw new TypeError("Joined path did not return an absolute path.")},"joinPaths"),w=$((t,e)=>{const o=L(e),s=D([o,t]);if(O(s))return s;throw new Error(`Cannot locate nearest "${t}" file!`)},"getNearestConfigPath");var F=Object.defineProperty,J=d((t,e)=>F(t,"name",{value:e,configurable:!0}),"i$1");const H=J(t=>{const e={};return t.forEach(o=>{const s=y(o);e[s]||(e[s]=[]),e[s].push(o)}),e},"groupFilePathsByDirectoryName"),v=N==="win32"||/^(msys|cygwin)$/u.test(R.OSTYPE??"");var I=Object.defineProperty,M=d((t,e)=>I(t,"name",{value:e,configurable:!0}),"o");const U=M(async(t,e=new C)=>{const o=(await Promise.allSettled(t.map(async s=>await e.isPathIgnored(s)))).map(s=>{if(s.status==="fulfilled")return s.value;throw console.error(` Unable to determine if file is ignored. ${s.reason}`),new Error("Stopping lint-staged because of an error.")});return t.filter((s,n)=>!o[n]).map(s=>`"${v?s:j([s])}"`)},"removeIgnoredFiles");var W=Object.defineProperty,k=d((t,e)=>W(t,"name",{value:e,configurable:!0}),"m");const m=".eslintrc",B=k((t,e)=>{const o=[];t["max-warnings"]!==void 0&&Number.isNaN(t["max-warnings"])?o.push(`--max-warnings=${t["max-warnings"]}`):t["max-warnings"]!==!1&&o.push("--max-warnings=0");const s=[];a(e,["eslint-plugin-react-hooks"])&&s.push("react-hooks/exhaustive-deps:off"),a(e,["eslint-plugin-eslint-comments"])&&s.push("eslint-comments/no-unused-disable:off");const n=[...t.rules??[],...s].filter(f=>f.trim().length>0);n.length>0&&o.push(n.map(f=>`--rule "${f}"`).join(" "));const l=[...t["fix-type"]??["layout"]].filter(f=>f.trim().length>0);return l.length>0&&o.push(`--fix-type ${l.join(",")}`,"--fix"),t.cache&&o.push("--cache"),o},"createEslintArguments"),Y=k(async(t,e,o,s)=>{const n=await U(s),l=B(o,e);if(o.config)return l.push(`--config ${o.config}`),[`${t} exec eslint ${l.join(" ")} ${n.join(" ")}`];const f=H(n),p=[];return Object.values(f).forEach(i=>{let r;[m,`${m}.js`,`${m}.cjs`,`${m}.json`,`${m}.yaml`,`${m}.yml`].forEach(x=>{if(!r)try{r=w(x,i[0])}catch{}}),r&&p.push(`${t} exec eslint ${l.join(" ")} --config ${r} ${i.join(" ")}`)}),p},"createEslintCommands");var z=Object.defineProperty,K=d((t,e)=>z(t,"name",{value:e,configurable:!0}),"r");const c=K(t=>t.map(e=>`"${v?e:j([e])}"`).join(" ").replaceAll("/@","/@"),"concatFiles");var Q=Object.defineProperty,h=d((t,e)=>Q(t,"name",{value:e,configurable:!0}),"f");const V=["cjs","js","mjs","cts","ts","mts","yml","yaml","jsx","tsx","mdx","toml","json","json5","jsonc"],X=["cts","ts","mts","tsx","ctsx"],Z=["css","scss","sass","less","styl","stylus","pcss","postcss","sss"],ce=h((t={})=>{const e={debug:!1,eslint:{extensions:V},stylesheets:{extensions:Z},typescript:{exclude:[],extensions:X},...t},o=e.cwd||process.cwd();if(!_(`${o}/package.json`))throw new Error(`No package.json found in the current working directory: ${o}; Please adjust the "cwd" option.`);const s=P(`${o}/package.json`,{resolveCatalogs:!0}),{packageManager:n}=S(o);e.debug&&console.debug("Package manager found:",n);const l=a(s,["markdownlint-cli"]),f=a(s,["markdownlint-cli2"]),p=a(s,["prettier"]);let i={};if(e.eslint!==!1&&a(s,["eslint"])){if(!Array.isArray(e.eslint.extensions)||e.eslint.extensions.length===0)throw new Error("The `extensions` option is required for the ESLint configuration.");!l&&!f&&e.eslint.extensions.push("md"),i[`**/*.{${e.eslint.extensions.join(",")}}`]=async r=>[...p?[`${n} exec prettier --write ${c(r)}`]:[],...await Y(n,s,e.eslint,r)]}if(e.json!==!1&&a(s,["sort-package-json"])&&(i["**/package.json"]=r=>[`${n} exec sort-package-json ${c(r)}`]),e.markdown!==!1&&l&&(i={...i,"**/*.md":h(r=>[...p?[`${n} exec prettier --write ${c(r)}`]:[],...l?[`${n} exec markdownlint --fix --ignore '**/node_modules/**' --ignore '**/CHANGELOG.md' ${c(r)}`]:[],...f?[`${n} exec markdownlint-cli2 --fix '!**/node_modules/**' '!**/CHANGELOG.md' ${c(r)}`]:[]],"**/*.md"),"**/*.mdx":h(r=>[...p?[`${n} exec prettier --write ${c(r)}`]:[]],"**/*.mdx")}),e.secretlint!==!1&&a(s,["secretlint"])&&(i["**/*"]=r=>[`${n} exec secretlint ${c(r)}`]),e.stylesheets!==!1&&a(s,["stylelint"])){if(!Array.isArray(e.stylesheets.extensions)||e.stylesheets.extensions.length===0)throw new Error("The `extensions` option is required for the Stylesheets configuration.");i[`**/*.{${e.stylesheets.extensions.join(",")}}`]=r=>[...p?[`${n} exec prettier --ignore-unknown --write ${c(r)}`]:[],`${n} exec stylelint --fix`]}if(e.typescript!==!1&&a(s,["typescript"])){if(!Array.isArray(e.typescript.extensions)||e.typescript.extensions.length===0)throw new Error("The `extensions` option is required for the TypeScript configuration.");i[`**/*.{${e.typescript.extensions.join(",")}}`]=r=>{const x=new Set;return r.forEach(g=>{if(typeof e.typescript?.exclude=="object"&&Array.isArray(e.typescript.exclude)){let u=!1;if(e.typescript.exclude.forEach(b=>{!u&&g.includes(b)&&(u=!0)}),u){e.debug&&console.info(`Skipping ${g} as it's excluded in the settings.`);return}}try{const u=w("tsconfig.json",g);x.add(`${n} exec tsc --noEmit --project ${u}`)}catch(u){e.debug&&console.error(u)}}),[...x]}}return a(s,["vitest"])&&(i["**/?(*.){test,spec}.?(c|m)[jt]s?(x)"]=["vitest related --run"],i["**/__tests__/**/*.?(c|m)[jt]s?(x)"]=[`${n} exec vitest related --run`]),a(s,["jest"])&&(i["**/*.spec.{js,ts,tsx}"]=[`${n} exec jest --findRelatedTests`],i["**/*.test.{js,ts,tsx}"]=[`${n} exec jest --findRelatedTests`],i["**/?(*.){test,spec}.?(c|m)[jt]s?(x)"]=[`${n} exec jest --findRelatedTests`],i["**/__mocks__/*.{js,ts,tsx}"]=[`${n} exec jest --findRelatedTests`],i["**/__tests__/**/*.?(c|m)[jt]s?(x)"]=[`${n} exec jest --findRelatedTests`],i["**/__tests__/*.{js,ts,tsx}"]=[`${n} exec jest --findRelatedTests`],i["**/test/*.{js,ts,tsx}"]=[`${n} exec jest --findRelatedTests`]),a(s,["ava"])&&(i["**/(test|tests|__tests__)/**/*.js"]=r=>[`${n} exec ava ${c(r)}`],i["**/*.(spec|test).js"]=r=>[`${n} exec ava ${c(r)}`],i["**/test.js"]=r=>[`${n} exec ava ${c(r)}`],i["**/test-*.js"]=r=>[`${n} exec ava ${c(r)}`]),i},"defineConfig");export{ce as defineConfig,V as eslintExtensions,Z as stylesheetsExtensions,X as typescriptExtensions};