-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfield_rules.ts
More file actions
397 lines (350 loc) · 13.1 KB
/
field_rules.ts
File metadata and controls
397 lines (350 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import map from 'lodash/map';
import { join, resolve } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { FsUtility, Locale, sanitizePath, cliux } from '@contentstack/cli-utilities';
import {
LogFn,
ConfigType,
ModularBlockType,
ContentTypeStruct,
GroupFieldDataType,
CtConstructorParam,
GlobalFieldDataType,
ModularBlocksDataType,
ModuleConstructorParam,
EntryStruct,
FieldRuleStruct,
} from '../types';
import auditConfig from '../config';
import { $t, auditFixMsg, auditMsg, commonMsg } from '../messages';
import { MarketplaceAppsInstallationData } from '../types/extension';
import { values } from 'lodash';
/* The `ContentType` class is responsible for scanning content types, looking for references, and
generating a report in JSON and CSV formats. */
export default class FieldRule {
public log: LogFn;
protected fix: boolean;
public fileName: string;
public config: ConfigType;
public folderPath: string;
public currentUid!: string;
public currentTitle!: string;
public extensions: string[] = [];
public inMemoryFix: boolean = false;
public gfSchema: ContentTypeStruct[];
public ctSchema: ContentTypeStruct[];
protected schema: ContentTypeStruct[] = [];
protected missingRefs: Record<string, any> = {};
public moduleName: keyof typeof auditConfig.moduleConfig;
public schemaMap: any = [];
public locales!: Locale[];
protected entries!: Record<string, EntryStruct>;
protected missingSelectFeild: Record<string, any> = {};
protected missingMandatoryFields: Record<string, any> = {};
protected missingEnvLocale: Record<string, any> = {};
public entryMetaData: Record<string, any>[] = [];
public action: string[] = ['show', 'hide'];
constructor({ log, fix, config, moduleName, ctSchema, gfSchema }: ModuleConstructorParam & CtConstructorParam) {
this.log = log;
this.config = config;
this.fix = fix ?? false;
this.ctSchema = ctSchema;
this.gfSchema = gfSchema;
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
this.fileName = config.moduleConfig[this.moduleName].fileName;
this.folderPath = resolve(
sanitizePath(config.basePath),
sanitizePath(config.moduleConfig[this.moduleName].dirName),
);
}
validateModules(
moduleName: keyof typeof auditConfig.moduleConfig,
moduleConfig: Record<string, unknown>,
): keyof typeof auditConfig.moduleConfig {
if (Object.keys(moduleConfig).includes(moduleName)) {
return moduleName;
}
return 'content-types';
}
/**
* The `run` function checks if a folder path exists, sets the schema based on the module name,
* iterates over the schema and looks for references, and returns a list of missing references.
* @returns the `missingRefs` object.
*/
async run() {
if (!existsSync(this.folderPath)) {
this.log(`Skipping ${this.moduleName} audit`, 'warn');
this.log($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' });
return {};
}
this.schema = this.moduleName === 'content-types' ? this.ctSchema : this.gfSchema;
await this.prerequisiteData();
await this.prepareEntryMetaData();
for (const schema of this.schema ?? []) {
this.currentUid = schema.uid;
this.currentTitle = schema.title;
this.missingRefs[this.currentUid] = [];
const { uid, title } = schema;
await this.lookForReference([{ uid, name: title }], schema, null);
this.missingRefs[this.currentUid] = [];
if (this.fix) {
this.fixFieldRules(schema);
} else {
this.validateFieldRules(schema);
}
this.schemaMap = [];
this.log(
$t(auditMsg.SCAN_CT_SUCCESS_MSG, { title, module: this.config.moduleConfig[this.moduleName].name }),
'info',
);
}
if (this.fix) {
await this.writeFixContent();
}
for (let propName in this.missingRefs) {
if (!this.missingRefs[propName].length) {
delete this.missingRefs[propName];
}
}
return this.missingRefs;
}
validateFieldRules(schema: Record<string, unknown>): void {
if (Array.isArray(schema.field_rules)) {
let count = 0;
schema.field_rules.forEach((fr) => {
fr.actions.forEach((actions: { target_field: any }) => {
if (!this.schemaMap.includes(actions.target_field)) {
this.log(
$t(auditMsg.FIELD_RULE_TARGET_ABSENT, {
target_field: actions.target_field,
ctUid: schema.uid as string,
}),
'error',
);
this.addMissingReferences(actions);
}
this.log(
$t(auditMsg.FIELD_RULE_TARGET_SCAN_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
'info',
);
});
fr.conditions.forEach((actions: { operand_field: any }) => {
if (!this.schemaMap.includes(actions.operand_field)) {
this.addMissingReferences(actions);
this.log($t(auditMsg.FIELD_RULE_CONDITION_ABSENT, { condition_field: actions.operand_field }), 'error');
}
this.log(
$t(auditMsg.FIELD_RULE_CONDITION_SCAN_MESSAGE, { num: count.toString(), ctUid: schema.uid as string }),
'info',
);
});
count = count + 1;
});
}
}
fixFieldRules(schema: Record<string, unknown>): void {
if (!Array.isArray(schema.field_rules)) return;
schema.field_rules = schema.field_rules
.map((fr: FieldRuleStruct, index: number) => {
const validActions = fr.actions?.filter(action => {
const isValid = this.schemaMap.includes(action.target_field);
const logMsg = isValid
? auditMsg.FIELD_RULE_TARGET_SCAN_MESSAGE
: auditMsg.FIELD_RULE_TARGET_ABSENT;
this.log(
$t(logMsg, {
num: index.toString(),
ctUid: schema.uid as string,
...(action.target_field && { target_field: action.target_field })
}),
isValid ? 'info' : 'error'
);
if (!isValid) {
this.addMissingReferences(action, 'Fixed');
this.log(
$t(auditFixMsg.FIELD_RULE_FIX_MESSAGE, {
num: index.toString(),
ctUid: schema.uid as string
}),
'info'
);
}
return isValid;
}) ?? [];
const validConditions = fr.conditions?.filter(condition => {
const isValid = this.schemaMap.includes(condition.operand_field);
const logMsg = isValid
? auditMsg.FIELD_RULE_CONDITION_SCAN_MESSAGE
: auditMsg.FIELD_RULE_CONDITION_ABSENT;
this.log(
$t(logMsg, {
num: index.toString(),
ctUid: schema.uid as string,
...(condition.operand_field && { condition_field: condition.operand_field })
}),
isValid ? 'info' : 'error'
);
if (!isValid) {
this.addMissingReferences(condition, 'Fixed');
this.log(
$t(auditFixMsg.FIELD_RULE_FIX_MESSAGE, {
num: index.toString(),
ctUid: schema.uid as string
}),
'info'
);
}
return isValid;
}) ?? [];
return (validActions.length && validConditions.length) ? {
...fr,
actions: validActions,
conditions: validConditions
} : null;
})
.filter(Boolean);
}
addMissingReferences(actions: Record<string, unknown>, fixStatus?: string) {
if (fixStatus) {
this.missingRefs[this.currentUid].push({
ctUid: this.currentUid,
action: actions,
fixStatus: 'Fixed',
});
} else {
this.missingRefs[this.currentUid].push({ ctUid: this.currentUid, action: actions });
}
}
/**
* @method prerequisiteData
* The `prerequisiteData` function reads and parses JSON files to retrieve extension and marketplace
* app data, and stores them in the `extensions` array.
*/
async prerequisiteData(): Promise<void> {
const extensionPath = resolve(this.config.basePath, 'extensions', 'extensions.json');
const marketplacePath = resolve(this.config.basePath, 'marketplace_apps', 'marketplace_apps.json');
if (existsSync(extensionPath)) {
try {
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
} catch (error) {}
}
if (existsSync(marketplacePath)) {
try {
const marketplaceApps: MarketplaceAppsInstallationData[] = JSON.parse(readFileSync(marketplacePath, 'utf8'));
for (const app of marketplaceApps) {
const metaData = map(map(app?.ui_location?.locations, 'meta').flat(), 'extension_uid').filter(
(val) => val,
) as string[];
this.extensions.push(...metaData);
}
} catch (error) {}
}
}
/**
* The function checks if it can write the fix content to a file and if so, it writes the content as
* JSON to the specified file path.
*/
async writeFixContent(): Promise<void> {
let canWrite = true;
if (this.fix) {
if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) {
canWrite = this.config.flags.yes ?? (await cliux.confirm(commonMsg.FIX_CONFIRMATION));
}
if (canWrite) {
writeFileSync(
join(this.folderPath, this.config.moduleConfig[this.moduleName].fileName),
JSON.stringify(this.schema),
);
}
}
}
async lookForReference(
tree: Record<string, unknown>[],
field: ContentTypeStruct | GlobalFieldDataType | ModularBlockType | GroupFieldDataType,
parent: string | null = null,
): Promise<void> {
const fixTypes = this.config.flags['fix-only'] ?? this.config['fix-fields'];
for (let child of field.schema ?? []) {
if (parent !== null) {
this.schemaMap.push(`${parent}.${child?.uid}`);
} else {
this.schemaMap.push(child.uid);
}
if (!fixTypes.includes(child.data_type) && child.data_type !== 'json') continue;
switch (child.data_type) {
case 'global_field':
await this.validateGlobalField(
[...tree, { uid: child.uid, name: child.display_name }],
child as GlobalFieldDataType,
parent ? `${parent}.${child?.uid}` : child?.uid,
);
break;
case 'blocks':
await this.validateModularBlocksField(
[...tree, { uid: child.uid, name: child.display_name }],
child as ModularBlocksDataType,
parent ? `${parent}.${child?.uid}` : child?.uid,
);
break;
case 'group':
await this.validateGroupField(
[...tree, { uid: child.uid, name: child.display_name }],
child as GroupFieldDataType,
parent ?`${parent}.${child?.uid}` : child?.uid,
);
break;
}
}
}
async validateGlobalField(
tree: Record<string, unknown>[],
field: GlobalFieldDataType,
parent: string | null,
): Promise<void> {
await this.lookForReference(tree, field, parent);
}
async validateModularBlocksField(
tree: Record<string, unknown>[],
field: ModularBlocksDataType,
parent: string | null,
): Promise<void> {
const { blocks } = field;
for (const block of blocks) {
const { uid, title } = block;
await this.lookForReference([...tree, { uid, name: title }], block, parent + '.' + block.uid);
}
}
async validateGroupField(
tree: Record<string, unknown>[],
field: GroupFieldDataType,
parent: string | null,
): Promise<void> {
// NOTE Any Group Field related logic can be added here (Ex data serialization or picking any metadata for report etc.,)
await this.lookForReference(tree, field, parent);
}
async prepareEntryMetaData() {
this.log(auditMsg.PREPARING_ENTRY_METADATA, 'info');
const localesFolderPath = resolve(this.config.basePath, this.config.moduleConfig.locales.dirName);
const localesPath = join(localesFolderPath, this.config.moduleConfig.locales.fileName);
const masterLocalesPath = join(localesFolderPath, 'master-locale.json');
this.locales = existsSync(masterLocalesPath) ? values(JSON.parse(readFileSync(masterLocalesPath, 'utf8'))) : [];
if (existsSync(localesPath)) {
this.locales.push(...values(JSON.parse(readFileSync(localesPath, 'utf8'))));
}
const entriesFolderPath = resolve(sanitizePath(this.config.basePath), 'entries');
for (const { code } of this.locales) {
for (const { uid } of this.ctSchema??[]) {
let basePath = join(entriesFolderPath, uid, code);
let fsUtility = new FsUtility({ basePath, indexFileName: 'index.json' });
let indexer = fsUtility.indexFileContent;
for (const _ in indexer) {
const entries = (await fsUtility.readChunkFiles.next()) as Record<string, EntryStruct>;
for (const entryUid in entries) {
let { title } = entries[entryUid];
this.entryMetaData.push({ uid: entryUid, title, ctUid: uid });
}
}
}
}
}
}