Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Global field audit:fix functionality added #1087

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 25 additions & 38 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ import { ContentTypeStruct, OutputColumn, RefErrorReturnType } from './types';
export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseCommand> {
private currentCommand!: string;

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, and entries, and displays
* any missing references.
Expand Down Expand Up @@ -57,40 +69,26 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
for (const module of this.sharedConfig.flags.modules || this.sharedConfig.modules) {
ux.action.start(this.$t(this.messages.AUDIT_START_SPINNER, { module }));

const constructorParam = {
ctSchema,
gfSchema,
log: this.log,
moduleName: module,
config: this.sharedConfig,
fix: this.currentCommand === 'cm:stacks:audit:fix',
};

switch (module) {
case 'content-types':
missingCtRefs = await new ContentType({
ctSchema,
gfSchema,
log: this.log,
moduleName: module,
config: this.sharedConfig,
fix: this.currentCommand === 'cm:stacks:audit:fix',
}).run();

missingCtRefs = await new ContentType(constructorParam).run();
await this.prepareReport(module, missingCtRefs);
break;
case 'global-fields':
missingGfRefs = await new GlobalField({
ctSchema,
gfSchema,
log: this.log,
moduleName: module,
config: this.sharedConfig,
fix: this.currentCommand === 'cm:stacks:audit:fix',
}).run();

missingGfRefs = await new GlobalField(constructorParam).run();
await this.prepareReport(module, missingGfRefs);
break;
case 'entries':
missingEntryRefs = await new Entries({
ctSchema,
gfSchema,
log: this.log,
moduleName: module,
config: this.sharedConfig,
fix: this.currentCommand === 'cm:stacks:audit:fix',
}).run();
missingEntryRefs = await new Entries(constructorParam).run();
await this.prepareReport(module, missingEntryRefs);
break;
}
Expand Down Expand Up @@ -197,17 +195,6 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
message: ` ${module}`,
},
]);
let fixColumn: Record<string, any> = {};

if (this.currentCommand === 'cm:stacks:audit:fix') {
fixColumn['fixStatus'] = {
minWidth: 7,
header: 'Fix Status',
get: (row: any) => {
return row.fixStatus === 'Fixed' ? chalk.greenBright(row.fixStatus) : chalk.redBright(row.fixStatus);
},
};
}

ux.table(
Object.values(missingRefs).flat(),
Expand All @@ -233,7 +220,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
);
},
},
...fixColumn,
...this.fixStatus,
treeStr: {
minWidth: 7,
header: 'Path',
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const commonMsg = {
const auditMsg = {
REPORT_PATH: 'Path to store the audit reports.',
MODULES: 'Provide the list of modules to be audited.',
AUDIT_START_SPINNER: 'Starting {module} scanning...',
AUDIT_START_SPINNER: '{module} scanning...',
PREPARING_ENTRY_METADATA: 'Creating entry metadata...',
REFERENCE_ONLY: 'Checks only for missing references.',
NOT_VALID_PATH: "Provided path '{path}' is not valid.",
Expand Down
7 changes: 3 additions & 4 deletions packages/contentstack-audit/src/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ export default class ContentType {
public ctSchema: ContentTypeStruct[];
protected schema: ContentTypeStruct[] = [];
protected missingRefs: Record<string, any> = {};
public moduleName: keyof typeof auditConfig.moduleConfig = 'content-types';
public moduleName: keyof typeof auditConfig.moduleConfig;

constructor({ log, fix, config, moduleName, ctSchema, gfSchema }: ModuleConstructorParam & CtConstructorParam) {
this.log = log;
this.fix = fix || false;
this.config = config;
this.fix = fix ?? false;
this.ctSchema = ctSchema;
this.gfSchema = gfSchema;
this.moduleName = moduleName ?? 'content-types';
this.fileName = config.moduleConfig[this.moduleName].fileName;
this.folderPath = resolve(config.basePath, config.moduleConfig[this.moduleName].dirName);

if (moduleName) this.moduleName = moduleName;
}

/**
Expand Down