UNPKG

@dovenv/lint

Version:

Lint configuration for dovenv

7 lines (5 loc) 7.89 kB
import{LazyLoader as g,deepmergeCustom as k,catchError as E,execChild as I,resolvePath as v}from"@dovenv/core/utils";const F="https://dovenv.pigeonposse.com/guide/plugin/lint",s={staged:"staged",stylelint:"stylelint",eslint:"eslint",commitlint:"commitlint",publint:"publint",custom:"custom"};class u{constructor(t,i){this.opts=t,this.utils=i,this.utils.title="lint",this.utils.helpURL=F}transformHelpInfo(t){this.utils.title=this.utils.title+"."+t,this.utils.helpURL=this.utils.helpURL+"#"+t}}const p=new g({"@commitlint/format":async()=>(await import("@commitlint/format")).default,"@commitlint/lint":async()=>(await import("@commitlint/lint")).default,"@commitlint/read":async()=>(await import("@commitlint/read")).default,"@commitlint/load":async()=>(await import("@commitlint/load")).default,"commitlint-config-gitmoji":async()=>(await import("commitlint-config-gitmoji")).default});class y extends u{#t=t=>{if(typeof t=="object"&&typeof t.parserOpts=="object")return t.parserOpts};async#s(t){const i=await p.get("@commitlint/format"),o=await p.get("@commitlint/lint"),n=await p.get("@commitlint/load"),e=await p.get("@commitlint/read"),l=this.opts?.config?this.opts?.config:{rules:{"header-max-length":[0,"always",100]}},c=k({mergeArrays:!1})(l,this.opts?.gitmoji?await p.get("commitlint-config-gitmoji"):{}),a=await n(c);console.debug("config",a);const m=t?[t]:await e({edit:!0});console.debug("result",m);const d=m[0];this.utils.prompt.log.info(this.utils.style.info.msg("Commit message to lint",d));const f=await o(d,a.rules,{parserOpts:this.#t(a.parserPreset),plugins:a.plugins,ignores:a.ignores,defaultIgnores:a.defaultIgnores});console.debug("report",f);const h=i({results:[f]});console.debug("formated response",h),h&&h!==""&&this.utils.prompt.log.errorWithExit(h),f.valid&&(this.utils.prompt.log.success(this.utils.style.success.h("Commit format is valid!")),this.utils.prompt.log.message(""))}async run(t){return this.transformHelpInfo(s.commitlint),await this.utils.catchFn(this.#s(t))}}class w extends u{async#t(t){const i=a=>{const{process:m,style:d}=this.utils;a?.stdout&&m.stdout.write(a.stdout),a?.stderr&&m.stderr.write(a?.stderr),a?.stdout?.trim()===""&&a?.stderr?.trim()===""&&this.utils.prompt.log.success(d.success.h(`Succesfully linted! `))},o=["eslint",...new Set([...t||[],...this.opts?.flags||[],"--color"])].join(" ");console.debug({extraFlags:t,cmd:o}),this.utils.prompt.log.info(this.utils.style.info.msg("ESLINT",`Exec: ${o} `));const[n,e]=await E(I(o)),l=e?.stderr&&e?.stderr.trim()===""?void 0:e?.stderr,c=e?.stdout&&e?.stdout.trim()===""?void 0:e?.stdout;console.debug({error:n,stderr:l,stdout:c}),n?(typeof n=="object"&&"stdout"in n&&"stderr"in n?i({stdout:n.stdout,stderr:n.stderr}):console.error("Unexpected error running eslint",n?.message||n),this.utils.exitWithError()):i(e)}async run(t){return this.transformHelpInfo(s.eslint),await this.utils.catchFn(this.#t(t))}}const b=new g({publint:async()=>(await import("publint")).publint,formatMessage:async()=>(await import("publint/utils")).formatMessage});class L extends u{async#t(t){const i=await b.get("publint"),o=await b.get("formatMessage"),{messages:n,pkg:e}=await i(t),l=t?.pkgDir?v(t?.pkgDir):this.utils.process.cwd();this.utils.prompt.log.info(this.utils.style.info.badge(t?.title||"lint")+" "+this.utils.style.info.p(l));for(const c of n){const a=o(c,e)||"No output message";c.type==="error"&&this.utils.prompt.log.errorWithExit(a),c.type==="warning"?this.utils.prompt.log.warn(a):this.utils.prompt.log.message(this.utils.style.warn.h(`recommendation: `)+a)}this.utils.prompt.log.success(this.utils.style.success.h(`Succefully linted! `))}async#s(t){if(!await this.utils.ensureOpts({input:this.opts})||!this.opts)return;console.debug({publintConf:this.opts});const i=await this.utils.getOptsKeys({input:this.opts,pattern:typeof t=="string"?[t]:t});if(i)for(const o of i)await this.#t({...this.opts[o],title:this.opts[o].title||o})}async runOne(t){await this.#t(t)}async run(t){this.transformHelpInfo(s.publint),await this.#s(t)}}const M=new g({"lint-staged":async()=>(await import("lint-staged")).default});class S extends u{async#t(){if(await this.utils.ensureOpts({input:this.opts})&&(console.debug({lintStagedConf:this.opts}),!await(await M.get("lint-staged"))({config:this.opts})))throw Error("Unsuccessful response \u{1F622}")}async run(){return this.transformHelpInfo(s.staged),await this.utils.catchFn(this.#t())}}const x=new g({stylelint:async()=>(await import("stylelint")).default,formatter:async()=>(await import("stylelint-formatter-pretty")).default});class $ extends u{async#t(t,i){if(this.opts={...this.opts,...t?{files:t}:{},...i?{fix:!0}:{}},this.opts=Object.keys(this.opts).length?this.opts:void 0,!await this.utils.ensureOpts({input:this.opts}))return;const o=await x.get("stylelint"),n=await x.get("formatter"),e=await o.lint({...this.opts,formatter:n});e.errored?console.error(e.report):console.log("\u2728 No errors found")}async run(t,i){return this.transformHelpInfo(s.stylelint),await this.utils.catchFn(this.#t(t,i))}}class C extends u{#t(t){return new w(t,this.utils)}#s(t){return new y(t,this.utils)}#i(t){return new $(t,this.utils)}#e(t){return new S(t,this.utils)}#n(t){return new L(t,this.utils)}async eslint(t){return await this.#t(this.opts?.eslint).run(t)}async commitlint(t){await this.#s(this.opts?.commitlint).run(t)}async stylelint(t,i){await this.#i(this.opts?.stylelint).run(t,i)}async publint(t){await this.#n(this.opts?.publint).run(t)}async staged(){await this.#e(this.opts?.staged).run()}async#o(t){const i=this.opts?.custom,o=await this.utils.getOptsKeys({input:i,pattern:t,name:this.utils.title+".custom"});if(!(!o||!i))for(const n of o){console.log(this.utils.style.info.h(`Custom lint for ${this.utils.style.badge(n)} key`));const e=i[n];await e({utils:this.utils,run:{[s.publint]:async l=>await this.#n({}).runOne(l),[s.eslint]:async l=>await this.#t(l).run(),[s.commitlint]:async l=>await this.#s(l).run(),[s.stylelint]:async l=>await this.#i(l).run(),[s.staged]:async l=>await this.#e(l).run()}})}}async custom(t){await this.#o(t)}}const O=r=>({custom:{lint:{desc:"Linter tools",cmds:{[s.staged]:{desc:"Lint staged git files"},[s.stylelint]:{desc:"Lint CSS/SCSS/LESS/SASS/PostCSS files configuration",opts:{fix:{desc:"Fix stylelint errors",type:"boolean"},files:{desc:"Files to lint",type:"array"}}},[s.eslint]:{desc:"Lint JS/TS/MD/JSON/YAML.. files",opts:{fix:{desc:"Fix errors",type:"boolean"},flags:{desc:"Eslint custom flags",type:"array"}}},[s.commitlint]:{desc:"Lint commit messages",opts:{message:{desc:"Set your commit message here. If is not provided, it will be read the last commit message",type:"string",alias:"m"}},examples:[{cmd:`$0 lint ${s.commitlint} --message "Message to commit"`,desc:"Lint commit specific message"},{cmd:`$0 lint ${s.commitlint}`,desc:"Lint last commit last message"}]},[s.publint]:{desc:"PubLint",opts:{key:{alias:"k",desc:"Key pattern",type:"array"}}},[s.custom]:{desc:"Custom lint function",opts:{key:{alias:"k",desc:"Key pattern",type:"array"}}}},examples:[{cmd:`$0 lint ${s.stylelint}`,desc:"Lint css/postcss files"},{cmd:`$0 lint ${s.eslint}`,desc:"Lint JS/TS/MD/JSON/YAML.. files"},{cmd:`$0 lint ${s.commitlint}`,desc:"Lint commit message"},{cmd:`$0 lint ${s.publint}`,desc:"PubLint (Lint if a package is published right)"},{cmd:`$0 lint ${s.staged}`,desc:"Lint staged files"}],fn:async({opts:t,cmds:i,showHelp:o,utils:n})=>{const e=new C(r,n);i?.includes(s.staged)?await e.staged():i?.includes(s.stylelint)?await e.stylelint(t?.files,t?.fix):i?.includes(s.eslint)?await e.eslint(t?.fix?["--fix"]:t?.flags?t?.flags:[]):i?.includes(s.commitlint)?await e.commitlint(t?.message):i?.includes(s.publint)?await e.publint(t?.key):i?.includes(s.custom)?await e.custom(t?.key):o()}}}});export{y as CommitLint,w as Eslint,C as Lint,L as PubLint,S as StagedLint,$ as StyleLint,O as default,O as lintPlugin};