Setting the file. One moment.
Coverage Reconcile · Rp Mapper · wix/skills · Skills Docs
ContentsBack to the top of the page scripts/coverage-reconcile.js
scripts/ coverage-reconcile.js
JavaScript · 88 lines · 3 KB
15 // the same write, so the normal recovery path never creates a disposition conflict.
16 //
17 // Exit codes: 0 every row reconciles; 2 a row has no, two, a stale or a mismatched disposition, a
18 // gap/decision names an unknown coverageId, or the coverage file is missing or malformed;
19 // 1 usage error.
20
21 const path = require ( 'node:path' );
22 const {
23 reconcileProject ,
24 recordCoverageDecision ,
25 SourceCoverageError ,
26 } = require ( '../../../lib/source-coverage.js' );
27
28 function parseArgs ( argv ) {
29 const args = { command: null , projectDir: null , coverageId: null , value: null , rationale: null , json: false };
30 const positional = [];
31 for ( let i = 0 ; i < argv. length ; i += 1 ) {
32 const arg = argv[i];
33 if (arg === '--coverage-id' ) { args.coverageId = argv[i + 1 ]; i += 1 ; }
34 else if (arg === '--value' ) { args.value = argv[i + 1 ]; i += 1 ; }
35 else if (arg === '--rationale' ) { args.rationale = argv[i + 1 ]; i += 1 ; }
36 else if (arg === '--json' ) args.json = true ;
37 else positional. push (arg);
38 }
39 [args.command, args.projectDir] = positional;
40 return args;
41 }
42
43 function usage () {
44 return 'Usage: coverage-reconcile.js check <projectDir> [--json] \n '
45 + ' coverage-reconcile.js decide <projectDir> --coverage-id <id> --value provided|declined [--rationale <text>] \n ' ;
46 }
47
48 function report ( failures , json ) {
49 if (json) {
50 process.stdout. write ( `${ JSON . stringify ({ ok: failures. length === 0 , failures }, null , 2 ) } \n ` );
51 return ;
52 }
53 if ( ! failures. length ) {
54 process.stdout. write ( 'source coverage reconciles: every row has exactly one valid disposition \n ' );
55 return ;
56 }
57 for ( const item of failures) process.stderr. write ( `${ item . code }: ${ item . message } \n ` );
58 }
59
60 async function main () {
61 const args = parseArgs (process.argv. slice ( 2 ));
62 if ( ! [ 'check' , 'decide' ]. includes (args.command) || ! args.projectDir) {
63 process.stderr. write ( usage ());
64 return 1 ;
65 }
66 const projectDir = path. resolve (args.projectDir);
67 try {
68 if (args.command === 'decide' ) {
69 const result = await recordCoverageDecision (projectDir, { coverageId: args.coverageId, value: args.value, rationale: args.rationale });
70 process.stdout. write ( `${ JSON . stringify ({ ok: true , ... result }) } \n ` );
71 }
72 const result = await reconcileProject (projectDir);
73 report (result.failures, args.json);
74 return result.ok ? 0 : 2 ;
75 } catch (error) {
76 if (error instanceof SourceCoverageError ) {
77 const failures = error.failures. length ? error.failures : [{ code: 'invalid_coverage' , coverageId: null , message: error.message, sources: [] }];
78 report (failures, args.json);
79 return 2 ;
80 }
81 throw error;
82 }
83 }
84
85 main (). then (( code ) => { process.exitCode = code; }). catch (( error ) => {
86 process.stderr. write ( `${ error . stack || error . message } \n ` );
87 process.exitCode = 1 ;
88 });