-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit-base-command.ts
More file actions
785 lines (715 loc) · 31.1 KB
/
audit-base-command.ts
File metadata and controls
785 lines (715 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
import chalk from 'chalk';
import * as csv from 'fast-csv';
import { copy } from 'fs-extra';
import { v4 as uuid } from 'uuid';
import isEmpty from 'lodash/isEmpty';
import { join, resolve } from 'path';
import cloneDeep from 'lodash/cloneDeep';
import {
cliux,
sanitizePath,
TableFlags,
TableHeader,
log,
configHandler,
CLIProgressManager,
clearProgressModuleSetting,
readContentTypeSchemas,
} from '@contentstack/cli-utilities';
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'fs';
import config from './config';
import { print } from './util/log';
import { auditMsg } from './messages';
import { BaseCommand } from './base-command';
import {
Entries,
GlobalField,
ContentType,
Extensions,
Workflows,
Assets,
FieldRule,
ModuleDataReader,
CustomRoles,
ComposableStudio,
} from './modules';
import {
CommandNames,
ContentTypeStruct,
CtConstructorParam,
ModuleConstructorParam,
OutputColumn,
RefErrorReturnType,
WorkflowExtensionsRefErrorReturnType,
AuditContext,
} from './types';
export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseCommand> {
private currentCommand!: CommandNames;
private readonly summaryDataToPrint: Record<string, any> = [];
protected auditContext!: AuditContext;
get fixStatus() {
return {
fixStatus: {
minWidth: 7,
header: 'Fix Status',
get: (row: any) => {
return row.fixStatus === 'Fixed' ? chalk.greenBright(row.fixStatus) : chalk.redBright(row.fixStatus);
},
},
};
}
/**
* The `start` function performs an audit on content types, global fields, entries, and workflows and displays
* any missing references.
* @param {string} command - The `command` parameter is a string that represents the current command
* being executed.
*/
async start(command: CommandNames): Promise<boolean> {
this.currentCommand = command;
// Set progress supported module and console logs setting BEFORE any log calls
// This ensures the logger respects the setting when it's initialized
const logConfig = configHandler.get('log') || {};
// Default to false so progress bars are shown instead of console logs
if (logConfig.showConsoleLogs === undefined) {
configHandler.set('log.showConsoleLogs', false);
}
configHandler.set('log.progressSupportedModule', 'audit');
// Initialize global summary for progress tracking
CLIProgressManager.initializeGlobalSummary('AUDIT', '', 'Auditing content...');
await this.promptQueue();
await this.createBackUp();
this.sharedConfig.reportPath = resolve(this.flags['report-path'] || process.cwd(), 'audit-report');
log.debug(`Data directory: ${this.flags['data-dir']}`, this.auditContext);
log.debug(`Report path: ${this.flags['report-path'] || process.cwd()}`, this.auditContext);
const {
missingCtRefs,
missingGfRefs,
missingEntryRefs,
missingCtRefsInExtensions,
missingCtRefsInWorkflow,
missingSelectFeild,
missingMandatoryFields,
missingTitleFields,
missingRefInCustomRoles,
missingEnvLocalesInAssets,
missingEnvLocalesInEntries,
missingFieldRules,
missingMultipleFields,
missingRefsInComposableStudio,
} = await this.scanAndFix();
if (this.flags['show-console-output']) {
this.showOutputOnScreen([
{ module: 'Content types', missingRefs: missingCtRefs },
{ module: 'Global Fields', missingRefs: missingGfRefs },
{ module: 'Entries', missingRefs: missingEntryRefs },
]);
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Extensions', missingRefs: missingCtRefsInExtensions }]);
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Workflows', missingRefs: missingCtRefsInWorkflow }]);
this.showOutputOnScreenWorkflowsAndExtension([
{ module: 'Entries Select Field', missingRefs: missingSelectFeild },
]);
this.showOutputOnScreenWorkflowsAndExtension([
{ module: 'Entries Mandatory Field', missingRefs: missingMandatoryFields },
]);
this.showOutputOnScreenWorkflowsAndExtension([
{ module: 'Entries Title Field', missingRefs: missingTitleFields },
]);
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Custom Roles', missingRefs: missingRefInCustomRoles }]);
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Assets', missingRefs: missingEnvLocalesInAssets }]);
this.showOutputOnScreenWorkflowsAndExtension([
{ module: 'Entries Missing Locale and Environments', missingRefs: missingEnvLocalesInEntries },
]);
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Field Rules', missingRefs: missingFieldRules }]);
this.showOutputOnScreenWorkflowsAndExtension([
{ module: 'Entries Changed Multiple Fields', missingRefs: missingMultipleFields },
]);
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Studio', missingRefs: missingRefsInComposableStudio }]);
}
this.showOutputOnScreenWorkflowsAndExtension([{ module: 'Summary', missingRefs: this.summaryDataToPrint }]);
if (
!isEmpty(missingCtRefs) ||
!isEmpty(missingGfRefs) ||
!isEmpty(missingEntryRefs) ||
!isEmpty(missingCtRefsInWorkflow) ||
!isEmpty(missingCtRefsInExtensions) ||
!isEmpty(missingSelectFeild) ||
!isEmpty(missingTitleFields) ||
!isEmpty(missingRefInCustomRoles) ||
!isEmpty(missingEnvLocalesInAssets) ||
!isEmpty(missingEnvLocalesInEntries) ||
!isEmpty(missingFieldRules) ||
!isEmpty(missingMultipleFields) ||
!isEmpty(missingRefsInComposableStudio)
) {
if (this.currentCommand === 'cm:stacks:audit') {
log.warn(this.$t(auditMsg.FINAL_REPORT_PATH, { path: this.sharedConfig.reportPath }), this.auditContext);
} else {
log.warn(
this.$t(this.messages.FIXED_CONTENT_PATH_MAG, { path: this.sharedConfig.basePath }),
this.auditContext,
);
}
} else {
log.info(this.messages.NO_MISSING_REF_FOUND, this.auditContext);
cliux.print('');
if (
this.flags['copy-dir'] &&
this.currentCommand === 'cm:stacks:audit:fix' &&
existsSync(this.sharedConfig.basePath)
) {
// NOTE Clean up the backup dir if no issue found while audit the content
rmSync(this.sharedConfig.basePath, { recursive: true });
}
}
// Print comprehensive summary at the end (commented out - Summary table above has the counts; progress bars show completion)
// CLIProgressManager.printGlobalSummary();
// Clear progress module setting now that audit is complete
clearProgressModuleSetting();
return (
!isEmpty(missingCtRefs) ||
!isEmpty(missingGfRefs) ||
!isEmpty(missingEntryRefs) ||
!isEmpty(missingCtRefsInWorkflow) ||
!isEmpty(missingCtRefsInExtensions) ||
!isEmpty(missingSelectFeild) ||
!isEmpty(missingRefInCustomRoles) ||
!isEmpty(missingEnvLocalesInAssets) ||
!isEmpty(missingEnvLocalesInEntries) ||
!isEmpty(missingFieldRules) ||
!isEmpty(missingRefsInComposableStudio)
);
}
/**
* The `scan` function performs an audit on different modules (content-types, global-fields, and
* entries) and returns the missing references for each module.
* @returns The function `scan()` returns an object with properties `missingCtRefs`, `missingGfRefs`,
* and `missingEntryRefs`.
*/
async scanAndFix() {
log.debug('Starting scan and fix process', this.auditContext);
let { ctSchema, gfSchema } = this.getCtAndGfSchema();
log.info(
`Retrieved ${ctSchema?.length || 0} content types and ${gfSchema?.length || 0} global fields`,
this.auditContext,
);
let missingCtRefs,
missingGfRefs,
missingEntryRefs,
missingCtRefsInExtensions,
missingCtRefsInWorkflow,
missingSelectFeild,
missingEntry: {
missingEntryRefs?: Record<string, any>;
missingSelectFeild?: Record<string, any>;
missingMandatoryFields?: Record<string, any>;
missingTitleFields?: Record<string, any>;
missingEnvLocale?: Record<string, any>;
missingMultipleFields?: Record<string, any>;
} = {},
missingMandatoryFields,
missingTitleFields,
missingRefInCustomRoles,
missingEnvLocalesInAssets,
missingEnvLocalesInEntries,
missingFieldRules,
missingMultipleFields,
missingRefsInComposableStudio;
const constructorParam: ModuleConstructorParam & CtConstructorParam = {
ctSchema,
gfSchema,
config: {
...this.sharedConfig,
auditContext: this.auditContext,
},
fix: this.currentCommand === 'cm:stacks:audit:fix',
};
let dataModuleWise: Record<string, any> = await new ModuleDataReader(cloneDeep(constructorParam)).run();
log.debug(`Data module wise: ${JSON.stringify(dataModuleWise)}`, this.auditContext);
// Extract logConfig and showConsoleLogs once before the loop to reuse throughout
const logConfig = configHandler.get('log') || {};
const showConsoleLogs = logConfig.showConsoleLogs ?? false;
for (const module of this.sharedConfig.flags.modules || this.sharedConfig.modules) {
// Update audit context with current module
this.auditContext = { module: module };
log.debug(`Starting audit for module: ${module}`, this.auditContext);
log.info(`Starting audit for module: ${module}`, this.auditContext);
// Only show spinner message if console logs are enabled (compatible with line-by-line logs)
if (showConsoleLogs) {
print([
{
bold: true,
color: 'whiteBright',
message: this.$t(this.messages.AUDIT_START_SPINNER, { module }),
},
]);
}
constructorParam['moduleName'] = module;
switch (module) {
case 'assets':
log.info('Executing assets audit', this.auditContext);
const assetsTotalCount = dataModuleWise['assets']?.Total || 0;
missingEnvLocalesInAssets = await new Assets(cloneDeep(constructorParam)).run(false, assetsTotalCount);
await this.prepareReport(module, missingEnvLocalesInAssets);
this.getAffectedData('assets', dataModuleWise['assets'], missingEnvLocalesInAssets);
log.success(
`Assets audit completed. Found ${Object.keys(missingEnvLocalesInAssets || {}).length} issues`,
this.auditContext,
);
break;
case 'content-types':
log.info('Executing content-types audit', this.auditContext);
const contentTypesTotalCount = dataModuleWise['content-types']?.Total || 0;
missingCtRefs = await new ContentType(cloneDeep(constructorParam)).run(false, contentTypesTotalCount);
await this.prepareReport(module, missingCtRefs);
this.getAffectedData('content-types', dataModuleWise['content-types'], missingCtRefs);
log.success(
`Content-types audit completed. Found ${Object.keys(missingCtRefs || {}).length} issues`,
this.auditContext,
);
break;
case 'global-fields':
log.info('Executing global-fields audit', this.auditContext);
const globalFieldsTotalCount = dataModuleWise['global-fields']?.Total || 0;
missingGfRefs = await new GlobalField(cloneDeep(constructorParam)).run(false, globalFieldsTotalCount);
await this.prepareReport(module, missingGfRefs);
this.getAffectedData('global-fields', dataModuleWise['global-fields'], missingGfRefs);
log.success(
`Global-fields audit completed. Found ${Object.keys(missingGfRefs || {}).length} issues`,
this.auditContext,
);
break;
case 'entries':
log.info('Executing entries audit', this.auditContext);
const entriesTotalCount = dataModuleWise['entries']?.Total || 0;
missingEntry = await new Entries(cloneDeep(constructorParam)).run(entriesTotalCount);
missingEntryRefs = missingEntry.missingEntryRefs ?? {};
missingSelectFeild = missingEntry.missingSelectFeild ?? {};
missingMandatoryFields = missingEntry.missingMandatoryFields ?? {};
missingTitleFields = missingEntry.missingTitleFields ?? {};
missingEnvLocalesInEntries = missingEntry.missingEnvLocale ?? {};
missingMultipleFields = missingEntry.missingMultipleFields ?? {};
await this.prepareReport(module, missingEntryRefs);
await this.prepareReport(`Entries_Select_field`, missingSelectFeild);
await this.prepareReport('Entries_Mandatory_field', missingMandatoryFields);
await this.prepareReport('Entries_Title_field', missingTitleFields);
await this.prepareReport('Entry_Missing_Locale_and_Env_in_Publish_Details', missingEnvLocalesInEntries);
await this.prepareReport('Entry_Multiple_Fields', missingMultipleFields);
this.getAffectedData('entries', dataModuleWise['entries'], missingEntry);
log.success(
`Entries audit completed. Found ${Object.keys(missingEntryRefs || {}).length} reference issues`,
this.auditContext,
);
break;
case 'workflows':
log.info('Executing workflows audit', this.auditContext);
const workflowsTotalCount = dataModuleWise['workflows']?.Total || 0;
missingCtRefsInWorkflow = await new Workflows({
ctSchema,
moduleName: module,
config: this.sharedConfig,
fix: this.currentCommand === 'cm:stacks:audit:fix',
}).run(workflowsTotalCount);
await this.prepareReport(module, missingCtRefsInWorkflow);
this.getAffectedData('workflows', dataModuleWise['workflows'], missingCtRefsInWorkflow);
log.success(
`Workflows audit completed. Found ${Object.keys(missingCtRefsInWorkflow || {}).length} issues`,
this.auditContext,
);
break;
case 'extensions':
log.info('Executing extensions audit', this.auditContext);
const extensionsTotalCount = dataModuleWise['extensions']?.Total || 0;
missingCtRefsInExtensions = await new Extensions(cloneDeep(constructorParam)).run(extensionsTotalCount);
await this.prepareReport(module, missingCtRefsInExtensions);
this.getAffectedData('extensions', dataModuleWise['extensions'], missingCtRefsInExtensions);
log.success(
`Extensions audit completed. Found ${Object.keys(missingCtRefsInExtensions || {}).length} issues`,
this.auditContext,
);
break;
case 'custom-roles':
log.info('Executing custom-roles audit', this.auditContext);
const customRolesTotalCount = dataModuleWise['custom-roles']?.Total || 0;
missingRefInCustomRoles = await new CustomRoles(cloneDeep(constructorParam)).run(customRolesTotalCount);
await this.prepareReport(module, missingRefInCustomRoles);
this.getAffectedData('custom-roles', dataModuleWise['custom-roles'], missingRefInCustomRoles);
log.success(
`Custom-roles audit completed. Found ${Object.keys(missingRefInCustomRoles || {}).length} issues`,
this.auditContext,
);
break;
case 'field-rules':
log.info('Executing field-rules audit', this.auditContext);
// NOTE: We are using the fixed content-type for validation of field rules
const data = this.getCtAndGfSchema();
constructorParam.ctSchema = data.ctSchema;
constructorParam.gfSchema = data.gfSchema;
const fieldRulesTotalCount = dataModuleWise['content-types']?.Total || 0;
missingFieldRules = await new FieldRule(cloneDeep(constructorParam)).run(fieldRulesTotalCount);
await this.prepareReport(module, missingFieldRules);
this.getAffectedData('field-rules', dataModuleWise['content-types'], missingFieldRules);
log.success(
`Field-rules audit completed. Found ${Object.keys(missingFieldRules || {}).length} issues`,
this.auditContext,
);
break;
case 'composable-studio':
log.info('Executing composable-studio audit', this.auditContext);
missingRefsInComposableStudio = await new ComposableStudio(cloneDeep(constructorParam)).run();
await this.prepareReport(module, missingRefsInComposableStudio);
this.getAffectedData(
'composable-studio',
dataModuleWise['composable-studio'] || { Total: Object.keys(missingRefsInComposableStudio || {}).length },
missingRefsInComposableStudio,
);
log.success(
`Composable-studio audit completed. Found ${
Object.keys(missingRefsInComposableStudio || {}).length
} issues`,
this.auditContext,
);
break;
}
// Only show completion message if console logs are enabled
if (showConsoleLogs) {
print([
{
bold: true,
color: 'whiteBright',
message: this.$t(this.messages.AUDIT_START_SPINNER, { module }),
},
{
bold: true,
message: ' done',
color: 'whiteBright',
},
]);
}
}
log.debug('Scan and fix process completed', this.auditContext);
this.prepareReport('Summary', this.summaryDataToPrint);
this.prepareCSV('Summary', this.summaryDataToPrint);
return {
missingCtRefs,
missingGfRefs,
missingEntryRefs,
missingCtRefsInExtensions,
missingCtRefsInWorkflow,
missingSelectFeild,
missingMandatoryFields,
missingTitleFields,
missingRefInCustomRoles,
missingEnvLocalesInAssets,
missingEnvLocalesInEntries,
missingFieldRules,
missingMultipleFields,
missingRefsInComposableStudio,
};
}
/**
* The `promptQueue` function prompts the user to enter a data directory path if the `data-dir` flag
* is missing, and sets the `basePath` property of the `sharedConfig` object to the entered path.
*/
async promptQueue() {
// NOTE get content path if data-dir flag is missing
this.sharedConfig.basePath =
this.flags['data-dir'] ||
(await cliux.inquire<string>({
type: 'input',
name: 'data-dir',
message: this.messages.DATA_DIR,
}));
}
/**
* The function `createBackUp` creates a backup of data if the `copy-dir` flag is set, and throws
* an error if the specified path does not exist.
*/
async createBackUp() {
if (this.currentCommand === 'cm:stacks:audit:fix' && this.flags['copy-dir']) {
if (!existsSync(this.sharedConfig.basePath)) {
throw Error(this.$t(this.messages.NOT_VALID_PATH, { path: this.sharedConfig.basePath }));
}
// NOTE create bkp directory
const backupDirPath = `${(
this.flags['copy-path'] ||
this.flags['data-dir'] ||
this.sharedConfig.basePath
).replace(/\/+$/, '')}_backup_${uuid()}`;
if (!existsSync(backupDirPath)) {
mkdirSync(backupDirPath, { recursive: true });
}
await copy(this.sharedConfig.basePath, backupDirPath);
this.sharedConfig.basePath = backupDirPath;
}
}
/**
* The function `getCtAndGfSchema` reads and parses JSON files containing content type and global
* field schemas, and returns them as an object.
* @returns The function `getCtAndGfSchema()` returns an object with two properties: `ctSchema` and
* `gfSchema`. The values of these properties are the parsed JSON data from two different files.
*/
getCtAndGfSchema() {
const ctDirPath = join(this.sharedConfig.basePath, this.sharedConfig.moduleConfig['content-types'].dirName);
const gfPath = join(
this.sharedConfig.basePath,
this.sharedConfig.moduleConfig['global-fields'].dirName,
this.sharedConfig.moduleConfig['global-fields'].fileName,
);
const gfSchema = existsSync(gfPath) ? (JSON.parse(readFileSync(gfPath, 'utf8')) as ContentTypeStruct[]) : [];
const ctSchema = (readContentTypeSchemas(ctDirPath) || []) as ContentTypeStruct[];
return { ctSchema, gfSchema };
}
/**
* The function `showOutputOnScreen` displays missing references on the terminal screen if the
* `showTerminalOutput` flag is set to true.
* @param {{ module: string; missingRefs?: Record<string, any> }[]} allMissingRefs - An array of
* objects, where each object has two properties:
*/
showOutputOnScreen(allMissingRefs: { module: string; missingRefs?: Record<string, any> }[]) {
if (this.sharedConfig.showTerminalOutput && !this.flags['external-config']?.noTerminalOutput) {
cliux.print(''); // NOTE adding new line
for (const { module, missingRefs } of allMissingRefs) {
if (!isEmpty(missingRefs)) {
print([
{
bold: true,
color: 'cyan',
message: ` ${module}`,
},
]);
const tableValues = Object.values(missingRefs).flat();
const tableHeaders: TableHeader[] = [
{
value: 'name',
alias: 'Title',
},
{
value: 'ct',
alias: 'Content Type',
},
{
value: 'locale',
alias: 'Locale',
},
{
value: 'display_name',
alias: 'Field name',
},
{
value: 'data_type',
alias: 'Field type',
},
{
value: 'missingRefs',
alias: 'Missing references',
formatter: (cellValue: any) => {
return chalk.red(typeof cellValue === 'object' ? JSON.stringify(cellValue) : cellValue);
},
},
{
value: 'treeStr',
alias: 'Path',
},
];
cliux.table(tableHeaders, tableValues, { ...(this.flags as TableFlags) });
cliux.print(''); // NOTE adding new line
}
}
}
}
// Make it generic it takes the column header as param
showOutputOnScreenWorkflowsAndExtension(allMissingRefs: { module: string; missingRefs?: Record<string, any> }[]) {
if (!this.sharedConfig.showTerminalOutput || this.flags['external-config']?.noTerminalOutput) {
return;
}
cliux.print(''); // Adding a new line
for (let { module, missingRefs } of allMissingRefs) {
if (isEmpty(missingRefs)) {
continue;
}
print([{ bold: true, color: 'cyan', message: ` ${module}` }]);
const tableValues = Object.values(missingRefs).flat();
missingRefs = Object.values(missingRefs).flat();
const tableKeys = Object.keys(missingRefs[0]);
const tableHeaders: TableHeader[] = tableKeys
.filter((key) => config.OutputTableKeys.includes(key)) // Remove invalid keys early
.map((key: string) => ({
value: key,
formatter: (cellValue: any) => {
if (key === 'fixStatus' || key === 'Fixable' || key === 'Fixed') {
return chalk.green(typeof cellValue === 'object' ? JSON.stringify(cellValue) : cellValue);
} else if (
key === 'content_types' ||
key === 'branches' ||
key === 'missingCTSelectFieldValues' ||
key === 'missingFieldUid' ||
key === 'action' ||
key === 'Non-Fixable' ||
key === 'Not-Fixed'
) {
return chalk.red(typeof cellValue === 'object' ? JSON.stringify(cellValue) : cellValue);
} else {
return chalk.white(typeof cellValue === 'object' ? JSON.stringify(cellValue) : cellValue);
}
},
}));
cliux.table(tableHeaders, tableValues, { ...(this.flags as TableFlags) });
cliux.print(''); // Adding a new line
}
}
/**
* The function prepares a report by writing a JSON file and a CSV file with a list of missing
* references for a given module.
* @param moduleName - The `moduleName` parameter is a string that represents the name of a module.
* It is used to generate the filename for the report.
* @param listOfMissingRefs - The `listOfMissingRefs` parameter is a record object that contains
* information about missing references. It is a key-value pair where the key represents the
* reference name and the value represents additional information about the missing reference.
* @returns The function `prepareReport` returns a Promise that resolves to `void`.
*/
prepareReport(
moduleName:
| keyof typeof config.moduleConfig
| keyof typeof config.ReportTitleForEntries
| 'field-rules'
| 'Summary',
listOfMissingRefs: Record<string, any>,
): Promise<void> {
log.debug(`Preparing report for module: ${moduleName}`, this.auditContext);
log.debug(`Report path: ${this.sharedConfig.reportPath}`, this.auditContext);
log.info(`Missing references count: ${Object.keys(listOfMissingRefs).length}`, this.auditContext);
if (isEmpty(listOfMissingRefs)) {
log.debug(`No missing references found for ${moduleName}, skipping report generation`, this.auditContext);
return Promise.resolve(void 0);
}
if (!existsSync(this.sharedConfig.reportPath)) {
log.debug(`Creating report directory: ${this.sharedConfig.reportPath}`, this.auditContext);
mkdirSync(this.sharedConfig.reportPath, { recursive: true });
} else {
log.debug(`Report directory already exists: ${this.sharedConfig.reportPath}`, this.auditContext);
}
// NOTE write int json
const jsonFilePath = join(sanitizePath(this.sharedConfig.reportPath), `${sanitizePath(moduleName)}.json`);
log.debug(`Writing JSON report to: ${jsonFilePath}`, this.auditContext);
writeFileSync(jsonFilePath, JSON.stringify(listOfMissingRefs));
// NOTE write into CSV
log.debug(`Preparing CSV report for: ${moduleName}`, this.auditContext);
return this.prepareCSV(moduleName, listOfMissingRefs);
}
/**
* The function `prepareCSV` takes a module name and a list of missing references, and generates a
* CSV file with the specified columns and filtered rows.
* @param moduleName - The `moduleName` parameter is a string that represents the name of a module.
* It is used to generate the name of the CSV file that will be created.
* @param listOfMissingRefs - The `listOfMissingRefs` parameter is a record object that contains
* information about missing references. Each key in the record represents a reference, and the
* corresponding value is an array of objects that contain details about the missing reference.
* @returns The function `prepareCSV` returns a Promise that resolves to `void`.
*/
prepareCSV(
moduleName:
| keyof typeof config.moduleConfig
| keyof typeof config.ReportTitleForEntries
| 'field-rules'
| 'Summary',
listOfMissingRefs: Record<string, any>,
): Promise<void> {
if (Object.keys(config.moduleConfig).includes(moduleName) || config.field_level_modules.includes(moduleName)) {
const csvPath = join(sanitizePath(this.sharedConfig.reportPath), `${sanitizePath(moduleName)}.csv`);
return new Promise<void>((resolve, reject) => {
// file deepcode ignore MissingClose: Will auto close once csv stream end
const ws = createWriteStream(csvPath).on('error', reject);
const defaultColumns = Object.keys(OutputColumn);
const userDefinedColumns = this.sharedConfig.flags.columns ? this.sharedConfig.flags.columns.split(',') : null;
let missingRefs: RefErrorReturnType[] | WorkflowExtensionsRefErrorReturnType[] =
Object.values(listOfMissingRefs).flat();
const columns: (keyof typeof OutputColumn)[] = userDefinedColumns
? [...userDefinedColumns, ...defaultColumns.filter((val: string) => !userDefinedColumns.includes(val))]
: defaultColumns;
if (this.sharedConfig.flags.filter) {
const [column, value]: [keyof typeof OutputColumn, string] = this.sharedConfig.flags.filter.split('=');
// Filter the missingRefs array
missingRefs = missingRefs.filter((row) => {
if (OutputColumn[column] in row) {
const rowKey = OutputColumn[column] as keyof (RefErrorReturnType | WorkflowExtensionsRefErrorReturnType);
return row[rowKey] === value;
}
return false;
});
}
const rowData: Record<string, string | string[]>[] = [];
for (const issue of missingRefs) {
let row: Record<string, string | string[]> = {};
for (const column of columns) {
if (Object.keys(issue).includes(OutputColumn[column])) {
const issueKey = OutputColumn[column] as keyof typeof issue;
row[column] = issue[issueKey] as string;
row[column] = typeof row[column] === 'object' ? JSON.stringify(row[column]) : row[column];
}
}
if (this.currentCommand === 'cm:stacks:audit:fix') {
row['Fix status'] = row.fixStatus;
}
rowData.push(row);
}
csv.write(rowData, { headers: true }).pipe(ws).on('error', reject).on('finish', resolve);
});
} else {
return new Promise<void>((reject) => {
return reject();
});
}
}
getAffectedData(
module: string,
dataExported: Record<string, any>,
listOfMissingRefs: Record<string, any>,
isFixable: boolean = true,
): void {
const result: Record<string, any> = { Module: module, ...dataExported };
if (module === 'entries') {
const missingRefs = Object.entries(listOfMissingRefs);
const uidSet = new Set<string>();
const nonFixable = new Set<string>();
for (const [key, refs] of missingRefs) {
const uids = Object.keys(refs);
if (
key === 'missingTitleFields' ||
(!this.sharedConfig.fixSelectField &&
key === 'missingSelectFeild' &&
this.currentCommand === 'cm:stacks:audit:fix')
) {
uids.forEach((uid) => {
if (uidSet.has(uid)) {
uidSet.delete(uid);
nonFixable.add(uid);
} else {
nonFixable.add(uid);
}
});
} else {
uids.forEach((uid) => uidSet.add(uid));
}
}
result.Passed = dataExported.Total - (uidSet.size + nonFixable.size);
if (this.currentCommand === 'cm:stacks:audit:fix') {
result.Fixed = uidSet.size;
result['Not-Fixed'] = nonFixable.size;
} else {
result.Fixable = uidSet.size;
result['Non-Fixable'] = nonFixable.size;
}
} else {
const missingCount = Object.keys(listOfMissingRefs).length;
result.Passed = dataExported.Total - missingCount;
if (this.currentCommand === 'cm:stacks:audit:fix') {
result.Fixed = missingCount > 0 && isFixable ? missingCount : 0;
result['Not-Fixed'] = missingCount > 0 && !isFixable ? missingCount : 0;
} else {
result.Fixable = missingCount > 0 && isFixable ? missingCount : 0;
result['Non-Fixable'] = missingCount > 0 && !isFixable ? missingCount : 0;
}
}
this.summaryDataToPrint.push(result);
}
}