Skip to content

Commit

Permalink
Feat: Add missing global fields schema in to content type if global f…
Browse files Browse the repository at this point in the history
…ield exists
  • Loading branch information
antonyagustine committed Nov 21, 2023
1 parent ed39365 commit 1c80b23
Show file tree
Hide file tree
Showing 23 changed files with 411 additions and 131 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ contentstack-cli-logs
packages/**/package-lock.json
.dccache
yarn.lock
contents-*
contents-*
*.http
*.todo
6 changes: 2 additions & 4 deletions package-lock.json

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

94 changes: 60 additions & 34 deletions packages/contentstack-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ USAGE
--no-truncate]
FLAGS
--modules=<option>... Provide the list of modules to be audited
<options: content-types|global-fields|entries>
--report-path=<value> Path to store the audit reports
COMMON FLAGS
-c, --config=<value> Path of the external config
-d, --data-dir=<value> Path where the data is stored
--columns=<value> only show provided columns (comma-separated)
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--modules=<option>... Provide the list of modules to be audited
<options: content-types|global-fields|entries>
--no-truncate do not truncate output to fit screen
--report-path=<value> Path to store the audit reports
--sort=<value> property to sort by (prepend '-' for descending)
TABLE FLAGS
--columns=<value> do not truncate output to fit screen
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--no-truncate do not truncate output to fit screen
--sort=<value> property to sort by (prepend '-' for descending)
DESCRIPTION
Perform audits and find possible errors in the exported Contentstack data
Expand Down Expand Up @@ -93,22 +97,29 @@ Perform audits and fix possible errors in the exported Contentstack data.
```
USAGE
$ csdx audit:fix [-c <value>] [-d <value>] [--report-path <value>] [--modules
content-types|global-fields|entries] [--copy-path <value> --copy-dir] [--columns <value> | ] [--sort <value>]
[--filter <value>] [--csv | --no-truncate]
content-types|global-fields|entries] [--copy-path <value> --copy-dir] [--fix-only
reference|global_field|json:rte|json:custom-field|blocks|group] [--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
--columns=<value> only show provided columns (comma-separated)
--copy-dir Create backup from the original data.
--copy-path=<value> Provide the path to backup the copied data
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--fix-only=<option>... Provide the list of fix options
<options: reference|global_field|json:rte|json:custom-field|blocks|group>
--modules=<option>... Provide the list of modules to be audited
<options: content-types|global-fields|entries>
--no-truncate do not truncate output to fit screen
--report-path=<value> Path to store the audit reports
--sort=<value> property to sort by (prepend '-' for descending)
COMMON FLAGS
-c, --config=<value> Path of the external config
-d, --data-dir=<value> Path where the data is stored
TABLE FLAGS
--columns=<value> do not truncate output to fit screen
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--no-truncate do not truncate output to fit screen
--sort=<value> property to sort by (prepend '-' for descending)
DESCRIPTION
Perform audits and fix possible errors in the exported Contentstack data.
Expand All @@ -124,6 +135,8 @@ EXAMPLES
$ csdx audit:fix --report-path=<path> --copy-dir --csv
$ csdx audit:fix --fix-only=reference,global_field --copy-dir
$ csdx audit:fix --report-path=<path> --filter="name=<filter-value>"
$ csdx audit:fix --report-path=<path> --modules=content-types --filter="name="<filter-value>" --copy-dir --copy-path=<path>
Expand All @@ -140,16 +153,20 @@ USAGE
--no-truncate]
FLAGS
--modules=<option>... Provide the list of modules to be audited
<options: content-types|global-fields|entries>
--report-path=<value> Path to store the audit reports
COMMON FLAGS
-c, --config=<value> Path of the external config
-d, --data-dir=<value> Path where the data is stored
--columns=<value> only show provided columns (comma-separated)
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--modules=<option>... Provide the list of modules to be audited
<options: content-types|global-fields|entries>
--no-truncate do not truncate output to fit screen
--report-path=<value> Path to store the audit reports
--sort=<value> property to sort by (prepend '-' for descending)
TABLE FLAGS
--columns=<value> do not truncate output to fit screen
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--no-truncate do not truncate output to fit screen
--sort=<value> property to sort by (prepend '-' for descending)
DESCRIPTION
Perform audits and find possible errors in the exported Contentstack data
Expand Down Expand Up @@ -179,22 +196,29 @@ Perform audits and fix possible errors in the exported Contentstack data.
```
USAGE
$ csdx cm:stacks:audit:fix [-c <value>] [-d <value>] [--report-path <value>] [--modules
content-types|global-fields|entries] [--copy-path <value> --copy-dir] [--columns <value> | ] [--sort <value>]
[--filter <value>] [--csv | --no-truncate]
content-types|global-fields|entries] [--copy-path <value> --copy-dir] [--fix-only
reference|global_field|json:rte|json:custom-field|blocks|group] [--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
--columns=<value> only show provided columns (comma-separated)
--copy-dir Create backup from the original data.
--copy-path=<value> Provide the path to backup the copied data
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--fix-only=<option>... Provide the list of fix options
<options: reference|global_field|json:rte|json:custom-field|blocks|group>
--modules=<option>... Provide the list of modules to be audited
<options: content-types|global-fields|entries>
--no-truncate do not truncate output to fit screen
--report-path=<value> Path to store the audit reports
--sort=<value> property to sort by (prepend '-' for descending)
COMMON FLAGS
-c, --config=<value> Path of the external config
-d, --data-dir=<value> Path where the data is stored
TABLE FLAGS
--columns=<value> do not truncate output to fit screen
--csv output is csv format [alias: --output=csv]
--filter=<value> filter property by partial string matching, ex: name=foo
--no-truncate do not truncate output to fit screen
--sort=<value> property to sort by (prepend '-' for descending)
DESCRIPTION
Perform audits and fix possible errors in the exported Contentstack data.
Expand All @@ -210,6 +234,8 @@ EXAMPLES
$ csdx cm:stacks:audit:fix --report-path=<path> --copy-dir --csv
$ csdx cm:stacks:audit:fix --fix-only=reference,global_field --copy-dir
$ csdx cm:stacks:audit:fix --report-path=<path> --filter="name=<filter-value>"
$ csdx cm:stacks:audit:fix --report-path=<path> --modules=content-types --filter="name="<filter-value>" --copy-dir --copy-path=<path>
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-audit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"eslint-config-oclif-typescript": "^1.0.3",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"oclif": "^3.10.0",
"oclif": "^3",
"shx": "^0.3.4",
"sinon": "^17.0.0",
"ts-jest": "^29.1.1",
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 @@ -6,17 +6,17 @@ 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';
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'fs';

import config from './config';
import { print } from './util/log';
import { auditMsg } from './messages';
import { BaseCommand } from './base-command';
import { Entries, GlobalField, ContentType } from './modules';
import { ContentTypeStruct, OutputColumn, RefErrorReturnType } from './types';
import { CommandNames, ContentTypeStruct, OutputColumn, RefErrorReturnType } from './types';

export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseCommand> {
private currentCommand!: string;
private currentCommand!: CommandNames;

get fixStatus() {
return {
Expand All @@ -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: string): Promise<void> {
async start(command: CommandNames): Promise<void> {
this.currentCommand = command;
await this.promptQueue();
await this.createBackUp();
Expand All @@ -59,6 +59,11 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
} else {
this.log(this.messages.NO_MISSING_REF_FOUND, 'info');
this.log('');

if (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 @@ -280,7 +285,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
const csvStream = csv.format({ headers: true });
const csvPath = join(this.sharedConfig.reportPath, `${moduleName}.csv`);
const assetFileStream = createWriteStream(csvPath);

return new Promise<void>((resolve, reject) => {
assetFileStream.on('error', reject);
csvStream.pipe(assetFileStream).on('close', resolve).on('error', reject);
Expand Down Expand Up @@ -312,7 +317,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
}

csvStream.end();
assetFileStream.destroy()
assetFileStream.destroy();
});
}
}
2 changes: 2 additions & 0 deletions packages/contentstack-audit/src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
static baseFlags: FlagInput = {
config: Flags.string({
char: 'c',
helpGroup: 'COMMON',
description: commonMsg.CONFIG,
}),
'data-dir': Flags.string({
char: 'd',
helpGroup: 'COMMON',
description: commonMsg.DATA_DIR,
}),
};
Expand Down
11 changes: 8 additions & 3 deletions packages/contentstack-audit/src/commands/cm/stacks/audit/fix.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FlagInput, Flags, ux } from '@contentstack/cli-utilities';

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

Expand All @@ -13,6 +14,7 @@ export default class AuditFix extends AuditBaseCommand {
'$ <%= config.bin %> <%= command.id %> --copy-dir',
'$ <%= config.bin %> <%= command.id %> --report-path=<path> --copy-dir',
'$ <%= config.bin %> <%= command.id %> --report-path=<path> --copy-dir --csv',
'$ <%= config.bin %> <%= command.id %> --fix-only=reference,global_field --copy-dir',
'$ <%= config.bin %> <%= command.id %> --report-path=<path> --filter="name=<filter-value>"',
'$ <%= config.bin %> <%= command.id %> --report-path=<path> --modules=content-types --filter="name="<filter-value>" --copy-dir --copy-path=<path>',
];
Expand All @@ -37,14 +39,17 @@ export default class AuditFix extends AuditBaseCommand {
dependsOn: ['copy-dir'],
description: auditFixMsg.BKP_PATH,
}),
'fix-only': Flags.string({
multiple: true,
options: config['fix-fields'],
description: auditFixMsg.FIX_OPTIONS,
}),
yes: Flags.boolean({
char: 'y',
hidden: true,
description: 'Use this flag to skip confirmation',
}),
...ux.table.flags({
only: ['columns', 'sort', 'filter', 'csv', 'no-truncate'],
}),
...getTableFlags(),
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FlagInput, Flags, ux } from '@contentstack/cli-utilities';
import config from '../../../../config';
import { auditMsg } from '../../../../messages';
import { AuditBaseCommand } from '../../../../audit-base-command';
import { getTableFlags } from '../../../../util';

