Skip to content

Commit

Permalink
Merge branch 'development' into snyk-upgrade-f18c7b83593ec6fb3634c393…
Browse files Browse the repository at this point in the history
…f8645bff
  • Loading branch information
netrajpatel authored Oct 26, 2023
2 parents c560119 + a35829d commit e19c3b0
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 111 deletions.
44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
}
}

let gfSchema = existsSync(gfPath) ? (JSON.parse(readFileSync(gfPath, 'utf-8')) as ContentTypeStruct[]) : [];
let ctSchema = existsSync(ctPath) ? (JSON.parse(readFileSync(ctPath, 'utf-8')) as ContentTypeStruct[]) : [];
const gfSchema = existsSync(gfPath) ? (JSON.parse(readFileSync(gfPath, 'utf8')) as ContentTypeStruct[]) : [];
const ctSchema = existsSync(ctPath) ? (JSON.parse(readFileSync(ctPath, 'utf8')) as ContentTypeStruct[]) : [];

return { ctSchema, gfSchema };
}
Expand Down
75 changes: 43 additions & 32 deletions packages/contentstack-audit/src/modules/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,7 @@ export default class Entries {
}

await this.prepareEntryMetaData();

this.ctSchema = (await new ContentType({
fix: true,
log: () => {},
config: this.config,
moduleName: 'content-types',
ctSchema: this.ctSchema,
gfSchema: this.gfSchema,
}).run(true)) as ContentTypeStruct[];
this.gfSchema = (await new ContentType({
fix: true,
log: () => {},
config: this.config,
moduleName: 'entries',
ctSchema: this.ctSchema,
gfSchema: this.gfSchema,
}).run(true)) as ContentTypeStruct[];
await this.fixPrerequisiteData();

