mejora 2.3.5 → 2.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/run.mjs +19 -19
- package/package.json +1 -1
package/dist/run.mjs
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{d as e,f as t,g as n,m as r,u as i}from"./typescript-JQUGT569.mjs";import{t as a}from"./check-registry-BJlMzCIV.mjs";import{fileURLToPath as o}from"node:url";import{mkdir as s,readFile as c,writeFile as l}from"node:fs/promises";import{inspect as u,parseArgs as d,styleText as f}from"node:util";import{Tinypool as p}from"tinypool";import{env as m}from"node:process";const h=e=>t=>f(e,typeof t==`number`?t.toString():t),g=h(`blue`),_=h(`bold`),
|
|
3
|
-
`).slice(1).map(e=>
|
|
2
|
+
import{d as e,f as t,g as n,m as r,u as i}from"./typescript-JQUGT569.mjs";import{t as a}from"./check-registry-BJlMzCIV.mjs";import{fileURLToPath as o}from"node:url";import{mkdir as s,readFile as c,writeFile as l}from"node:fs/promises";import{inspect as u,parseArgs as d,styleText as f}from"node:util";import{Tinypool as p}from"tinypool";import{env as m}from"node:process";const h=e=>t=>f(e,typeof t==`number`?t.toString():t),g=h(`blue`),_=h(`bold`),v=h(`cyan`),y=h(`dim`),b=h(`green`),x=h(`red`),S=h(`gray`),ee=h(`underline`),te=h(`yellow`);function ne(e){let t=[e.message];if(e.stack){let n=e.stack.split(`
|
|
3
|
+
`).slice(1).map(e=>y(e.trim())).join(`
|
|
4
4
|
`);t.push(n)}return t.join(`
|
|
5
|
-
`)}function
|
|
6
|
-
`)}function
|
|
7
|
-
`)}function
|
|
8
|
-
`);let o=
|
|
9
|
-
`)}function
|
|
5
|
+
`)}function C(...e){return e.map(e=>typeof e==`string`?e:e instanceof Error?ne(e):u(e,{colors:!1,depth:10})).join(` `)}const w={error:(...e)=>{console.error(x(`✖`),C(...e))},log:(...e)=>{console.log(C(...e))},start:(...e)=>{console.log(v(`◐`),C(...e))},success:(...e)=>{console.log(b(`✔`),C(...e))}};function T(e,t){return e.file===t.file?e.line===t.line?e.column-t.column:e.line-t.line:e.file.localeCompare(t.file)}function re(e){let t=Map.groupBy(e,e=>e.signature),n=[];for(let[e,r]of t){r.sort(T);for(let[t,{signature:a,...o}]of r.entries())n.push({...o,id:i(`${e}:${t}`)})}return n}function E(e){return{items:re(e.items.map(e=>({...e,signature:`${e.file} - ${e.rule}: ${e.message}`}))).toSorted(T),type:`items`}}function D(e,t){return e===1?t:`${t}s`}const O=`__unparsable__`;function k(e){return e.replaceAll(`<`,`<`).replaceAll(`>`,`>`).replaceAll(`[`,`[`).replaceAll(`]`,`]`)}function A(e,t,n){let i=r(t,e);return n?`${i}#L${n}`:i}function j(e,t){return`[${e}](${t})`}function ie(e,t){let n=A(e.file,t,e.line);return`- ${j(e.line?`Line ${e.line}`:e.file,n)} - ${`${e.rule}: ${k(e.message)}`}`}function ae(e){let t=Object.groupBy(e,e=>e.file||O);return Object.entries(t).map(([e,t=[]])=>({filePath:e,items:t})).toSorted((e,t)=>e.filePath===O?1:t.filePath===O?-1:e.filePath.localeCompare(t.filePath))}function oe(e,t){let n=e.length,r=D(n,`issue`),i=[`\n### Other Issues · ${t}\n`];for(let t of e)i.push(`- ${t.rule}: ${k(t.message)}`);return i.push(`\n${n} ${r} in Other Issues`,``),i.join(`
|
|
6
|
+
`)}function M(e,t,n){if(e.filePath===O)return oe(e.items,n);let r=A(e.filePath,t),i=j(e.filePath,r),a=e.items.length,o=D(a,`issue`),s=[`\n### ${i} · ${n}\n`];for(let n of e.items)s.push(ie(n,t));return s.push(`\n${a} ${o} in ${e.filePath}`,``),s.join(`
|
|
7
|
+
`)}function se(e,t,n){let r=t.length,i=D(r,`issue`),a=[`\n## ${e}\n`];if(t.length===0)return a.push(`No issues`),a.join(`
|
|
8
|
+
`);let o=ae(t);for(let t of o)a.push(M(t,n,e));return a.push(`---\n${r} total ${i} for ${e}`),a.join(`
|
|
9
|
+
`)}function ce(e){return`${e.replaceAll(/\n{3,}/g,`
|
|
10
10
|
|
|
11
|
-
`).trimEnd()}\n`}function
|
|
11
|
+
`).trimEnd()}\n`}function N(e,t){let n=[`<!-- prettier-ignore-start -->
|
|
12
12
|
`,`# Mejora Baseline
|
|
13
|
-
`,`This file represents the current accepted state of the codebase.`];for(let[r,{items:i=[]}]of Object.entries(e.checks))n.push(
|
|
14
|
-
<!-- prettier-ignore-end -->`),
|
|
15
|
-
`))}function ue(){return{hasImprovement:!1,hasRegression:!1,hasRelocation:!1,isInitial:!0,newIssues:[],removedIssues:[]}}function de(e,t,n){return{hasImprovement:t.length>0,hasRegression:e.length>0,hasRelocation:n,isInitial:!1,newIssues:e.toSorted((e,t)=>e.id.localeCompare(t.id)),removedIssues:t.toSorted((e,t)=>e.id.localeCompare(t.id))}}function
|
|
16
|
-
}`.repeat(t)}`}function
|
|
13
|
+
`,`This file represents the current accepted state of the codebase.`];for(let[r,{items:i=[]}]of Object.entries(e.checks))n.push(se(r,i,t));return n.push(`
|
|
14
|
+
<!-- prettier-ignore-end -->`),ce(n.join(`
|
|
15
|
+
`))}const le=[`CI`,`CONTINUOUS_INTEGRATION`].some(e=>m[e]&&m[e]!==`0`&&m[e]!==`false`);function ue(){return{hasImprovement:!1,hasRegression:!1,hasRelocation:!1,isInitial:!0,newIssues:[],removedIssues:[]}}function de(e,t,n){return{hasImprovement:t.length>0,hasRegression:e.length>0,hasRelocation:n,isInitial:!1,newIssues:e.toSorted((e,t)=>e.id.localeCompare(t.id)),removedIssues:t.toSorted((e,t)=>e.id.localeCompare(t.id))}}function P(e=[]){return new Map(e.map(e=>[e.id,e]))}function F(e){return new Set(e.keys())}function I(e,t){let n=[];for(let r of t){let t=e.get(r);n.push(t)}return n}function L(e,t){for(let[n,r]of e){let e=t.get(n);if(e&&(r.line!==e.line||r.column!==e.column))return!0}return!1}function R(e,t){let n=P(e.items),r=P(t.items),i=F(n),a=F(r),o=i.difference(a),s=a.difference(i);return de(I(n,o),I(r,s),i.size>o.size?L(n,r):!1)}function z(e,t){return t?R(e,t):ue()}const B=(e,t)=>{if(!t)return!1;let n=e.items,r=t.items;if(n.length!==r.length)return!1;let i=n.toSorted((e,t)=>e.id.localeCompare(t.id)),a=r.toSorted((e,t)=>e.id.localeCompare(t.id));return i.every((e,t)=>{let n=a[t];return e.id===n.id&&e.file===n.file&&e.line===n.line&&e.column===n.column&&e.rule===n.rule&&e.message===n.message})};function V(e,t){let n=e;for(let e=0;e<t;e++){let e=n.trimEnd();if(!e.endsWith(`}`))break;n=e.slice(0,-1)}return n}function H(e,t){return`${e}${`
|
|
16
|
+
}`.repeat(t)}`}function fe(e){let t=(e.match(/\{/g)?.length??0)-(e.match(/\}/g)?.length??0);return t===0?e:t<0?V(e,-t):H(e,t)}function pe(e){try{return JSON.parse(e)}catch{return}}function U(e){let t=e.trim();return t.endsWith(`,`)?t.slice(0,-1):t}function me(e){return`{
|
|
17
17
|
"version": 2,
|
|
18
18
|
${U(e)}
|
|
19
|
-
}`}function W(e){if(typeof e!=`object`||!e)throw TypeError(`Baseline must be an object`);if(`checks`in e&&e.checks&&typeof e.checks==`object`)return{checks:e.checks,version:2};let t={};for(let[n,r]of Object.entries(e))n!==`version`&&(t[n]=r);return{checks:t,version:2}}function G(e){try{let t=
|
|
20
|
-
`,r=e.snapshot.items.length,i=[`${n}${g(`ℹ`)} ${e.checkId}:`,` Initial baseline created with ${g(r)} ${D(r,`issue`)}`];return e.snapshot.items.length>0&&i.push(...X(e.snapshot.items,`initial`)),i.push(``,...Z(e)),i}function
|
|
21
|
-
`}${e.hasRegression?
|
|
22
|
-
`,r=e.snapshot.items.length;return e.duration===void 0?[`${n}${
|
|
23
|
-
`)}function
|
|
24
|
-
`)}const{values:$}=d({allowPositionals:!1,options:{force:{default:!1,short:`f`,type:`boolean`},help:{short:`h`,type:`boolean`},json:{default:!1,type:`boolean`},only:{type:`string`},skip:{type:`string`}},strict:!0});$.help&&(
|
|
19
|
+
}`}function W(e){if(typeof e!=`object`||!e)throw TypeError(`Baseline must be an object`);if(`checks`in e&&e.checks&&typeof e.checks==`object`)return{checks:e.checks,version:2};let t={};for(let[n,r]of Object.entries(e))n!==`version`&&(t[n]=r);return{checks:t,version:2}}function G(e){try{let t=pe(e.trim());if(t)return W(t);let n=me(fe(U(e)));return W(JSON.parse(n))}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to parse baseline during conflict resolution: ${t}`,{cause:e})}}function he(e){let t=[...e.matchAll(/<<<<<<< .*\n([\s\S]*?)\n=======\n([\s\S]*?)\n>>>>>>> .*$/gm)];if(t.length===0)throw Error(`Could not parse conflict markers in baseline`);return t.map(([,e=``,t=``])=>({ours:e,theirs:t}))}function ge(e){let t=new Map;for(let n of e)for(let[e,{items:r=[]}]of Object.entries(n.checks)){if(r.length===0)continue;let n=t.get(e);n||(n=new Map,t.set(e,n));for(let e of r)n.set(e.id,e)}let n={};for(let[e,r]of t)n[e]={items:[...r.values()].toSorted((e,t)=>e.id.localeCompare(t.id)),type:`items`};return{checks:n,version:2}}function _e(e){let t=he(e),n=[];for(let{ours:e,theirs:r}of t)n.push(G(e),G(r));return ge(n)}var K=class e{baselinePath;constructor(e=`.mejora/baseline.json`){this.baselinePath=e}static create(e){return{checks:e,version:2}}static getEntry(e,t){return e?.checks[t]}static update(t,n,r){let i=t??e.create({}),a=i.checks[n];return B(r,a)?i:{...i,checks:{...i.checks,[n]:r}}}async load(){try{let e=await c(this.baselinePath,`utf8`);if(e.includes(`<<<<<<<`)){w.start(`Merge conflict detected in baseline, auto-resolving...`);let t=_e(e);return await this.save(t,!0),w.success(`Baseline conflict resolved`),t}return await this.resolveMarkdownConflictIfNeeded(e),JSON.parse(e)}catch(e){if(e.code===`ENOENT`)return null;throw e}}async save(e,n=!1){if(le&&!n)return;let r=`${JSON.stringify(e,null,2)}\n`,i=this.baselinePath.replace(`.json`,`.md`),a=N(e,t(this.baselinePath));await s(t(this.baselinePath),{recursive:!0}),await Promise.all([l(this.baselinePath,r,`utf8`),l(i,a,`utf8`)])}async resolveMarkdownConflictIfNeeded(e){let n=this.baselinePath.replace(`.json`,`.md`);try{(await c(n,`utf8`)).includes(`<<<<<<<`)&&(w.start(`Merge conflict detected in markdown report, regenerating...`),await l(n,N(JSON.parse(e),t(this.baselinePath)),`utf8`),w.success(`Markdown report regenerated`))}catch{}}};const ve=o(new URL(`workers/check.mjs`,import.meta.url));var ye=class e{baselineManager;registry;constructor(e,t){this.registry=e,this.baselineManager=new K(t)}static async executeChecksParallel(e,t){let n=Object.keys(e),r=new p({filename:ve});try{let e=n.map(async e=>{let n=await r.run({checkId:e}),i=E(n.snapshot),a=K.getEntry(t,e),o=z(i,a);return{baseline:a,checkId:e,duration:n.duration,hasImprovement:o.hasImprovement,hasRegression:o.hasRegression,hasRelocation:o.hasRelocation,isInitial:o.isInitial,newIssues:o.newIssues,removedIssues:o.removedIssues,snapshot:i}});return await Promise.all(e)}catch(e){return w.error(`Parallel execution failed:`,e),null}finally{await r.destroy()}}static filterChecks=(t,n)=>{let r=n.only?e.resolveRegex(n.only,`--only`):null,i=n.skip?e.resolveRegex(n.skip,`--skip`):null;return!r&&!i?t:Object.fromEntries(Object.entries(t).filter(([e])=>!(r&&!r.test(e)||i?.test(e))))};static resolveRegex(e,t){try{return new RegExp(e)}catch{throw Error(`Invalid regex pattern for ${t}: "${e}"`)}}async run(t,n={}){let r=performance.now(),i=await this.baselineManager.load(),a=e.filterChecks(t.checks,n),o=Object.keys(a).length;w.start(`Running ${o} check${o===1?``:`s`}...`);let s=o>1?await e.executeChecksParallel(a,i):await this.executeChecksSequential(a,i);if(!s)return{exitCode:2,hasImprovement:!1,hasRegression:!0,results:[],totalDuration:performance.now()-r};let c=i,l=!1,u=!1,d=!1;for(let e of s)e.hasRegression&&(l=!0),e.hasImprovement&&(u=!0),e.isInitial&&(d=!0),(e.hasImprovement||e.hasRelocation||n.force||e.isInitial)&&(c=K.update(c,e.checkId,{items:e.snapshot.items,type:e.snapshot.type}));return c&&c!==i&&(!l||n.force||d)&&await this.baselineManager.save(c,n.force),{exitCode:l&&!n.force?1:0,hasImprovement:u,hasRegression:l,results:s,totalDuration:performance.now()-r}}async executeChecksSequential(e,t){try{let t=a.getRequiredTypes(e);await Promise.all([this.registry.setup(t),this.registry.validate(t)])}catch(e){return w.error(`Setup failed:`,e),null}let n=[];for(let[r,i]of Object.entries(e))try{let e=performance.now(),a=await this.registry.get(i.type).run(i),o=performance.now()-e,s=E(a),c=K.getEntry(t,r),l=z(s,c);n.push({baseline:c,checkId:r,duration:o,hasImprovement:l.hasImprovement,hasRegression:l.hasRegression,hasRelocation:l.hasRelocation,isInitial:l.isInitial,newIssues:l.newIssues,removedIssues:l.removedIssues,snapshot:s})}catch(e){return w.error(`Error running check "${r}":`,e),null}return n}};function q(e,t){if(!(e===void 0||t===0))return e/t}function be(e){let{results:t,totalDuration:n}=e,r=t.length,i=[],a=[],o=[],s=[],c=0,l=0,u=0,d=0,f=0,p=[];for(let e of t){let t=e.snapshot.items.length;f+=t,e.isInitial?(u+=t,o.push(e.checkId)):(e.hasImprovement&&(c+=e.removedIssues.length,i.push(e.checkId)),e.hasRegression&&(l+=e.newIssues.length,a.push(e.checkId)),!e.hasImprovement&&!e.hasRegression&&(d+=t,s.push(e.checkId))),p.push({checkId:e.checkId,duration:e.duration,hasImprovement:e.hasImprovement,hasRegression:e.hasRegression,isInitial:e.isInitial,newIssues:e.newIssues,removedIssues:e.removedIssues,totalIssues:t})}let m={checks:p,exitCode:e.exitCode,hasImprovement:e.hasImprovement,hasRegression:e.hasRegression,summary:{avgDuration:q(n,r),checksRun:r,improvementChecks:i,improvements:c,initial:u,initialChecks:o,regressionChecks:a,regressions:l,totalIssues:f,unchanged:d,unchangedChecks:s},totalDuration:n};return JSON.stringify(m,null,2)}function xe(e){if(e<1e3)return`${e}ms`;let t=e/1e3;if(t<60)return t%1==0?`${t}s`:`${t.toFixed(1)}s`;let n=e/6e4;if(n<60)return n%1==0?`${n}m`:`${n.toFixed(1)}m`;let r=e/36e5;return r%1==0?`${r}h`:`${r.toFixed(1)}h`}function J(e){let t=Math.round(e);return t<1?`<1ms`:xe(t)}const Y=` `,Se=`${Y} `;function Ce(e){return e===`initial`?y(`→`):e===`improvement`?b(`↑`):x(`↓`)}function we(n,r,i){let a=t(n),o=e(n),s=r>0?`:${r}:${i>0?i:1}`:``;return`${a===`.`?``:y(`${a}/`)}${ee(o)}${s?y(s):``}`}function Te(e,t){return[`${Ce(t)} ${we(e.file,e.line,e.column)} ${y(e.rule)}`,e.message]}function X(e,t){let n=[],r=e.slice(0,10);for(let e of r){let[r,i]=Te(e,t);n.push(`${Y}${r}`,`${Se}${i}`)}let i=e.length-r.length;return i>0&&n.push(`${Y}${y(`... and ${i} more`)}`),n}function Ee(e){return e===void 0?[]:[` ${y(`Duration`)} ${J(e)}`]}function De(e){return[` ${y(`Issues`)} ${_(e)}`]}function Z(e){return[...Ee(e.duration),...De(e.snapshot.items.length)]}function Oe(e){if(!e.hasRegression)return[];let t=e.newIssues.length;return[` ${x(t)} new ${D(t,`issue`)} (${D(t,`regression`)}):`,...X(e.newIssues,`regression`)]}function Q(e){if(!e.hasImprovement)return[];let t=e.removedIssues.length;return[` ${b(t)} ${D(t,`issue`)} fixed (${D(t,`improvement`)}):`,...X(e.removedIssues,`improvement`)]}function ke(e,t){let n=t?``:`
|
|
20
|
+
`,r=e.snapshot.items.length,i=[`${n}${g(`ℹ`)} ${e.checkId}:`,` Initial baseline created with ${g(r)} ${D(r,`issue`)}`];return e.snapshot.items.length>0&&i.push(...X(e.snapshot.items,`initial`)),i.push(``,...Z(e)),i}function Ae(e,t){return[`${t?``:`
|
|
21
|
+
`}${e.hasRegression?x(`✖`):b(`✔`)} ${e.checkId}:`,...Oe(e),...Q(e),``,...Z(e)]}function je(e,t){let n=t?``:`
|
|
22
|
+
`,r=e.snapshot.items.length;return e.duration===void 0?[`${n}${S(`ℹ`)} ${e.checkId} (${_(r)})`]:[`${n}${S(`ℹ`)} ${e.checkId} (${_(r)}) ${y(J(e.duration))}`]}function Me(e,t){return e.isInitial?ke(e,t):e.hasRegression||e.hasImprovement?Ae(e,t):je(e,t)}function Ne(e,t,n){return t?g(`✔ Initial baseline created successfully`):e.hasRegression?n?te(`⚠ Regressions detected (forced)`):`${x(`✗ Regressions detected`)} - Run failed`:e.hasImprovement?`${b(`✔ Improvements detected`)} - Baseline updated`:b(`✔ All checks passed`)}function Pe(e,t){let n={hasAnyInitial:!1,totalImprovements:0,totalInitial:0,totalIssues:0,totalRegressions:0};for(let t of e.results){let e=t.snapshot.items.length;if(n.totalIssues+=e,t.isInitial){n.hasAnyInitial=!0,n.totalInitial+=e;continue}let{hasImprovement:r,hasRegression:i}=t;r&&(n.totalImprovements+=t.removedIssues.length),i&&(n.totalRegressions+=t.newIssues.length)}let r=[` ${y(`Improvements`)} ${b(n.totalImprovements)}`,` ${y(`Regressions`)} ${x(n.totalRegressions)}`,` ${y(`Initial`)} ${g(n.totalInitial)}`,` ${y(`Checks`)} ${e.results.length}`,` ${y(`Issues`)} ${_(n.totalIssues)}`],i=q(e.totalDuration,e.results.length);return e.totalDuration!==void 0&&i!==void 0&&r.push(` ${y(`Duration`)} ${J(e.totalDuration)} ${S(`(avg ${J(i)})`)}`),r.push(``,Ne(e,n.hasAnyInitial,t)),r.join(`
|
|
23
|
+
`)}function Fe(e,t){let n=[];for(let[t,r]of e.results.entries())n.push(...Me(r,t===0));return n.length>0&&n.push(``),n.push(Pe(e,t)),n.join(`
|
|
24
|
+
`)}const{values:$}=d({allowPositionals:!1,options:{force:{default:!1,short:`f`,type:`boolean`},help:{short:`h`,type:`boolean`},json:{default:!1,type:`boolean`},only:{type:`string`},skip:{type:`string`}},strict:!0});$.help&&(w.log(`
|
|
25
25
|
mejora - Prevent regressions by allowing only improvement
|
|
26
26
|
|
|
27
27
|
Usage:
|
|
@@ -40,4 +40,4 @@ Examples:
|
|
|
40
40
|
mejora --json
|
|
41
41
|
mejora --only "eslint > *"
|
|
42
42
|
mejora --skip typescript
|
|
43
|
-
`),process.exit(0));try{let e=new a,t=await n();e.init(t);let r=await new
|
|
43
|
+
`),process.exit(0));try{let e=new a,t=await n();e.init(t);let r=await new ye(e).run(t,{force:$.force,json:$.json,only:$.only,skip:$.skip});$.json?w.log(be(r)):(w.log(``),w.log(Fe(r,$.force))),process.exit(r.exitCode)}catch(e){e instanceof Error?w.error(e.message):w.error(e),process.exit(2)}export{};
|