Skip to content

Commit

Permalink
Merge branch 'development' into feat/CS-42980
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-raj committed Jan 3, 2024
2 parents 1bbbf52 + f6b0664 commit 3bc4c81
Show file tree
Hide file tree
Showing 38 changed files with 355 additions and 217 deletions.
64 changes: 32 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-audit
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/cli-audit/1.3.0 darwin-arm64 node-v20.8.0
@contentstack/cli-audit/1.3.2 darwin-arm64 node-v20.8.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-audit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-audit",
"version": "1.3.1",
"version": "1.3.2",
"description": "Contentstack audit plugin",
"author": "Contentstack CLI",
"homepage": "https://github.com/contentstack/cli",
Expand All @@ -19,7 +19,7 @@
],
"dependencies": {
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.9",
"@contentstack/cli-utilities": "~1.5.10",
"@oclif/plugin-help": "^5",
"@oclif/plugin-plugins": "^4.1.9",
"chalk": "^4.1.2",
Expand Down
17 changes: 11 additions & 6 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
this.log(this.messages.NO_MISSING_REF_FOUND, 'info');
this.log('');

if (this.currentCommand === 'cm:stacks:audit:fix' && existsSync(this.sharedConfig.basePath)) {
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 });
}
Expand Down Expand Up @@ -135,10 +139,11 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
}

// NOTE create bkp directory
const backupDirPath = `${(this.flags['copy-path'] || this.flags['data-dir']).replace(
/\/+$/,
'',
)}_backup_${uuid()}`;
const backupDirPath = `${(
this.flags['copy-path'] ||
this.flags['data-dir'] ||
this.sharedConfig.basePath
).replace(/\/+$/, '')}_backup_${uuid()}`;

if (!existsSync(backupDirPath)) {
mkdirSync(backupDirPath, { recursive: true });
Expand Down Expand Up @@ -194,7 +199,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
* objects, where each object has two properties:
*/
showOutputOnScreen(allMissingRefs: { module: string; missingRefs?: Record<string, any> }[]) {
if (this.sharedConfig.showTerminalOutput) {
if (this.sharedConfig.showTerminalOutput && !this.flags['external-config']?.noTerminalOutput) {
this.log(''); // NOTE adding new line
for (const { module, missingRefs } of allMissingRefs) {
if (!isEmpty(missingRefs)) {
Expand Down
22 changes: 17 additions & 5 deletions packages/contentstack-audit/src/base-command.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import merge from 'lodash/merge';
import { existsSync, readFileSync } from 'fs';
import { Command } from '@contentstack/cli-command';
import { Flags, FlagInput, Interfaces, cliux as ux } from '@contentstack/cli-utilities';
import { Flags, FlagInput, Interfaces, cliux, ux, PrintOptions } from '@contentstack/cli-utilities';

import config from './config';
import { Logger } from './util';
import { ConfigType, LogFn } from './types';
import { ConfigType, LogFn, LoggerType } from './types';
import messages, { $t, commonMsg } from './messages';

export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>;

const noLog = (_message: string | any, _logType?: LoggerType | PrintOptions | undefined) => {};

export abstract class BaseCommand<T extends typeof Command> extends Command {
public log!: LogFn;
public logger!: Logger;
Expand Down Expand Up @@ -55,12 +57,22 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

this.sharedConfig = Object.assign(this.sharedConfig, { flags: this.flags });

ux.registerSearchPlugin();
if (this.flags['external-config']?.config) {
this.sharedConfig = Object.assign(this.sharedConfig, this.flags['external-config']?.config);
}

cliux.registerSearchPlugin();
this.registerConfig();

// Init logger
const logger = new Logger(this.sharedConfig);
this.log = logger.log.bind(logger);
if (this.flags['external-config']?.noLog) {
this.log = noLog;
ux.action.start = () => {};
ux.action.stop = () => {};
} else {
const logger = new Logger(this.sharedConfig);
this.log = logger.log.bind(logger);
}
}

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/contentstack-audit/src/commands/cm/stacks/audit/fix.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { FlagInput, Flags, ux } from '@contentstack/cli-utilities';

import config from '../../../../config';
import { getTableFlags } from '../../../../util';
import { ConfigType } from '../../../../types';
import { auditFixMsg, auditMsg } from '../../../../messages';
import { AuditBaseCommand } from '../../../../audit-base-command';
import { getJsonInputFlags, getTableFlags } from '../../../../util';

const jsonFlag = getJsonInputFlags({ hidden: true });

export default class AuditFix extends AuditBaseCommand {
static aliases: string[] = ['audit:fix', 'cm:stacks:audit:fix'];
Expand Down Expand Up @@ -49,16 +52,21 @@ export default class AuditFix extends AuditBaseCommand {
hidden: true,
description: 'Use this flag to skip confirmation',
}),
'external-config': jsonFlag(),
...getTableFlags(),
};

/**
* The `run` function is an asynchronous function that performs an audit on different modules
* (content-types, global-fields, entries) and generates a report.
*/
async run(): Promise<void> {
async run(): Promise<void | ConfigType> {
try {
await this.start('cm:stacks:audit:fix');

if (this.flags['external-config']?.returnConfig) {
return this.sharedConfig;
}
} catch (error) {
this.log(error instanceof Error ? error.message : error, 'error');
console.trace(error);
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-audit/src/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ export default class ContentType {

if (!refExist) {
this.missingRefs[this.currentUid].push(refErrorObj);
}

return refExist;
return block;
}
}

block.schema = this.runFixOnSchema(tree, block.schema as ContentTypeSchemaType[]);
Expand Down
Loading

0 comments on commit 3bc4c81

Please sign in to comment.