for (const { code } of this.locales) {
for (const ctSchema of this.ctSchema) {
Expand Down Expand Up @@ -125,13 +109,43 @@ export default class Entries {
}
this.log('', 'info'); // Adding empty line

this.removeEmptyVal();

return this.missingRefs;
}

/**
* The function removes any properties from the `missingRefs` object that have an empty array value.
*/
removeEmptyVal() {
for (let propName in this.missingRefs) {
if (!this.missingRefs[propName].length) {
delete this.missingRefs[propName];
}
}
}

return this.missingRefs;
/**
* The function `fixPrerequisiteData` fixes the prerequisite data by updating the `ctSchema` and
* `gfSchema` properties using the `ContentType` class.
*/
async fixPrerequisiteData() {
this.ctSchema = (await new ContentType({
fix: true,
log: () => {},
config: this.config,
moduleName: 'content-types',
ctSchema: this.ctSchema,
gfSchema: this.gfSchema,
}).run(true)) as ContentTypeStruct[];
this.gfSchema = (await new ContentType({
fix: true,
log: () => {},
config: this.config,
moduleName: 'entries',
ctSchema: this.ctSchema,
gfSchema: this.gfSchema,
}).run(true)) as ContentTypeStruct[];
}

/**
Expand Down Expand Up @@ -449,13 +463,14 @@ export default class Entries {
if (data_type === 'json') {
if (field.field_metadata.extension) {
// NOTE Custom field type
return field;
break;
} else if (field.field_metadata.allow_json_rte) {
return this.fixJsonRteMissingReferences(
this.fixJsonRteMissingReferences(
[...tree, { uid: field.uid, name: field.display_name, data_type: field.data_type }],
field as JsonRTEFieldDataType,
entry[uid] as EntryJsonRTEFieldDataType,
);
break;
}
}

Expand All @@ -464,7 +479,7 @@ export default class Entries {
[...tree, { uid: field.uid, name: field.display_name, data_type: field.data_type }],
field as ReferenceFieldDataType,
entry[uid] as EntryReferenceFieldDataType[],
) as EntryReferenceFieldDataType[];
);
break;
case 'blocks':
entry[uid] = this.fixModularBlocksReferences(
Expand Down Expand Up @@ -541,7 +556,7 @@ export default class Entries {

return eBlock;
})
.filter((val) => !isEmpty(val)) as EntryModularBlocksDataType[];
.filter((val) => !isEmpty(val));
});

return entry;
Expand Down Expand Up @@ -604,11 +619,7 @@ export default class Entries {
) {
if (Array.isArray(entry)) {
entry = entry.map((child: any, index) => {
return this.fixJsonRteMissingReferences(
[...tree, { index, type: (child as any)?.type, uid: child?.uid }],
field,
child,
);
return this.fixJsonRteMissingReferences([...tree, { index, type: child?.type, uid: child?.uid }], field, child);
}) as EntryJsonRTEFieldDataType[];
} else {
entry.children = entry.children
Expand Down Expand Up @@ -669,7 +680,7 @@ export default class Entries {
data_type: field.data_type,
display_name: field.display_name,
treeStr: tree
.map(({ name, index }) => (index || index === 0 ? `[${index}].${name}` : name))
.map(({ name, index }) => (index || index === 0 ? `[${+index}].${name}` : name))
.filter((val) => val)
.join(' ➜ '),
missingRefs,
Expand Down Expand Up @@ -697,7 +708,7 @@ export default class Entries {
tree: Record<string, unknown>[],
blocks: ModularBlockType[],
entryBlock: EntryModularBlocksDataType,
index: Number,
index: number,
) {
const validBlockUid = blocks.map((block) => block.uid);
const invalidKeys = Object.keys(entryBlock).filter((key) => !validBlockUid.includes(key));
Expand All @@ -715,7 +726,7 @@ export default class Entries {
fixStatus: this.fix ? 'Fixed' : undefined,
tree: [...tree, { index, uid: key, name: key }],
treeStr: [...tree, { index, uid: key, name: key }]
.map(({ name, index }) => (index || index === 0 ? `[${index}].${name}` : name))
.map(({ name, index }) => (index || index === 0 ? `[${+index}].${name}` : name))
.filter((val) => val)
.join(' ➜ '),
missingRefs: [key],
Expand Down Expand Up @@ -775,8 +786,8 @@ export default class Entries {
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 = values(JSON.parse(readFileSync(masterLocalesPath, 'utf-8')));
this.locales.push(...values(JSON.parse(readFileSync(localesPath, 'utf-8'))));
this.locales = values(JSON.parse(readFileSync(masterLocalesPath, 'utf8')));
this.locales.push(...values(JSON.parse(readFileSync(localesPath, 'utf8'))));

for (const { code } of this.locales) {
for (const { uid } of this.ctSchema) {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-branches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $ npm install -g @contentstack/cli-cm-branches
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-branches/1.0.15 darwin-arm64 node-v20.3.1
@contentstack/cli-cm-branches/1.0.15 darwin-arm64 node-v20.8.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function getAssets(stack, folder, bulkPublish, environments, locale, apiVe
environments: environments,
locale,
stack: stack,
apiVersion
apiVersion,
});
bulkPublishSet = [];
}
Expand All @@ -63,7 +63,7 @@ async function getAssets(stack, folder, bulkPublish, environments, locale, apiVe
environments: environments,
locale,
stack: stack,
apiVersion
apiVersion,
});
bulkPublishSet = [];
}
Expand All @@ -83,6 +83,8 @@ async function getAssets(stack, folder, bulkPublish, environments, locale, apiVe
}
await getAssets(stack, folder, bulkPublish, environments, locale, apiVersion, skip);
return resolve();
} else {
resolve();
}
})
.catch((error) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/contentstack-import/src/commands/cm/stacks/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
FlagInput,
ContentstackClient,
} from '@contentstack/cli-utilities';

import { trace } from '../../../utils/log';
import { ImportConfig } from '../../../types';
import { ModuleImporter } from '../../../import';
import { setupImportConfig, formatError, log } from '../../../utils';
Expand Down Expand Up @@ -120,6 +122,7 @@ export default class ImportCommand extends Command {
// Note setting host to create cma client
importConfig.host = this.cmaHost;
backupDir = importConfig.backupDir;

const managementAPIClient: ContentstackClient = await managementSDKClient(importConfig);
const moduleImporter = new ModuleImporter(managementAPIClient, importConfig);
await moduleImporter.start();
Expand All @@ -130,6 +133,7 @@ export default class ImportCommand extends Command {
'success',
);
} catch (error) {
trace(error, 'error', true);
log({ data: backupDir } as ImportConfig, `Failed to import stack content - ${formatError(error)}`, 'error');
log(
{ data: backupDir } as ImportConfig,
Expand Down
11 changes: 7 additions & 4 deletions packages/contentstack-import/src/import/module-importer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ContentstackClient, HttpClient } from '@contentstack/cli-utilities';
import { ImportConfig, Modules } from '../types';
import { backupHandler, log, validateBranch, masterLocalDetails, sanitizeStack } from '../utils';

import startModuleImport from './modules';
import startJSModuleImport from './modules-js';
import { ImportConfig, Modules } from '../types';
import { backupHandler, log, validateBranch, masterLocalDetails, sanitizeStack, initLogger } from '../utils';

class ModuleImporter {
private managementAPIClient: ContentstackClient;
Expand All @@ -25,7 +26,6 @@ class ModuleImporter {

// Temporarily adding this api call to verify management token has read and write permissions
// TODO: CS-40354 - CLI | import rewrite | Migrate HTTP call to SDK call once fix is ready from SDK side

const httpClient = new HttpClient({
headers: { api_key: this.importConfig.apiKey, authorization: this.importConfig.management_token },
});
Expand Down Expand Up @@ -53,6 +53,9 @@ class ModuleImporter {
this.importConfig.data = backupDir;
}

// NOTE init log
initLogger(this.importConfig);

await sanitizeStack(this.stackAPIClient);

return this.import();
Expand All @@ -72,7 +75,7 @@ class ModuleImporter {
log(this.importConfig, `Starting import of ${moduleName} module`, 'info');
// import the modules by name
// calls the module runner which inturn calls the module itself
// Todo: Implement a mechanism to determine whether module is new or old
// NOTE: Implement a mechanism to determine whether module is new or old
if (this.importConfig.contentVersion === 2) {
return startModuleImport({
stackAPIClient: this.stackAPIClient,
Expand Down
Loading

0 comments on commit e19c3b0

Please sign in to comment.