-
Notifications
You must be signed in to change notification settings - Fork 16
Feat: Added Check for publish details for Assets and Entries #1778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87bffbe
Feat: Added Check for publish detials for Assets and Entries
cs-raj 76b3e6a
PR fixes
cs-raj 1fb8af2
Merge branch 'development' into feat/DX-2243
cs-raj e0293d3
Fix update
cs-raj e021fe2
Fixed PR comments
cs-raj 37e1188
Merge branch 'development' into feat/DX-2243
cs-raj f720b43
lock-file update
cs-raj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import { join, resolve } from 'path'; | ||
| import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
|
|
||
| import { FsUtility, sanitizePath, ux } from '@contentstack/cli-utilities'; | ||
|
|
||
| import { | ||
| LogFn, | ||
| ConfigType, | ||
| 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'; | ||
|
|
||
| /* The `ContentType` class is responsible for scanning content types, looking for references, and | ||
| generating a report in JSON and CSV formats. */ | ||
| export default class Assets { | ||
| public log: LogFn; | ||
| protected fix: boolean; | ||
| public fileName: string; | ||
| public config: ConfigType; | ||
| 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; | ||
|
|
||
| constructor({ log, fix, config, moduleName }: ModuleConstructorParam & CtConstructorParam) { | ||
| this.log = log; | ||
| this.config = 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. | ||
| * @returns the `missingEnvLocales` object. | ||
| */ | ||
| async run(returnFixSchema = false) { | ||
| if (!existsSync(this.folderPath)) { | ||
| this.log(`Skipping ${this.moduleName} audit`, 'warn'); | ||
| this.log($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' }); | ||
| return returnFixSchema ? [] : {}; | ||
| } | ||
|
|
||
| await this.prerequisiteData(); | ||
| await this.lookForReference(); | ||
|
|
||
| if (returnFixSchema) { | ||
| return this.schema; | ||
| } | ||
|
|
||
| for (let propName in this.missingEnvLocales) { | ||
| if (!this.missingEnvLocales[propName].length) { | ||
| delete this.missingEnvLocales[propName]; | ||
| } | ||
| } | ||
|
|
||
| return this.missingEnvLocales; | ||
| } | ||
|
|
||
| /** | ||
| * @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() { | ||
| 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')))); | ||
| } | ||
| this.locales = this.locales.map((locale: any) => locale.code); | ||
| const environmentPath = resolve( | ||
| this.config.basePath, | ||
| this.config.moduleConfig.environments.dirName, | ||
| this.config.moduleConfig.environments.fileName, | ||
| ); | ||
| this.environments = existsSync(environmentPath) ? keys(JSON.parse(readFileSync(environmentPath, 'utf8'))) : []; | ||
| } | ||
|
|
||
| /** | ||
| * 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>) { | ||
| let canWrite = true; | ||
|
|
||
| if (this.fix) { | ||
| if (!this.config.flags['copy-dir'] && !this.config.flags['external-config']?.skipConfirm) { | ||
| canWrite = this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION)); | ||
| } | ||
|
|
||
| if (canWrite) { | ||
| writeFileSync(filePath, JSON.stringify(schema)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This function traverse over the publish detials of the assets and remove the publish details where the locale or environment does not exist | ||
| */ | ||
| async lookForReference(): Promise<void> { | ||
| let basePath = join(this.folderPath); | ||
| let fsUtility = new FsUtility({ basePath, indexFileName: 'assets.json' }); | ||
| let indexer = fsUtility.indexFileContent; | ||
| for (const fileIndex in indexer) { | ||
| const assets = (await fsUtility.readChunkFiles.next()) as Record<string, EntryStruct>; | ||
| this.assets = assets; | ||
| for (const assetUid in assets) { | ||
|
|
||
| if(this.assets[assetUid]?.publish_details && !Array.isArray(this.assets[assetUid].publish_details)) { | ||
| this.log($t(auditMsg.ASSET_NOT_EXIST, { uid: assetUid }), { color: 'red' }); | ||
| } | ||
|
|
||
| this.assets[assetUid].publish_details = this.assets[assetUid]?.publish_details.filter((pd: any) => { | ||
| if (this.locales?.includes(pd?.locale) && this.environments?.includes(pd?.environment)) { | ||
| this.log($t(auditMsg.SCAN_ASSET_SUCCESS_MSG, { uid: assetUid }), { color: 'green' }); | ||
| return true; | ||
| } else { | ||
| this.log( | ||
| $t(auditMsg.SCAN_ASSET_WARN_MSG, { uid: assetUid, locale: pd.locale, environment: pd.environment }), | ||
| { color: 'yellow' }, | ||
| ); | ||
| if (!this.missingEnvLocales[assetUid]) { | ||
| this.missingEnvLocales[assetUid] = [{ uid: assetUid, locale: pd.locale, environment: pd.environment }]; | ||
| } else { | ||
| this.missingEnvLocales[assetUid].push([ | ||
| ...this.missingEnvLocales[assetUid], | ||
| { uid: assetUid, locale: pd.locale, environment: pd.environment }, | ||
| ]); | ||
| } | ||
| this.log($t(auditMsg.SCAN_ASSET_SUCCESS_MSG, { uid: assetUid }), { color: 'green' }); | ||
| return false; | ||
| } | ||
| }); | ||
| if (this.fix) { | ||
| this.log($t(auditFixMsg.ASSET_FIX, { uid: assetUid }), { color: 'green' }); | ||
| await this.writeFixContent(`${basePath}/${indexer[fileIndex]}`, this.assets); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.