Skip to content

Commit ecf7a78

Browse files
Merge pull request #1421 from contentstack/staging
DX | 03-06-2024 | Release
2 parents 26ac975 + 956e121 commit ecf7a78

File tree

101 files changed

+1034
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1034
-723
lines changed

package-lock.json

Lines changed: 442 additions & 292 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-audit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-audit
1919
$ csdx COMMAND
2020
running command...
2121
$ csdx (--version|-v)
22-
@contentstack/cli-audit/1.6.1 darwin-arm64 node-v21.6.2
22+
@contentstack/cli-audit/1.6.2 darwin-arm64 node-v21.6.2
2323
$ csdx --help [COMMAND]
2424
USAGE
2525
$ csdx COMMAND

packages/contentstack-audit/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/cli-audit",
3-
"version": "1.6.1",
3+
"version": "1.6.2",
44
"description": "Contentstack audit plugin",
55
"author": "Contentstack CLI",
66
"homepage": "https://github.com/contentstack/cli",
@@ -19,7 +19,7 @@
1919
],
2020
"dependencies": {
2121
"@contentstack/cli-command": "~1.2.18",
22-
"@contentstack/cli-utilities": "~1.6.1",
22+
"@contentstack/cli-utilities": "~1.6.2",
2323
"@oclif/plugin-help": "^5",
2424
"@oclif/plugin-plugins": "^5.0.0",
2525
"chalk": "^4.1.2",

packages/contentstack-audit/src/audit-base-command.ts

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { v4 as uuid } from 'uuid';
55
import isEmpty from 'lodash/isEmpty';
66
import { join, resolve } from 'path';
77
import cloneDeep from 'lodash/cloneDeep';
8-
import { cliux, ux } from '@contentstack/cli-utilities';
8+
import { cliux, sanitizePath, ux } from '@contentstack/cli-utilities';
99
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'fs';
1010

1111
import config from './config';
@@ -396,7 +396,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
396396
}
397397

398398
// NOTE write int json
399-
writeFileSync(join(this.sharedConfig.reportPath, `${moduleName}.json`), JSON.stringify(listOfMissingRefs));
399+
writeFileSync(join(sanitizePath(this.sharedConfig.reportPath), `${sanitizePath(moduleName)}.json`), JSON.stringify(listOfMissingRefs));
400400

