Skip to content

Commit

Permalink
code optimisation, fixed test cases, and wrote generic function for s…
Browse files Browse the repository at this point in the history
…howing output on screen
  • Loading branch information
cs-raj committed Mar 2, 2024
1 parent 0389df2 commit e97798b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 128 deletions.
78 changes: 38 additions & 40 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
},
]);
const tableValues = Object.values(missingRefs).flat();

ux.table(
tableValues,
{
Expand Down Expand Up @@ -265,47 +264,46 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma

// 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) {
this.log(''); // 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();
ux.table(
tableValues,
{
name: {
minWidth: 7,
header: 'Title',
},
uid: {
minWidth: 12,
header: 'Workflow Uid',
},
content_types: {
minWidth: 7,
header: 'Missing Content Types',
get: (row) => {
return chalk.red(
typeof row.content_types === 'object' ? JSON.stringify(row.content_types) : row.content_types,
);
},
},
...(tableValues[0]?.fixStatus ? this.fixStatus : {}),
},
{
...this.flags,
if (!this.sharedConfig.showTerminalOutput || this.flags['external-config']?.noTerminalOutput) {
return;
}

this.log(''); // Adding a new line

for (const { module, missingRefs } of allMissingRefs) {
if (isEmpty(missingRefs)) {
continue;
}

print([{ bold: true, color: 'cyan', message: ` ${module}` }]);

const tableValues = Object.values(missingRefs).flat();

const tableKeys = Object.keys(missingRefs[0]);
const arrayOfObjects = tableKeys.map((key) => {
if (['title', 'name', 'uid', 'content_types', 'fixStatus'].includes(key)) {
return {
[key]: {
minWidth: 7,
header: key,
get: (row: Record<string, unknown>) => {
if(key==='fixStatus') {
return chalk.green(typeof row[key] === 'object' ? JSON.stringify(row[key]) : row[key]);
} else if(key==='content_types') {
return chalk.red(typeof row[key] === 'object' ? JSON.stringify(row[key]) : row[key]);
} else {
return chalk.white(typeof row[key] === 'object' ? JSON.stringify(row[key]) : row[key]);
}
}
},
);
this.log(''); // NOTE adding new line
};
}
}
return {};
});
const mergedObject = Object.assign({}, ...arrayOfObjects);

ux.table(tableValues, mergedObject, { ...this.flags });
this.log(''); // Adding a new line
}
}

Expand Down
87 changes: 38 additions & 49 deletions packages/contentstack-audit/src/modules/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,85 +54,74 @@ 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));
this.workflowSchema.forEach((workflow: Workflow) => {
let ctNotPresent: string[] = [];
workflow.content_types.forEach((ct) => {
if (!this.ctUidSet.has(ct)) {
ctNotPresent.push(ct);
this.missingCts.add(ct);
}
});

for (const workflow of this.workflowSchema) {
const ctNotPresent = workflow.content_types.filter((ct) => !this.ctUidSet.has(ct));
if (ctNotPresent.length) {
workflow.content_types = ctNotPresent;
this.missingCtInWorkflows.push(cloneDeep(workflow));
const tempwf = cloneDeep(workflow);
tempwf.content_types = ctNotPresent;
this.missingCtInWorkflows.push(tempwf);
}

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

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

return this.missingCtInWorkflows;
}

async fixWorkflowSchema() {
for (let workflow in this.workflowSchema) {
this.workflowSchema[workflow].content_types = this.workflowSchema[workflow].content_types.filter((ct) => {
!this.missingCts.has(ct);
});
}
let newWorkflowSchema: Record<string, Workflow> = existsSync(this.workflowPath)
async fixWorkflowSchema() {
const newWorkflowSchema: Record<string, Workflow> = existsSync(this.workflowPath)
? JSON.parse(readFileSync(this.workflowPath, 'utf8'))
: {};

if (Object.keys(newWorkflowSchema).length !== 0) {
for (let workflow in this.workflowSchema) {
let fixedCts = this.workflowSchema[workflow].content_types.filter((ct) => {
!this.missingCts.has(ct);
});
for (const workflow of this.workflowSchema) {
const fixedCts = workflow.content_types.filter((ct) => !this.missingCts.has(ct));

if (fixedCts.length) {
newWorkflowSchema[this.workflowSchema[workflow].uid].content_types = fixedCts;
newWorkflowSchema[workflow.uid].content_types = fixedCts;
} else {
this.log(
$t(commonMsg.WORKFLOW_FIX_WARN, {
name: this.workflowSchema[workflow].name,
uid: this.workflowSchema[workflow].uid,
}),
{ color: 'yellow' },
);
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[this.workflowSchema[workflow].uid];
delete newWorkflowSchema[workflow.uid];
}
}
}
}

await this.writeFixContent(newWorkflowSchema);
}

async writeFixContent(newWorkflowSchema: Record<string, Workflow>) {
let canWrite = true;

if (this.fix) {
if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) {
canWrite = this.config.flags.yes ?? (await ux.confirm(commonMsg.FIX_CONFIRMATION));
}
if (canWrite) {
writeFileSync(
join(this.folderPath, this.config.moduleConfig[this.moduleName].fileName),
JSON.stringify(newWorkflowSchema),
);
}
if (
this.fix &&
!(this.config.flags['copy-dir'] || this.config.flags['external-config']?.skipConfirm) &&
(this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION)))
) {
writeFileSync(
join(this.folderPath, this.config.moduleConfig[this.moduleName].fileName),
JSON.stringify(newWorkflowSchema),
);
}
}
}
1 change: 1 addition & 0 deletions packages/contentstack-audit/src/types/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface Workflow {
enabled?: boolean;
deleted_at?: any;
missingRefs?: any;
fixStatus?:string
}
98 changes: 59 additions & 39 deletions packages/contentstack-audit/test/unit/modules/workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import config from '../../../src/config';
import { Workflows } from '../../../src/modules';
import { $t, auditMsg } from '../../../src/messages';
import { values } from 'lodash';
import { Workflow } from '../../../src/types';
import { join } from 'path';

