UNPKG

@dovenv/lint

Version:
3 lines (2 loc) 5.82 kB
import*as S from"@dovenv/eslint-config";export{S as dovenvEslintConfig};import*as x from"@dovenv/stylelint-config";export{x as dovenvStylelintConfig};import L from"@commitlint/format";import C from"@commitlint/lint";import b from"@commitlint/load";import $ from"@commitlint/read";import{deepmergeCustom as v,catchError as F,execChild as O}from"@dovenv/core/utils";import I from"commitlint-config-gitmoji";import j from"lint-staged";import E from"stylelint";import H from"stylelint-formatter-pretty";const M="https://dovenv.pigeonposse.com/guide/plugin/lint",s={staged:"staged",stylelint:"stylelint",eslint:"eslint",commitlint:"commitlint",custom:"custom"};class u{constructor(t,i){this.opts=t,this.utils=i,this.utils.title="lint",this.utils.helpURL=M}transformHelpInfo(t){this.utils.title=this.utils.title+"."+t,this.utils.helpURL=this.utils.helpURL+"#"+t}}class d extends u{#t=t=>{if(typeof t=="object"&&typeof t.parserOpts=="object")return t.parserOpts};async#s(t){const i=this.opts?.config?this.opts?.config:{rules:{"header-max-length":[0,"always",100]}},n=v({mergeArrays:!1})(i,this.opts?.gitmoji?I:{}),e=await b(n);console.debug("config",e);const o=t?[t]:await $({edit:!0});console.debug("result",o);const r=o[0];this.utils.prompt.log.info(this.utils.style.info.msg("Commit message to lint",r));const c=await C(r,e.rules,{parserOpts:this.#t(e.parserPreset),plugins:e.plugins,ignores:e.ignores,defaultIgnores:e.defaultIgnores});console.debug("report",c);const l=L({results:[c]});console.debug("formated response",l),l&&l!==""&&this.utils.prompt.log.error(l),c.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 p extends u{async#t(t){const i=l=>{const{process:m,style:w}=this.utils;l?.stdout&&m.stdout.write(l.stdout),l?.stderr&&m.stderr.write(l?.stderr),l?.stdout?.trim()===""&&l?.stderr?.trim()===""&&m.stdout.write(w.success.h(`Succesfully linted! `))};t=this.opts?.flags?[...!t||t.length?[]:t,...this.opts.flags]:t;const n=`eslint ${t?.join(" ")||""}${t?.includes("--color")?" ":" --color"}`;console.debug({cmd:n});const[e,o]=await F(O(n)),r=o?.stderr&&o?.stderr.trim()===""?void 0:o?.stderr,c=o?.stdout&&o?.stdout.trim()===""?void 0:o?.stdout;console.debug({error:e,stderr:r,stdout:c}),e?(typeof e=="object"&&"stdout"in e&&"stderr"in e?i({stdout:e.stdout,stderr:e.stderr}):console.error("Unexpected error running eslint",e?.message||e),this.utils.exitWithError()):i(o)}async run(t){return this.transformHelpInfo(s.eslint),await this.utils.catchFn(this.#t(t))}}class h extends u{async#t(){if(await this.utils.ensureOpts({input:this.opts})&&(console.debug({lintStagedConf:this.opts}),!await j({config:this.opts,shell:!0})))throw Error("Unsuccessful response \u{1F622}")}async run(){return this.transformHelpInfo(s.staged),await this.utils.catchFn(this.#t())}}class f 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 n=await E.lint({...this.opts,formatter:H});n.errored?console.error(n.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 g extends u{#t(t){return new p(t,this.utils)}#s(t){return new d(t,this.utils)}#i(t){return new f(t,this.utils)}#e(t){return new h(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 staged(){await this.#e(this.opts?.staged).run()}async#o(t){const i=this.opts?.custom,n=await this.utils.getOptsKeys({input:i,pattern:t,name:this.utils.title+".custom"});if(!(!n||!i))for(const e of n){console.log(this.utils.style.info.h(`Custom lint for ${this.utils.style.badge(e)} key`));const o=i[e];await o({config:this.utils.config||{},run:{[s.eslint]:async r=>await this.#t(r).run(),[s.commitlint]:async r=>await this.#s(r).run(),[s.stylelint]:async r=>await this.#i(r).run(),[s.staged]:async r=>await this.#e(r).run()}})}}async custom(t){return await this.utils.catchFn(this.#o(t))}}const y=a=>({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"}}},[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.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.staged}`,desc:"Lint staged files"}],fn:async({opts:t,cmds:i,showHelp:n,utils:e})=>{const o=new g(a,e);i?.includes(s.staged)?await o.staged():i?.includes(s.stylelint)?await o.stylelint(t?.files,t?.fix):i?.includes(s.eslint)?await o.eslint(t?.fix?["--fix"]:[]):i?.includes(s.commitlint)?await o.commitlint(t?.message):i?.includes(s.custom)?await o.custom(t?.key):n()}}}});export{d as CommitLint,p as Eslint,g as Lint,h as StagedLint,f as StyleLint,y as default,y as lintPlugin};