-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets.ts
More file actions
272 lines (239 loc) · 12.9 KB
/
assets.ts
File metadata and controls
272 lines (239 loc) · 12.9 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
import { join, resolve } from 'path';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { FsUtility, sanitizePath, cliux, log } from '@contentstack/cli-utilities';
import {
ContentTypeStruct,
CtConstructorParam,
ModuleConstructorParam,
EntryStruct,
} from '../types';
import auditConfig from '../config';
import { $t, auditFixMsg, auditMsg, commonMsg } from '../messages';
import values from 'lodash/values';
import { keys } from 'lodash';
import BaseClass from './base-class';
/* The `Assets` class is responsible for scanning assets, looking for missing environment/locale references,
and generating a report in JSON and CSV formats. */
export default class Assets extends BaseClass {
protected fix: boolean;
public fileName: string;
public folderPath: string;
public currentUid!: string;
public currentTitle!: string;
public assets!: Record<string, any>;
public locales: string[] = [];
public environments: string[] = [];
protected schema: ContentTypeStruct[] = [];
protected missingEnvLocales: Record<string, any> = {};
public moduleName: keyof typeof auditConfig.moduleConfig;
private fixOverwriteConfirmed: boolean | null = null;
constructor({ fix, config, moduleName }: ModuleConstructorParam & CtConstructorParam) {
super({ config });
this.fix = fix ?? false;
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 'assets';
}
/**
* 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.
* @param returnFixSchema - If true, returns the fixed schema instead of missing references
* @param totalCount - Total number of assets to process (for progress tracking)
* @returns the `missingEnvLocales` object.
*/
async run(returnFixSchema = false, totalCount?: number) {
try {
log.debug(`Starting ${this.moduleName} audit process`, this.config.auditContext);
log.debug(`Data directory: ${this.folderPath}`, this.config.auditContext);
log.debug(`Fix mode: ${this.fix}`, this.config.auditContext);
if (!existsSync(this.folderPath)) {
log.debug(`Skipping ${this.moduleName} audit - path does not exist`, this.config.auditContext);
log.warn(`Skipping ${this.moduleName} audit`, this.config.auditContext);
cliux.print($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' });
return returnFixSchema ? [] : {};
}
// Load prerequisite data with loading spinner
await this.withLoadingSpinner('ASSETS: Loading prerequisite data (locales and environments)...', async () => {
await this.prerequisiteData();
});
// Create progress manager if we have a total count
if (totalCount && totalCount > 0) {
const progress = this.createSimpleProgress(this.moduleName, totalCount);
progress.updateStatus('Validating asset references...');
}
log.debug('Starting asset Reference, Environment and Locale validation', this.config.auditContext);
await this.lookForReference();
if (returnFixSchema) {
log.debug(`Returning fixed schema with ${this.schema?.length || 0} items`, this.config.auditContext);
return this.schema;
}
log.debug('Cleaning up empty missing environment/locale references', this.config.auditContext);
for (let propName in this.missingEnvLocales) {
if (Array.isArray(this.missingEnvLocales[propName])) {
if (!this.missingEnvLocales[propName].length) {
delete this.missingEnvLocales[propName];
}
}
}
const totalIssues = Object.keys(this.missingEnvLocales).length;
log.debug(`${this.moduleName} audit completed. Found ${totalIssues} assets with missing environment/locale references`, this.config.auditContext);
this.completeProgress(true);
return this.missingEnvLocales;
} catch (error: any) {
this.completeProgress(false, error?.message || 'Assets audit failed');
throw error;
}
}
/**
* @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() {
log.debug('Loading prerequisite data (locales and environments)', this.config.auditContext);
log.info(auditMsg.PREPARING_ENTRY_METADATA, this.config.auditContext);
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');
log.debug(`Loading locales from: ${localesFolderPath}`, this.config.auditContext);
log.debug(`Master locales path: ${masterLocalesPath}`, this.config.auditContext);
log.debug(`Locales path: ${localesPath}`, this.config.auditContext);
this.locales = existsSync(masterLocalesPath) ? values(JSON.parse(readFileSync(masterLocalesPath, 'utf8'))) : [];
log.debug(`Loaded ${this.locales.length} locales from master-locale.json`, this.config.auditContext);
if (existsSync(localesPath)) {
log.debug(`Loading additional locales from: ${localesPath}`, this.config.auditContext);
const additionalLocales = values(JSON.parse(readFileSync(localesPath, 'utf8')));
this.locales.push(...additionalLocales);
log.debug(`Added ${additionalLocales.length} additional locales`, this.config.auditContext);
} else {
log.debug('No additional locales file found', this.config.auditContext);
}
this.locales = this.locales.map((locale: any) => locale.code);
log.debug(`Total locales loaded: ${this.locales.length}`, this.config.auditContext);
log.debug(`Locale codes: ${this.locales.join(', ')}`, this.config.auditContext);
const environmentPath = resolve(
this.config.basePath,
this.config.moduleConfig.environments.dirName,
this.config.moduleConfig.environments.fileName,
);
log.debug(`Loading environments from: ${environmentPath}`, this.config.auditContext);
this.environments = existsSync(environmentPath) ? keys(JSON.parse(readFileSync(environmentPath, 'utf8'))) : [];
log.debug(`Total environments loaded: ${this.environments.length}`, this.config.auditContext);
log.debug(`Environment names: ${this.environments.join(', ')}`, this.config.auditContext);
}
/**
* 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(filePath: string, schema: Record<string, EntryStruct>) {
log.debug(`Starting writeFixContent process for: ${filePath}`, this.config.auditContext);
let canWrite = true;
if (this.fix) {
log.debug('Fix mode enabled, checking write permissions', this.config.auditContext);
if (this.config.flags['copy-dir'] || this.config.flags['external-config']?.skipConfirm || this.config.flags.yes) {
this.fixOverwriteConfirmed = true;
log.debug('Skipping confirmation due to copy-dir, external-config, or yes flags', this.config.auditContext);
} else if (this.fixOverwriteConfirmed !== null) {
canWrite = this.fixOverwriteConfirmed;
log.debug(`Using cached overwrite confirmation: ${canWrite}`, this.config.auditContext);
} else {
log.debug(`Asking user for confirmation to write fix content (--yes flag: ${this.config.flags.yes})`, this.config.auditContext);
this.completeProgress(true);
canWrite = await cliux.confirm(commonMsg.FIX_CONFIRMATION);
this.fixOverwriteConfirmed = canWrite;
}
if (canWrite) {
log.debug(`Writing fixed assets to: ${filePath}`, this.config.auditContext);
writeFileSync(filePath, JSON.stringify(schema));
log.debug(`Successfully wrote ${Object.keys(schema).length} assets to file`, this.config.auditContext);
} else {
log.debug('User declined to write fix content', this.config.auditContext);
}
} else {
log.debug('Skipping writeFixContent - not in fix mode', this.config.auditContext);
}
}
/**
* This function traverses over the publish details of the assets and removes the publish details where the locale or environment does not exist
*/
async lookForReference(): Promise<void> {
log.debug('Starting asset reference validation', this.config.auditContext);
let basePath = join(this.folderPath);
log.debug(`Assets base path: ${basePath}`, this.config.auditContext);
let fsUtility = new FsUtility({ basePath, indexFileName: 'assets.json' });
let indexer = fsUtility.indexFileContent;
log.debug(`Found ${Object.keys(indexer).length} asset files to process`, this.config.auditContext);
for (const fileIndex in indexer) {
log.debug(`Processing asset file: ${indexer[fileIndex]}`, this.config.auditContext);
const assets = (await fsUtility.readChunkFiles.next()) as Record<string, EntryStruct>;
this.assets = assets;
log.debug(`Loaded ${Object.keys(assets).length} assets from file`, this.config.auditContext);
for (const assetUid in assets) {
log.debug(`Processing asset: ${assetUid}`, this.config.auditContext);
if (this.assets[assetUid]?.publish_details && !Array.isArray(this.assets[assetUid].publish_details)) {
log.debug(`Asset ${assetUid} has invalid publish_details format`, this.config.auditContext);
cliux.print($t(auditMsg.ASSET_NOT_EXIST, { uid: assetUid }), { color: 'red' });
this.assets[assetUid].publish_details = [];
}
const publishDetails = this.assets[assetUid]?.publish_details;
log.debug(`Asset ${assetUid} has ${publishDetails?.length || 0} publish details`, this.config.auditContext);
if (Array.isArray(this.assets[assetUid].publish_details)) {
this.assets[assetUid].publish_details = this.assets[assetUid].publish_details.filter((pd: any) => {
log.debug(`Checking publish detail: locale=${pd?.locale}, environment=${pd?.environment}`, this.config.auditContext);
if (this.locales?.includes(pd?.locale) && this.environments?.includes(pd?.environment)) {
log.debug(`Publish detail valid for asset ${assetUid}: locale=${pd.locale}, environment=${pd.environment}`, this.config.auditContext);
return true;
} else {
log.debug(`Publish detail invalid for asset ${assetUid}: locale=${pd.locale}, environment=${pd.environment}`, this.config.auditContext);
cliux.print(
$t(auditMsg.SCAN_ASSET_WARN_MSG, { uid: assetUid, locale: pd.locale, environment: pd.environment }),
{ color: 'yellow' },
);
if (!Object.keys(this.missingEnvLocales).includes(assetUid)) {
log.debug(`Creating new missing reference entry for asset ${assetUid}`, this.config.auditContext);
this.missingEnvLocales[assetUid] = [
{ asset_uid: assetUid, publish_locale: pd.locale, publish_environment: pd.environment },
];
} else {
log.debug(`Adding to existing missing reference entry for asset ${assetUid}`, this.config.auditContext);
this.missingEnvLocales[assetUid].push({
asset_uid: assetUid,
publish_locale: pd.locale,
publish_environment: pd.environment,
});
}
return false;
}
});
}
log.info($t(auditMsg.SCAN_ASSET_SUCCESS_MSG, { uid: assetUid }), this.config.auditContext);
const remainingPublishDetails = this.assets[assetUid].publish_details?.length || 0;
log.debug(`Asset ${assetUid} now has ${remainingPublishDetails} valid publish details`, this.config.auditContext);
// Track progress for each asset processed
if (this.progressManager) {
this.progressManager.tick(true, `asset: ${assetUid}`, null);
}
if (this.fix) {
log.debug(`Fixing asset ${assetUid}`, this.config.auditContext);
log.info($t(auditFixMsg.ASSET_FIX, { uid: assetUid }), this.config.auditContext);
}
}
if (this.fix) {
await this.writeFixContent(`${basePath}/${indexer[fileIndex]}`, this.assets);
}
}
log.debug(`Asset reference validation completed. Processed ${Object.keys(this.missingEnvLocales).length} assets with issues`, this.config.auditContext);
}
}