|
| 1 | +import { join, resolve } from 'path'; |
| 2 | +import { existsSync, readFileSync, writeFileSync } from 'fs'; |
| 3 | + |
| 4 | +import { FsUtility, sanitizePath, ux } from '@contentstack/cli-utilities'; |
| 5 | + |
| 6 | +import { |
| 7 | + LogFn, |
| 8 | + ConfigType, |
| 9 | + ContentTypeStruct, |
| 10 | + CtConstructorParam, |
| 11 | + ModuleConstructorParam, |
| 12 | + EntryStruct, |
| 13 | +} from '../types'; |
| 14 | +import auditConfig from '../config'; |
| 15 | +import { $t, auditFixMsg, auditMsg, commonMsg } from '../messages'; |
| 16 | +import values from 'lodash/values'; |
| 17 | +import { keys } from 'lodash'; |
| 18 | + |
| 19 | +/* The `ContentType` class is responsible for scanning content types, looking for references, and |
| 20 | +generating a report in JSON and CSV formats. */ |
| 21 | +export default class Assets { |
| 22 | + public log: LogFn; |
| 23 | + protected fix: boolean; |
| 24 | + public fileName: string; |
| 25 | + public config: ConfigType; |
| 26 | + public folderPath: string; |
| 27 | + public currentUid!: string; |
| 28 | + public currentTitle!: string; |
| 29 | + public assets!: Record<string, any>; |
| 30 | + public locales: string[] = []; |
| 31 | + public environments: string[] = []; |
| 32 | + protected schema: ContentTypeStruct[] = []; |
| 33 | + protected missingEnvLocales: Record<string, any> = {}; |
| 34 | + public moduleName: keyof typeof auditConfig.moduleConfig; |
| 35 | + |
| 36 | + constructor({ log, fix, config, moduleName }: ModuleConstructorParam & CtConstructorParam) { |
| 37 | + this.log = log; |
| 38 | + this.config = config; |
| 39 | + this.fix = fix ?? false; |
| 40 | + this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig); |
| 41 | + this.fileName = config.moduleConfig[this.moduleName].fileName; |
| 42 | + this.folderPath = resolve( |
| 43 | + sanitizePath(config.basePath), |
| 44 | + sanitizePath(config.moduleConfig[this.moduleName].dirName), |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + validateModules( |
| 49 | + moduleName: keyof typeof auditConfig.moduleConfig, |
| 50 | + moduleConfig: Record<string, unknown>, |
| 51 | + ): keyof typeof auditConfig.moduleConfig { |
| 52 | + if (Object.keys(moduleConfig).includes(moduleName)) { |
| 53 | + return moduleName; |
| 54 | + } |
| 55 | + return 'assets'; |
| 56 | + } |
| 57 | + /** |
| 58 | + * The `run` function checks if a folder path exists, sets the schema based on the module name, |
| 59 | + * iterates over the schema and looks for references, and returns a list of missing references. |
| 60 | + * @returns the `missingEnvLocales` object. |
| 61 | + */ |
| 62 | + async run(returnFixSchema = false) { |
| 63 | + if (!existsSync(this.folderPath)) { |
| 64 | + this.log(`Skipping ${this.moduleName} audit`, 'warn'); |
| 65 | + this.log($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' }); |
| 66 | + return returnFixSchema ? [] : {}; |
| 67 | + } |
| 68 | + |
| 69 | + await this.prerequisiteData(); |
| 70 | + await this.lookForReference(); |
| 71 | + |
| 72 | + if (returnFixSchema) { |
| 73 | + return this.schema; |
| 74 | + } |
| 75 | + |
| 76 | + for (let propName in this.missingEnvLocales) { |
| 77 | + if (!this.missingEnvLocales[propName].length) { |
| 78 | + delete this.missingEnvLocales[propName]; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return this.missingEnvLocales; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @method prerequisiteData |
| 87 | + * The `prerequisiteData` function reads and parses JSON files to retrieve extension and marketplace |
| 88 | + * app data, and stores them in the `extensions` array. |
| 89 | + */ |
| 90 | + async prerequisiteData() { |
| 91 | + this.log(auditMsg.PREPARING_ENTRY_METADATA, 'info'); |
| 92 | + |
| 93 | + const localesFolderPath = resolve(this.config.basePath, this.config.moduleConfig.locales.dirName); |
| 94 | + const localesPath = join(localesFolderPath, this.config.moduleConfig.locales.fileName); |
| 95 | + const masterLocalesPath = join(localesFolderPath, 'master-locale.json'); |
| 96 | + this.locales = existsSync(masterLocalesPath) ? values(JSON.parse(readFileSync(masterLocalesPath, 'utf8'))) : []; |
| 97 | + |
| 98 | + if (existsSync(localesPath)) { |
| 99 | + this.locales.push(...values(JSON.parse(readFileSync(localesPath, 'utf8')))); |
| 100 | + } |
| 101 | + this.locales = this.locales.map((locale: any) => locale.code); |
| 102 | + const environmentPath = resolve( |
| 103 | + this.config.basePath, |
| 104 | + this.config.moduleConfig.environments.dirName, |
| 105 | + this.config.moduleConfig.environments.fileName, |
| 106 | + ); |
| 107 | + this.environments = existsSync(environmentPath) ? keys(JSON.parse(readFileSync(environmentPath, 'utf8'))) : []; |
| 108 | + console.log(JSON.stringify(this.environments), JSON.stringify(this.locales)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * The function checks if it can write the fix content to a file and if so, it writes the content as |
| 113 | + * JSON to the specified file path. |
| 114 | + */ |
| 115 | + async writeFixContent(filePath: string, schema: Record<string, EntryStruct>) { |
| 116 | + let canWrite = true; |
| 117 | + |
| 118 | + if (this.fix) { |
| 119 | + if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) { |
| 120 | + canWrite = this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION)); |
| 121 | + } |
| 122 | + |
| 123 | + if (canWrite) { |
| 124 | + writeFileSync(filePath, JSON.stringify(schema)); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * This function traverse over the publish detials of the assets and remove the publish details where the locale or environment does not exist |
| 131 | + */ |
| 132 | + async lookForReference(): Promise<void> { |
| 133 | + let basePath = join(this.folderPath); |
| 134 | + let fsUtility = new FsUtility({ basePath, indexFileName: 'assets.json' }); |
| 135 | + let indexer = fsUtility.indexFileContent; |
| 136 | + for (const fileIndex in indexer) { |
| 137 | + const assets = (await fsUtility.readChunkFiles.next()) as Record<string, EntryStruct>; |
| 138 | + this.assets = assets; |
| 139 | + for (const assetUid in assets) { |
| 140 | + this.assets[assetUid].publish_details = this.assets[assetUid].publish_details.filter((pd: any) => { |
| 141 | + if (this.locales.includes(pd.locale) && this.environments.includes(pd.environment)) { |
| 142 | + this.log($t(auditMsg.SCAN_ASSET_SUCCESS_MSG, { uid: assetUid }), { color: 'green' }); |
| 143 | + return true; |
| 144 | + } else { |
| 145 | + this.log( |
| 146 | + $t(auditMsg.SCAN_ASSET_WARN_MSG, { uid: assetUid, locale: pd.locale, environment: pd.environment }), |
| 147 | + { color: 'yellow' }, |
| 148 | + ); |
| 149 | + if (!this.missingEnvLocales[assetUid]) { |
| 150 | + this.missingEnvLocales[assetUid] = [{ uid: assetUid, locale: pd.locale, environment: pd.environment }]; |
| 151 | + } else { |
| 152 | + this.missingEnvLocales[assetUid].push([ |
| 153 | + ...this.missingEnvLocales[assetUid], |
| 154 | + { uid: assetUid, locale: pd.locale, environment: pd.environment }, |
| 155 | + ]); |
| 156 | + } |
| 157 | + this.log($t(auditMsg.SCAN_ASSET_SUCCESS_MSG, { uid: assetUid }), { color: 'green' }); |
| 158 | + return false; |
| 159 | + } |
| 160 | + }); |
| 161 | + if (this.fix) { |
| 162 | + this.log($t(auditFixMsg.ASSET_FIX, { uid: assetUid }), { color: 'green' }); |
| 163 | + await this.writeFixContent(`${basePath}/${indexer[fileIndex]}`, this.assets); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments