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: Audit fix command for entry #1100

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 22 additions & 22 deletions packages/contentstack-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ $ csdx plugins:install @contentstack/cli-audit

## How to use this plugin

This plugin requires you to be authenticated using [csdx auth:login](https://www.contentstack.com/docs/developers/cli/authenticate-with-the-cli/).

<!-- usage -->
```sh-session
$ npm install -g @contentstack/cli-audit
Expand All @@ -30,20 +28,24 @@ USAGE

# Commands
<!-- commands -->
* [`csdx audit`](#csdx-audit)
* [`csdx audit:fix`](#csdx-auditfix)
* [`csdx cm:stacks:audit`](#csdx-cmstacksaudit)
* [`csdx cm:stacks:audit:fix`](#csdx-cmstacksauditfix)
* [`csdx help [COMMANDS]`](#csdx-help-commands)
* [`csdx plugins`](#csdx-plugins)
* [`csdx plugins:install PLUGIN...`](#csdx-pluginsinstall-plugin)
* [`csdx plugins:inspect PLUGIN...`](#csdx-pluginsinspect-plugin)
* [`csdx plugins:install PLUGIN...`](#csdx-pluginsinstall-plugin-1)
* [`csdx plugins:link PLUGIN`](#csdx-pluginslink-plugin)
* [`csdx plugins:uninstall PLUGIN...`](#csdx-pluginsuninstall-plugin)
* [`csdx plugins:uninstall PLUGIN...`](#csdx-pluginsuninstall-plugin-1)
* [`csdx plugins:uninstall PLUGIN...`](#csdx-pluginsuninstall-plugin-2)
* [`csdx plugins:update`](#csdx-pluginsupdate)
- [@contentstack/cli-audit](#contentstackcli-audit)
- [How to install this plugin](#how-to-install-this-plugin)
- [How to use this plugin](#how-to-use-this-plugin)
- [Commands](#commands)
- [`csdx audit`](#csdx-audit)
- [`csdx audit:fix`](#csdx-auditfix)
- [`csdx cm:stacks:audit`](#csdx-cmstacksaudit)
- [`csdx cm:stacks:audit:fix`](#csdx-cmstacksauditfix)
- [`csdx help [COMMANDS]`](#csdx-help-commands)
- [`csdx plugins`](#csdx-plugins)
- [`csdx plugins:install PLUGIN...`](#csdx-pluginsinstall-plugin)
- [`csdx plugins:inspect PLUGIN...`](#csdx-pluginsinspect-plugin)
- [`csdx plugins:install PLUGIN...`](#csdx-pluginsinstall-plugin-1)
- [`csdx plugins:link PLUGIN`](#csdx-pluginslink-plugin)
- [`csdx plugins:uninstall PLUGIN...`](#csdx-pluginsuninstall-plugin)
- [`csdx plugins:uninstall PLUGIN...`](#csdx-pluginsuninstall-plugin-1)
- [`csdx plugins:uninstall PLUGIN...`](#csdx-pluginsuninstall-plugin-2)
- [`csdx plugins:update`](#csdx-pluginsupdate)

## `csdx audit`

Expand Down Expand Up @@ -93,14 +95,13 @@ Audit and fix possible errors in the exported data
```
USAGE
$ csdx audit:fix [-c <value>] [-d <value>] [--report-path <value>] [--modules
content-types|global-fields|entries] [--backup-dir <value> --copy-dir] [-y] [--columns <value> | ] [--sort <value>]
content-types|global-fields|entries] [--backup-dir <value> --copy-dir] [--columns <value> | ] [--sort <value>]
[--filter <value>] [--csv | --no-truncate]

FLAGS
-c, --config=<value> Path of the external config.
-d, --data-dir=<value> Path where the data is stored.
-y, --yes Use this flag to skip confirmation
--backup-dir=<value> Provided path to backup original data
--backup-dir=<value> Provide the path to backup the copied data.
--columns=<value> only show provided columns (comma-separated)
--copy-dir Create backup from original data
--csv output is csv format [alias: --output=csv]
Expand Down Expand Up @@ -180,14 +181,13 @@ Audit and fix possible errors in the exported data
```
USAGE
$ csdx cm:stacks:audit:fix [-c <value>] [-d <value>] [--report-path <value>] [--modules
content-types|global-fields|entries] [--backup-dir <value> --copy-dir] [-y] [--columns <value> | ] [--sort <value>]
content-types|global-fields|entries] [--backup-dir <value> --copy-dir] [--columns <value> | ] [--sort <value>]
[--filter <value>] [--csv | --no-truncate]

FLAGS
-c, --config=<value> Path of the external config.
-d, --data-dir=<value> Path where the data is stored.
-y, --yes Use this flag to skip confirmation
--backup-dir=<value> Provided path to backup original data
--backup-dir=<value> Provide the path to backup the copied data.
--columns=<value> only show provided columns (comma-separated)
--copy-dir Create backup from original data
--csv output is csv format [alias: --output=csv]
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-audit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
],
"topics": {
"cm:stacks:audit": {
"description": "Audit and find possible errors in the exported data"
"description": "Perform audits and find possible errors in the exported Contentstack data"
},
"cm:stacks:audit:fix": {
"description": "Audit and fix possible errors in the exported data"
"description": "Perform audits and fix possible errors in the exported Contentstack data."
}
},
"repositoryPrefix": "<%- repo %>/blob/main/packages/contentstack-audit/<%- commandPath %>"
Expand Down
12 changes: 7 additions & 5 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { copy } from 'fs-extra';
import { v4 as uuid } from 'uuid';
import isEmpty from 'lodash/isEmpty';
import { join, resolve } from 'path';
import cloneDeep from 'lodash/cloneDeep';
import { cliux, ux } from '@contentstack/cli-utilities';
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';

Expand Down Expand Up @@ -80,15 +81,15 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma

switch (module) {
case 'content-types':
missingCtRefs = await new ContentType(constructorParam).run();
missingCtRefs = await new ContentType(cloneDeep(constructorParam)).run();
await this.prepareReport(module, missingCtRefs);
break;
case 'global-fields':
missingGfRefs = await new GlobalField(constructorParam).run();
missingGfRefs = await new GlobalField(cloneDeep(constructorParam)).run();
await this.prepareReport(module, missingGfRefs);
break;
case 'entries':
missingEntryRefs = await new Entries(constructorParam).run();
missingEntryRefs = await new Entries(cloneDeep(constructorParam)).run();
await this.prepareReport(module, missingEntryRefs);
break;
}
Expand Down Expand Up @@ -195,9 +196,10 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
message: ` ${module}`,
},
]);
const tableValues = Object.values(missingRefs).flat();

ux.table(
Object.values(missingRefs).flat(),
tableValues,
{
name: {
minWidth: 7,
Expand All @@ -220,7 +222,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
);
},
},
...this.fixStatus,
...(tableValues[0]?.fixStatus ? this.fixStatus : {}),
treeStr: {
minWidth: 7,
header: 'Path',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AuditBaseCommand } from '../../../../audit-base-command';
export default class AuditFix extends AuditBaseCommand {
static aliases: string[] = ['audit:fix', 'cm:stacks:audit:fix'];

static description = 'Audit and fix possible errors in the exported data';
static description = auditFixMsg.AUDIT_FIX_CMD_DESCRIPTION;

static examples = [
'$ <%= config.bin %> <%= command.id %> --copy-dir',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AuditBaseCommand } from '../../../../audit-base-command';
export default class Audit extends AuditBaseCommand {
static aliases: string[] = ['audit', 'cm:stacks:audit'];

static description = 'Perform audits and find possible errors in the exported Contentstack data';
static description = auditMsg.AUDIT_CMD_DESCRIPTION;

static examples = [
'$ <%= config.bin %> <%= command.id %>',
Expand Down
7 changes: 5 additions & 2 deletions packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const commonMsg = {
CONFIG: 'Path of the external config.',
CURRENT_WORKING_DIR: 'Current working directory.',
DATA_DIR: 'Path where the data is stored.',
FIX_CONFIRMATION: 'Would you like to overwrite existing file.?',
};

const auditMsg = {
Expand All @@ -19,12 +20,14 @@ const auditMsg = {
FINAL_REPORT_PATH: "Reports ready. Please find the reports at '{path}'.",
SCAN_CT_SUCCESS_MSG: "Successfully completed the scanning of {module} '{title}'.",
SCAN_ENTRY_SUCCESS_MSG: "Successfully completed the scanning of {module} ({local}) '{title}'.",
AUDIT_CMD_DESCRIPTION: 'Perform audits and find possible errors in the exported Contentstack data',
};

const auditFixMsg = {
COPY_DATA: 'Create backup from original data',
COPY_DATA: 'Create backup from the original data.',
BKP_PATH: 'Provide the path to backup the copied data.',
FIXED_CONTENT_PATH_MAG: 'You can locate the fixed content at {path}',
FIXED_CONTENT_PATH_MAG: 'You can locate the fixed content at {path}.',
AUDIT_FIX_CMD_DESCRIPTION: 'Perform audits and fix possible errors in the exported Contentstack data.',
};

const messages: typeof errors & typeof commonMsg & typeof auditMsg & typeof auditFixMsg = {
Expand Down
Loading