describe('Workflows', () => {
describe('run method with invalid path for workflows', () => {
Expand Down Expand Up @@ -42,17 +40,35 @@ describe('Workflows', () => {
flags: {},
}),
});

fancy
.stdout({ print: process.env.PRINT === 'true' || true })
.stub(ux, 'confirm', async () => true)
.it(
'should expect missingRefs equal to empty array, expect entire workflow schema and empty missingCts',
'should expect missingRefs equal to workflow which has missing refs, missingCts equal to missing Cts',
async () => {
const missingRefs = await wf.run();
expect(wf.workflowSchema).eql(values(JSON.parse(fs.readFileSync(wf.workflowPath, 'utf8'))));
expect(missingRefs).eql([]);
expect(wf.missingCts).eql(new Set([]));
expect(missingRefs).eql([
{
name: 'wf1',
uid: 'wf1',
org_uid: 'org1',
api_key: 'apiKey',
content_types: ['ct45', 'ct14'],
enabled: false,
deleted_at: false,
},
{
name: 'wf3',
uid: 'wf3',
org_uid: 'org1',
api_key: 'apiKey',
content_types: ['ct6'],
enabled: false,
deleted_at: false,
},
]);
expect(wf.missingCts).eql(new Set(['ct45', 'ct14', 'ct6']));
},
);
});
Expand All @@ -73,6 +89,7 @@ describe('Workflows', () => {
.stub(ux, 'confirm', async () => true)
.it('should expect missingRefs equal to all workflows', async () => {
const missingRefs = await wf.run();
wf.workflowSchema.pop();
expect(missingRefs).eql(wf.workflowSchema);
});
});
Expand All @@ -81,7 +98,7 @@ describe('Workflows', () => {
const wf = new Workflows({
log: () => {},
moduleName: 'workflows',
ctSchema: [],
ctSchema: cloneDeep(require('./../mock/contents/workflows/ctSchema.json')),
config: Object.assign(config, {
basePath: resolve(`./test/unit/mock/contents/`),
flags: {},
Expand All @@ -91,40 +108,43 @@ describe('Workflows', () => {

fancy
.stdout({ print: process.env.PRINT === 'true' || true })
.stub(wf, 'fixWorkflowSchema', async () => {})
.stub(wf, 'writeFixContent', async () => {})
.stub(wf, 'log', async () => {})
.stub(ux, 'confirm', async () => true)
.stub(wf, 'WriteFileSync', () => {})
.it('the run function should run and flow should go till fixWorkflowSchema', async () => {
await wf.run();
});

fancy
.stdout({ print: process.env.PRINT === 'true' || true })
.stub(wf, 'writeFixContent', () => {})
.stub(ux, 'confirm', async () => true)
.it('should expect fixWorkflow schema to run', async () => {
const wfInstance = new (class Class extends Workflows {
public newWorkflowSchema!: Record<string, Workflow>;
constructor() {
super({
log: () => {},
moduleName: 'workflows',
ctSchema: [],
config: Object.assign(config, {
basePath: resolve(`./test/unit/mock/contents/`),
flags: {},
}),
fix: true,
});
this.workflowPath = join(this.folderPath, this.fileName);
}
async writeFixContent(WorkflowSchema: Record<string, Workflow>) {
this.newWorkflowSchema = WorkflowSchema;
}
})();
await wfInstance.run();
await wfInstance.fixWorkflowSchema();
expect(wfInstance.newWorkflowSchema).eql({});
const fixedReference = await wf.run();
expect(fixedReference).eql([
{
name: 'wf1',
uid: 'wf1',
org_uid: 'org1',
api_key: 'apiKey',
content_types: ['ct45', 'ct14'],
enabled: false,
deleted_at: false,
fixStatus: 'Fixed',
},
{
name: 'wf3',
uid: 'wf3',
org_uid: 'org1',
api_key: 'apiKey',
content_types: ['ct6'],
enabled: false,
deleted_at: false,
fixStatus: 'Fixed',
},
{
api_key: 'apiKey',
content_types: ['ct88'],
deleted_at: false,
enabled: false,
fixStatus: 'Fixed',
name: 'wf6',
org_uid: 'org1',
uid: 'wf6',
},
]);
});
});
});

0 comments on commit e97798b

Please sign in to comment.