401401
// NOTE write into CSV
402402
return this.prepareCSV(moduleName, listOfMissingRefs);
@@ -416,50 +416,55 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
416416
moduleName: keyof typeof config.moduleConfig | keyof typeof config.ReportTitleForEntries,
417417
listOfMissingRefs: Record<string, any>,
418418
): Promise<void> {
419-
const csvPath = join(this.sharedConfig.reportPath, `${moduleName}.csv`);
420-
421-
return new Promise<void>((resolve, reject) => {
422-
// file deepcode ignore MissingClose: Will auto close once csv stream end
423-
const ws = createWriteStream(csvPath).on('error', reject);
424-
const defaultColumns = Object.keys(OutputColumn);
425-
const userDefinedColumns = this.sharedConfig.flags.columns ? this.sharedConfig.flags.columns.split(',') : null;
426-
let missingRefs: RefErrorReturnType[] | WorkflowExtensionsRefErrorReturnType[] =
427-
Object.values(listOfMissingRefs).flat();
428-
const columns: (keyof typeof OutputColumn)[] = userDefinedColumns
429-
? [...userDefinedColumns, ...defaultColumns.filter((val: string) => !userDefinedColumns.includes(val))]
430-
: defaultColumns;
431-
432-
if (this.sharedConfig.flags.filter) {
433-
const [column, value]: [keyof typeof OutputColumn, string] = this.sharedConfig.flags.filter.split('=');
434-
// Filter the missingRefs array
435-
missingRefs = missingRefs.filter((row) => {
436-
if (OutputColumn[column] in row) {
437-
const rowKey = OutputColumn[column] as keyof (RefErrorReturnType | WorkflowExtensionsRefErrorReturnType);
438-
return row[rowKey] === value;
439-
}
440-
return false;
441-
});
442-
}
419+
if (Object.keys(config.moduleConfig).includes(moduleName)) {
420+
const csvPath = join(sanitizePath(this.sharedConfig.reportPath), `${sanitizePath(moduleName)}.csv`);
421+
return new Promise<void>((resolve, reject) => {
422+
// file deepcode ignore MissingClose: Will auto close once csv stream end
423+
const ws = createWriteStream(csvPath).on('error', reject);
424+
const defaultColumns = Object.keys(OutputColumn);
425+
const userDefinedColumns = this.sharedConfig.flags.columns ? this.sharedConfig.flags.columns.split(',') : null;
426+
let missingRefs: RefErrorReturnType[] | WorkflowExtensionsRefErrorReturnType[] =
427+
Object.values(listOfMissingRefs).flat();
428+
const columns: (keyof typeof OutputColumn)[] = userDefinedColumns
429+
? [...userDefinedColumns, ...defaultColumns.filter((val: string) => !userDefinedColumns.includes(val))]
430+
: defaultColumns;
431+
432+
if (this.sharedConfig.flags.filter) {
433+
const [column, value]: [keyof typeof OutputColumn, string] = this.sharedConfig.flags.filter.split('=');
434+
// Filter the missingRefs array
435+
missingRefs = missingRefs.filter((row) => {
436+
if (OutputColumn[column] in row) {
437+
const rowKey = OutputColumn[column] as keyof (RefErrorReturnType | WorkflowExtensionsRefErrorReturnType);
438+
return row[rowKey] === value;
439+
}
440+
return false;
441+
});
442+
}
443443

444-
const rowData: Record<string, string | string[]>[] = [];
445-
for (const issue of missingRefs) {
446-
let row: Record<string, string | string[]> = {};
444+
const rowData: Record<string, string | string[]>[] = [];
445+
for (const issue of missingRefs) {
446+
let row: Record<string, string | string[]> = {};
447447

448-
for (const column of columns) {
449-
if (Object.keys(issue).includes(OutputColumn[column])) {
450-
const issueKey = OutputColumn[column] as keyof typeof issue;
451-
row[column] = issue[issueKey] as string;
452-
row[column] = typeof row[column] === 'object' ? JSON.stringify(row[column]) : row[column];
448+
for (const column of columns) {
449+
if (Object.keys(issue).includes(OutputColumn[column])) {
450+
const issueKey = OutputColumn[column] as keyof typeof issue;
451+
row[column] = issue[issueKey] as string;
452+
row[column] = typeof row[column] === 'object' ? JSON.stringify(row[column]) : row[column];
453+
}
453454
}
454-
}
455455

456-
if (this.currentCommand === 'cm:stacks:audit:fix') {
457-
row['Fix status'] = row.fixStatus;
458-
}
456+
if (this.currentCommand === 'cm:stacks:audit:fix') {
457+
row['Fix status'] = row.fixStatus;
458+
}
459459

460-
rowData.push(row);
461-
}
462-
csv.write(rowData, { headers: true }).pipe(ws).on('error', reject).on('finish', resolve);
463-
});
460+
rowData.push(row);
461+
}
462+
csv.write(rowData, { headers: true }).pipe(ws).on('error', reject).on('finish', resolve);
463+
});
464+
} else {
465+
return new Promise<void>((reject) => {
466+
return reject()
467+
})
468+
}
464469
}
465470
}

packages/contentstack-audit/src/messages/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import memoize from 'lodash/memoize';
2-
import { escapeRegExp } from '@contentstack/cli-utilities';
2+
import { escapeRegExp, validateRegex } from '@contentstack/cli-utilities';
33

44
const errors = {};
55

