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: New hidden flag introduced to accept json data [CS-43198] #1223

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,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
35 changes: 33 additions & 2 deletions packages/contentstack-audit/src/util/flags.ts
Original file line number Diff line number Diff line change
@@ -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;
};

antonyagustine marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand All @@ -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<string, unknown>, Record<string, unknown>> {
const { hidden, description = defaultJSONOptions.description } = options;

return Flags.custom<Record<string, unknown>, Record<string, unknown>>({
hidden,
description,
parse: async (input, _opts) => {
try {
return JSON.parse(input);
} catch (error) {
throw new Error('Invalid JSON');
}
},
});
}

export { getTableFlags, getJsonInputFlags };
2 changes: 1 addition & 1 deletion packages/contentstack-utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Loading