Skip to content

Commit

Permalink
Merge pull request #1232 from contentstack/development
Browse files Browse the repository at this point in the history
Merge development to staging
  • Loading branch information
aman19K authored Jan 9, 2024
2 parents 8b87c8d + 51ced51 commit 07ba7da
Show file tree
Hide file tree
Showing 54 changed files with 1,083 additions and 20,692 deletions.
20,130 changes: 46 additions & 20,084 deletions package-lock.json

Large diffs are not rendered by default.

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
21 changes: 14 additions & 7 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
* @param {string} command - The `command` parameter is a string that represents the current command
* being executed.
*/
async start(command: CommandNames): Promise<void> {
async start(command: CommandNames): Promise<boolean> {
this.currentCommand = command;
await this.promptQueue();
await this.createBackUp();
Expand All @@ -60,11 +60,17 @@ 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 });
}
}

return !isEmpty(missingCtRefs) || !isEmpty(missingGfRefs) || !isEmpty(missingEntryRefs);
}

/**
Expand Down Expand Up @@ -135,10 +141,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 +201,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
23 changes: 18 additions & 5 deletions packages/contentstack-audit/src/base-command.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import merge from 'lodash/merge';
import isEmpty from 'lodash/isEmpty';
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 +58,22 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

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

ux.registerSearchPlugin();
if (!isEmpty(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
14 changes: 11 additions & 3 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 | { config: ConfigType; hasFix: boolean }> {
try {
await this.start('cm:stacks:audit:fix');
const hasFix = await this.start('cm:stacks:audit:fix');

if (this.flags['external-config']?.returnResponse) {
return { config: this.sharedConfig, hasFix };
}
} catch (error) {
this.log(error instanceof Error ? error.message : error, 'error');
console.trace(error);
Expand Down
5 changes: 4 additions & 1 deletion packages/contentstack-audit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export default {};
import Audit from "./commands/cm/stacks/audit";
import AuditFix from "./commands/cm/stacks/audit/fix";

export { Audit, AuditFix };
6 changes: 3 additions & 3 deletions packages/contentstack-audit/src/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class ContentType {
let canWrite = true;

if (!this.inMemoryFix && this.fix) {
if (!this.config.flags['copy-dir']) {
if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) {
canWrite = this.config.flags.yes ?? (await ux.confirm(commonMsg.FIX_CONFIRMATION));
}

Expand Down 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
37 changes: 23 additions & 14 deletions packages/contentstack-audit/src/modules/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ export default class Entries {
const { uid, title } = entry;
this.currentUid = uid;
this.currentTitle = title;
this.missingRefs[this.currentUid] = [];
this.lookForReference([{ uid, name: title }], ctSchema, this.entries[entryUid]);

if (!this.missingRefs[this.currentUid]) {
this.missingRefs[this.currentUid] = [];
}

this.lookForReference([{ locale: code, uid, name: title }], ctSchema, this.entries[entryUid]);
this.log(
$t(auditMsg.SCAN_ENTRY_SUCCESS_MSG, {
title,
Expand Down Expand Up @@ -156,7 +160,7 @@ export default class Entries {
let canWrite = true;

if (this.fix) {
if (!this.config.flags['copy-dir']) {
if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) {
canWrite = this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION));
}

Expand Down Expand Up @@ -451,6 +455,10 @@ export default class Entries {
schema.forEach((field) => {
const { uid, data_type } = field;

if (!Object(entry).hasOwnProperty(uid)) {
return;
}

switch (data_type) {
case 'global_field':
entry[uid] = this.fixGlobalFieldReferences(
Expand All @@ -474,7 +482,6 @@ export default class Entries {
break;
}
}

// NOTE Reference field
entry[uid] = this.fixMissingReferences(
[...tree, { uid: field.uid, name: field.display_name, data_type: field.data_type }],
Expand Down Expand Up @@ -623,19 +630,21 @@ export default class Entries {
return this.fixJsonRteMissingReferences([...tree, { index, type: child?.type, uid: child?.uid }], field, child);
}) as EntryJsonRTEFieldDataType[];
} else {
entry.children = entry.children
.map((child) => {
const refExist = this.jsonRefCheck(tree, field, child);
if (entry?.children) {
entry.children = entry.children
.map((child) => {
const refExist = this.jsonRefCheck(tree, field, child);

if (!refExist) return null;
if (!refExist) return null;

if (isEmpty(child.children)) {
child = this.fixJsonRteMissingReferences(tree, field, child) as EntryJsonRTEFieldDataType;
}
if (!isEmpty(child.children)) {
child = this.fixJsonRteMissingReferences(tree, field, child) as EntryJsonRTEFieldDataType;
}

return child;
})
.filter((val) => val) as EntryJsonRTEFieldDataType[];
return child;
})
.filter((val) => val) as EntryJsonRTEFieldDataType[];
}
}

return entry;
Expand Down
5 changes: 5 additions & 0 deletions packages/contentstack-audit/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ export type PrintType = {
bold?: boolean;
color?: typeof Color;
};

export type JSONFlagOptions = {
hidden?: boolean;
description?: string;
};
32 changes: 29 additions & 3 deletions packages/contentstack-audit/src/util/flags.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ux } from '@contentstack/cli-utilities';
import { FlagDefinition, Flags, ux } from '@contentstack/cli-utilities';

import { IFlags, IncludeFlags } from '../types';
import { IFlags, IncludeFlags, JSONFlagOptions } from '../types';
import { tableColumnDescriptions } from '../messages';

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 +30,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-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.9",
"@contentstack/cli-utilities": "~1.5.10",
"chalk": "^4.0.0",
"debug": "^4.1.1",
"inquirer": "8.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dependencies": {
"@contentstack/cli-cm-seed": "~1.7.0",
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.9",
"@contentstack/cli-utilities": "~1.5.10",
"inquirer": "8.2.4",
"mkdirp": "^1.0.4",
"tar": "^6.1.13"
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-branches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $ npm install -g @contentstack/cli-cm-branches
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-branches/1.0.19 darwin-arm64 node-v20.8.0
@contentstack/cli-cm-branches/1.0.20 darwin-arm64 node-v20.8.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-branches/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@contentstack/cli-cm-branches",
"description": "Contentstack CLI plugin to do branches operations",
"version": "1.0.19",
"version": "1.0.20",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.9",
"@contentstack/cli-utilities": "~1.5.10",
"@oclif/core": "^2.9.3",
"async": "^3.2.4",
"big-json": "^3.2.0",
Expand Down
15 changes: 11 additions & 4 deletions packages/contentstack-branches/src/branch/merge-handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os from 'os';
import path from 'path';
import forEach from 'lodash/forEach';
import { cliux } from '@contentstack/cli-utilities';
Expand Down Expand Up @@ -275,7 +276,7 @@ export default class MergeHandler {
deleted: [],
};

selectedMergeItems.forEach((item) => {
selectedMergeItems?.forEach((item) => {
mergeContent.content_types[item.status].push(item.value);
});
break;
Expand All @@ -290,10 +291,16 @@ export default class MergeHandler {
if (scriptFolderPath !== undefined) {
cliux.success(`\nSuccess! We have generated entry migration files in the folder ${scriptFolderPath}`);
cliux.print('\nWARNING!!! Migration is not intended to be run more than once. Migrated(entries/assets) will be duplicated if run more than once', {color: 'yellow'});

const migrationCommand = `csdx cm:stacks:migration --multiple --file-path ./${scriptFolderPath} --config {compare-branch:${mergePayload.compare_branch},file-path:./${scriptFolderPath}} --branch ${mergePayload.base_branch} --stack-api-key ${this.stackAPIKey}`;

let migrationCommand: string;
if(os.platform() === 'win32'){
migrationCommand = `csdx cm:stacks:migration --multiple --file-path ./${scriptFolderPath} --config compare-branch:${mergePayload.compare_branch},file-path:./${scriptFolderPath} --branch ${mergePayload.base_branch} --stack-api-key ${this.stackAPIKey}`;
}else{
migrationCommand = `csdx cm:stacks:migration --multiple --file-path ./${scriptFolderPath} --config {compare-branch:${mergePayload.compare_branch},file-path:./${scriptFolderPath}} --branch ${mergePayload.base_branch} --stack-api-key ${this.stackAPIKey}`;
}

cliux.print(
`\nKindly follow the steps in the guide "https://www.contentstack.com/docs/developers/cli/migrate-branch-entries" to update the migration scripts, and then run the command:\n\n${migrationCommand}`,
`\nKindly follow the steps in the guide "https://www.contentstack.com/docs/developers/cli/entry-migration" to update the migration scripts, and then run the command:\n\n${migrationCommand}`,
{ color: 'blue' },
);
}
Expand Down
Loading

0 comments on commit 07ba7da

Please sign in to comment.