@@ -77,7 +77,12 @@ function $t(msg: string, args: Record<string, string>): string {
7777

7878
for (const key of Object.keys(args)) {
7979
const escapedKey = escapeRegExp(key);
80-
msg = msg.replace(new RegExp(`{${escapedKey}}`, 'g'), escapeRegExp(args[key]) || escapedKey);
80+
const escapedKeyRegex = new RegExp(`{${escapedKey}}`, 'g');
81+
let { status } = validateRegex(escapedKeyRegex)
82+
if (status === 'safe') {
83+
const sanitizedValue = args[key] ? escapeRegExp(args[key]) : '';
84+
msg = msg.replace(escapedKeyRegex, sanitizedValue || escapedKey);
85+
}
8186
}
8287

8388
return msg;

packages/contentstack-audit/src/modules/content-types.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import isEmpty from 'lodash/isEmpty';
44
import { join, resolve } from 'path';
55
import { existsSync, readFileSync, writeFileSync } from 'fs';
66

7-
import { ux } from '@contentstack/cli-utilities';
7+
import { sanitizePath, ux } from '@contentstack/cli-utilities';
88

99
import {
1010
LogFn,
@@ -51,11 +51,17 @@ export default class ContentType {
5151
this.fix = fix ?? false;
5252
this.ctSchema = ctSchema;
5353
this.gfSchema = gfSchema;
54-
this.moduleName = moduleName ?? 'content-types';
54+
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
5555
this.fileName = config.moduleConfig[this.moduleName].fileName;
56-
this.folderPath = resolve(config.basePath, config.moduleConfig[this.moduleName].dirName);
56+
this.folderPath = resolve(sanitizePath(config.basePath), sanitizePath(config.moduleConfig[this.moduleName].dirName));
5757
}
5858

59+
validateModules(moduleName: keyof typeof auditConfig.moduleConfig, moduleConfig: Record<string, unknown>): keyof typeof auditConfig.moduleConfig {
60+
if (Object.keys(moduleConfig).includes(moduleName)) {
61+
return moduleName;
62+
}
63+
return 'content-types'
64+
}
5965
/**
6066
* The `run` function checks if a folder path exists, sets the schema based on the module name,
6167
* iterates over the schema and looks for references, and returns a list of missing references.
@@ -115,7 +121,7 @@ export default class ContentType {
115121
if (existsSync(extensionPath)) {
116122
try {
117123
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
118-
} catch (error) {}
124+
} catch (error) { }
119125
}
120126

121127
if (existsSync(marketplacePath)) {
@@ -128,7 +134,7 @@ export default class ContentType {
128134
) as string[];
129135
this.extensions.push(...metaData);
130136
}
131-
} catch (error) {}
137+
} catch (error) { }
132138
}
133139
}
134140

@@ -183,8 +189,8 @@ export default class ContentType {
183189
...this.validateReferenceField(
184190
[...tree, { uid: field.uid, name: child.display_name }],
185191
child as ReferenceFieldDataType,
186-
),
187-
);
192+
),
193+
);
188194
break;
189195
case 'global_field':
190196
await this.validateGlobalField(
@@ -264,19 +270,19 @@ export default class ContentType {
264270

265271
return missingRefs.length
266272
? [
267-
{
268-
tree,
269-
data_type,
270-
missingRefs,
271-
display_name,
272-
ct_uid: this.currentUid,
273-
name: this.currentTitle,
274-
treeStr: tree
275-
.map(({ name }) => name)
276-
.filter((val) => val)
277-
.join(' ➜ '),
278-
},
279-
]
273+
{
274+
tree,
275+
data_type,
276+
missingRefs,
277+
display_name,
278+
ct_uid: this.currentUid,
279+
name: this.currentTitle,
280+
treeStr: tree
281+
.map(({ name }) => name)
282+
.filter((val) => val)
283+
.join(' ➜ '),
284+
},
285+
]
280286
: [];
281287
}
282288

@@ -392,19 +398,19 @@ export default class ContentType {
392398

393399
return missingRefs.length
394400
? [
395-
{
396-
tree,
397-
data_type,
398-
missingRefs,
399-
display_name,
400-
ct_uid: this.currentUid,
401-
name: this.currentTitle,
402-
treeStr: tree
403-
.map(({ name }) => name)
404-
.filter((val) => val)
405-
.join(' ➜ '),
406-
},
407-
]
401+
{
402+
tree,
403+
data_type,
404+
missingRefs,
405+
display_name,
406+
ct_uid: this.currentUid,
407+
name: this.currentTitle,
408+
treeStr: tree
409+
.map(({ name }) => name)
410+
.filter((val) => val)
411+
.join(' ➜ '),
412+
},
413+
]
408414
: [];
409415
}
410416

0 commit comments

Comments
 (0)