Skip to content

Commit

Permalink
Merge pull request #1340 from contentstack/fix/stage-audit-issue
Browse files Browse the repository at this point in the history
fixed the writing of content without --yes flag in audit
  • Loading branch information
cs-raj authored Mar 13, 2024
2 parents 44f55a8 + 616c2db commit f1365c0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
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 @@ -34,7 +34,7 @@ const auditMsg = {
SCAN_ENTRY_SUCCESS_MSG: "Successfully completed the scanning of {module} ({local}) '{title}'.",
SCAN_EXT_SUCCESS_MSG: "Successfully completed scanning the {module} titled '{title}' with UID '{uid}'",
AUDIT_CMD_DESCRIPTION: 'Perform audits and find possible errors in the exported Contentstack data',
SCAN_WF_SUCCESS_MSG: 'Successfully removed the workflow with UID {uid} and name {name}.',
SCAN_WF_SUCCESS_MSG: 'Successfully completed the scanning of workflow with UID {uid} and name {name}.',
};

const auditFixMsg = {
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-audit/src/modules/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Extensions {
if (this.fix && this.missingCtInExtensions.length) {
await this.fixExtensionsScope(cloneDeep(this.missingCtInExtensions));
this.missingCtInExtensions.forEach((ext) => (ext.fixStatus = 'Fixed'));
return this.missingCtInExtensions
return this.missingCtInExtensions;
}
return this.missingCtInExtensions;
}
Expand All @@ -106,7 +106,7 @@ export default class Extensions {

async writeFixContent(fixedExtensions: Record<string, Extension>) {
if (
this.fix ||
this.fix &&
(this.config.flags['copy-dir'] ||
this.config.flags['external-config']?.skipConfirm ||
(await ux.confirm(commonMsg.FIX_CONFIRMATION)))
Expand Down
34 changes: 18 additions & 16 deletions packages/contentstack-audit/src/modules/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ export default class Workflows {
this.log($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' });
return {};
}

this.workflowPath = join(this.folderPath, this.fileName);
this.workflowSchema = existsSync(this.workflowPath) ? values(JSON.parse(readFileSync(this.workflowPath, 'utf8')) as Workflow[]) : [];

this.workflowSchema = existsSync(this.workflowPath)
? values(JSON.parse(readFileSync(this.workflowPath, 'utf8')) as Workflow[])
: [];

this.ctSchema.forEach((ct) => this.ctUidSet.add(ct.uid));

for (const workflow of this.workflowSchema) {
const ctNotPresent = workflow.content_types.filter((ct) => !this.ctUidSet.has(ct));
if (ctNotPresent.length) {
Expand All @@ -68,53 +70,53 @@ export default class Workflows {
ctNotPresent.forEach((ct) => this.missingCts.add(ct));
this.missingCtInWorkflows.push(tempwf);
}

this.log(
$t(auditMsg.SCAN_WF_SUCCESS_MSG, {
name: workflow.name,
module: this.config.moduleConfig[this.moduleName].name,
uid: workflow.uid,
}),
'info'
'info',
);
}

if (this.fix && this.missingCtInWorkflows.length) {
await this.fixWorkflowSchema();
this.missingCtInWorkflows.forEach((wf) => (wf.fixStatus = 'Fixed'));
}

return this.missingCtInWorkflows;
}

async fixWorkflowSchema() {
async fixWorkflowSchema() {
const newWorkflowSchema: Record<string, Workflow> = existsSync(this.workflowPath)
? JSON.parse(readFileSync(this.workflowPath, 'utf8'))
: {};

if (Object.keys(newWorkflowSchema).length !== 0) {
for (const workflow of this.workflowSchema) {
for (const workflow of this.workflowSchema) {
const fixedCts = workflow.content_types.filter((ct) => !this.missingCts.has(ct));
if (fixedCts.length) {
newWorkflowSchema[workflow.uid].content_types = fixedCts;
} else {
const { name, uid } = workflow;
const warningMessage = $t(commonMsg.WORKFLOW_FIX_WARN, { name, uid });

this.log(warningMessage, { color: 'yellow' });

if (this.config.flags.yes || (await ux.confirm(commonMsg.WORKFLOW_FIX_CONFIRMATION))) {
delete newWorkflowSchema[workflow.uid];
}
}
}
}

await this.writeFixContent(newWorkflowSchema);
}

async writeFixContent(newWorkflowSchema: Record<string, Workflow>) {
if (
this.fix ||
this.fix &&
!(this.config.flags['copy-dir'] || this.config.flags['external-config']?.skipConfirm) &&
(this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION)))
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/src/import/module-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ModuleImporter {
if (
!this.importConfig.skipAudit &&
(!this.importConfig.moduleName ||
['content-types', 'global-fields', 'entries'].includes(this.importConfig.moduleName))
['content-types', 'global-fields', 'entries', 'extensions', 'workflows'].includes(this.importConfig.moduleName))
) {
if (!(await this.auditImportData(logger))) {
return { noSuccessMsg: true };
Expand Down

0 comments on commit f1365c0

Please sign in to comment.