From 92f3f2518e4b1855211dbb312d1fd5f5e29296c9 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 21 Dec 2023 14:23:51 +0530 Subject: [PATCH 1/2] Feat: New hidden flag introduced to accept json data to overwrite default audit-fix functionalities --- .../src/audit-base-command.ts | 2 +- .../contentstack-audit/src/base-command.ts | 22 +++++++++--- .../src/commands/cm/stacks/audit/fix.ts | 12 +++++-- packages/contentstack-audit/src/util/flags.ts | 35 +++++++++++++++++-- packages/contentstack-utilities/src/index.ts | 2 +- 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/packages/contentstack-audit/src/audit-base-command.ts b/packages/contentstack-audit/src/audit-base-command.ts index b1794a4247..8f899224b7 100644 --- a/packages/contentstack-audit/src/audit-base-command.ts +++ b/packages/contentstack-audit/src/audit-base-command.ts @@ -199,7 +199,7 @@ export abstract class AuditBaseCommand extends BaseCommand }[]) { - 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)) { diff --git a/packages/contentstack-audit/src/base-command.ts b/packages/contentstack-audit/src/base-command.ts index e5158ef675..1e7506e242 100644 --- a/packages/contentstack-audit/src/base-command.ts +++ b/packages/contentstack-audit/src/base-command.ts @@ -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 = Interfaces.InferredArgs; export type Flags = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>; +const noLog = (_message: string | any, _logType?: LoggerType | PrintOptions | undefined) => {}; + export abstract class BaseCommand extends Command { public log!: LogFn; public logger!: Logger; @@ -55,12 +57,22 @@ export abstract class BaseCommand 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); + } } /** diff --git a/packages/contentstack-audit/src/commands/cm/stacks/audit/fix.ts b/packages/contentstack-audit/src/commands/cm/stacks/audit/fix.ts index e27de41ca0..31f22e05e5 100644 --- a/packages/contentstack-audit/src/commands/cm/stacks/audit/fix.ts +++ b/packages/contentstack-audit/src/commands/cm/stacks/audit/fix.ts @@ -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']; @@ -49,6 +52,7 @@ export default class AuditFix extends AuditBaseCommand { hidden: true, description: 'Use this flag to skip confirmation', }), + 'external-config': jsonFlag(), ...getTableFlags(), }; @@ -56,9 +60,13 @@ export default class AuditFix extends AuditBaseCommand { * 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 { + async run(): Promise { 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); diff --git a/packages/contentstack-audit/src/util/flags.ts b/packages/contentstack-audit/src/util/flags.ts index 3af4fc96bf..8205ac52e5 100644 --- a/packages/contentstack-audit/src/util/flags.ts +++ b/packages/contentstack-audit/src/util/flags.ts @@ -1,8 +1,15 @@ -import { ux } from '@contentstack/cli-utilities'; +import { FlagDefinition, Flags, ux } from '@contentstack/cli-utilities'; import { IFlags, IncludeFlags } from '../types'; import { tableColumnDescriptions } from '../messages'; +type JSONFlagOptions = { + hidden?: boolean; + description?: string; +}; + +const defaultJSONOptions = { description: 'Provide JSON input' }; + /** * The function `getTableFlags` returns a set of table flags based on the specified columns, with * updated descriptions and help groups. @@ -28,4 +35,28 @@ function getTableFlags( return flags; } -export { getTableFlags }; +/** + * The function `getJsonInputFlags` returns a flag definition for parsing JSON input. + * @param {JSONFlagOptions} options - The `options` parameter is an object that contains the following + * properties: + * @returns a `FlagDefinition` object. + */ +function getJsonInputFlags( + options: JSONFlagOptions = defaultJSONOptions, +): FlagDefinition, Record> { + const { hidden, description = defaultJSONOptions.description } = options; + + return Flags.custom, Record>({ + hidden, + description, + parse: async (input, _opts) => { + try { + return JSON.parse(input); + } catch (error) { + throw new Error('Invalid JSON'); + } + }, + }); +} + +export { getTableFlags, getJsonInputFlags }; diff --git a/packages/contentstack-utilities/src/index.ts b/packages/contentstack-utilities/src/index.ts index 4922cd7bfe..6de0b624aa 100644 --- a/packages/contentstack-utilities/src/index.ts +++ b/packages/contentstack-utilities/src/index.ts @@ -73,7 +73,7 @@ export { stderr, stdout, } from '@oclif/core'; -export { FlagInput, ArgInput } from '@oclif/core/lib/interfaces/parser'; +export { FlagInput, ArgInput, FlagDefinition } from '@oclif/core/lib/interfaces/parser'; export { default as TablePrompt } from './inquirer-table-prompt'; From 5b7eddea551dfe61f02aeec98a30831b128aab93 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 21 Dec 2023 16:37:49 +0530 Subject: [PATCH 2/2] Type definition moved from script file to types file --- packages/contentstack-audit/src/types/utils.ts | 5 +++++ packages/contentstack-audit/src/util/flags.ts | 7 +------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/contentstack-audit/src/types/utils.ts b/packages/contentstack-audit/src/types/utils.ts index 98ab4a85e1..c0940db196 100644 --- a/packages/contentstack-audit/src/types/utils.ts +++ b/packages/contentstack-audit/src/types/utils.ts @@ -27,3 +27,8 @@ export type PrintType = { bold?: boolean; color?: typeof Color; }; + +export type JSONFlagOptions = { + hidden?: boolean; + description?: string; +}; \ No newline at end of file diff --git a/packages/contentstack-audit/src/util/flags.ts b/packages/contentstack-audit/src/util/flags.ts index 8205ac52e5..c49ba53d33 100644 --- a/packages/contentstack-audit/src/util/flags.ts +++ b/packages/contentstack-audit/src/util/flags.ts @@ -1,13 +1,8 @@ import { FlagDefinition, Flags, ux } from '@contentstack/cli-utilities'; -import { IFlags, IncludeFlags } from '../types'; +import { IFlags, IncludeFlags, JSONFlagOptions } from '../types'; import { tableColumnDescriptions } from '../messages'; -type JSONFlagOptions = { - hidden?: boolean; - description?: string; -}; - const defaultJSONOptions = { description: 'Provide JSON input' }; /**