export default class Audit extends AuditBaseCommand {
static aliases: string[] = ['audit', 'cm:stacks:audit'];
Expand Down Expand Up @@ -30,9 +31,7 @@ export default class Audit extends AuditBaseCommand {
options: config.modules,
description: auditMsg.MODULES,
}),
...ux.table.flags({
only: ['columns', 'sort', 'filter', 'csv', 'no-truncate'],
}),
...getTableFlags(),
};

/**
Expand Down
1 change: 1 addition & 0 deletions packages/contentstack-audit/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const config = {
showTerminalOutput: true,
skipRefs: ['sys_assets'],
modules: ['content-types', 'global-fields', 'entries'],
'fix-fields': ['reference', 'global_field', 'json:rte', 'json:custom-field', 'blocks', 'group'],
moduleConfig: {
'content-types': {
name: 'content type',
Expand Down
18 changes: 16 additions & 2 deletions packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const errors = {};

const tableColumnDescriptions = {
TABLE_COLUMN: 'only show provided columns (comma-separated)',
TABLE_SORT: "property to sort by (prepend '-' for descending)",
TABLE_CSV: 'output is csv format [alias: --output=csv]',
TABLE_FILTER: 'filter property by partial string matching, ex: name=foo',
'TABLE_NO-TRUNCATE': 'do not truncate output to fit screen',
};

const commonMsg = {
CONFIG: 'Path of the external config',
DATA_DIR: 'Path where the data is stored',
Expand All @@ -23,16 +31,22 @@ const auditMsg = {
const auditFixMsg = {
COPY_DATA: 'Create backup from the original data.',
BKP_PATH: 'Provide the path to backup the copied data',
FIX_OPTIONS: 'Provide the list of fix options',
FIXED_CONTENT_PATH_MAG: 'You can locate the fixed content at {path}.',
EMPTY_FIX_MSG: 'Successfully removed the empty field/block found at {path} from the schema.',
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 = {
const messages: typeof errors &
typeof commonMsg &
typeof auditMsg &
typeof auditFixMsg &
typeof tableColumnDescriptions = {
...errors,
...commonMsg,
...auditMsg,
...auditFixMsg,
...tableColumnDescriptions,
};

/**
Expand All @@ -55,4 +69,4 @@ function $t(msg: string, args: Record<string, string>): string {
}

export default messages;
export { $t, errors, commonMsg, auditMsg, auditFixMsg };
export { $t, errors, commonMsg, auditMsg, auditFixMsg, tableColumnDescriptions };
Loading

0 comments on commit 1c80b23

Please sign in to comment.