UNPKG

@anolilab/lint-staged-config

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