Skip to content

Commit 2ef8e3a

Browse files
author
naman-contentstack
committed
chore: updated implementation for AM import
1 parent 93c7444 commit 2ef8e3a

File tree

22 files changed

+350
-215
lines changed

22 files changed

+350
-215
lines changed

.talismanrc

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
fileignoreconfig:
2-
- filename: pnpm-lock.yaml
3-
checksum: 9c19eb613068c193fac35e72327198fbc86e759968391f07cc876c56b2b1a63d
4-
- filename: packages/contentstack-export/test/unit/export/modules/base-class.test.ts
5-
checksum: bd2b28305fff90ca26bce56b2c5c61751a62225d310a2553874e9ec009ed78e8
6-
- filename: packages/contentstack-export/test/unit/export/modules/assets.test.ts
7-
checksum: 73ff01e2d19c8d1384dca2ee7087f8c19e0b1fac6b29c75a02ca523a36b7cb92
8-
- filename: packages/contentstack-export/src/types/default-config.ts
9-
checksum: a204b00fc47046fd638f952c1326b6d88615dd96e37dc83e4c7e9404dc0435bb
10-
- filename: packages/contentstack-export/src/types/index.ts
11-
checksum: fa36c236abac338b03bf307102a99f25dddac9afe75b6b34fb82e318e7759799
12-
- filename: packages/contentstack-export/src/config/index.ts
13-
checksum: 3998e30abaf9838f86025c3422cab085441bc38e958ed9e63084f928dbb7995c
14-
- filename: packages/contentstack-export/test/unit/export/modules/stack.test.ts
15-
checksum: 79876b8f635037a2d8ba38dac055e7625bf85db6a3cf5729434e6a97e44857d6
16-
- filename: packages/contentstack-export/src/export/modules/stack.ts
17-
checksum: 375c0c5f58d43430b355050d122d3283083ca91891abe8105a4b4fd9433ece97
2+
- filename: packages/contentstack-asset-management/src/export/base.ts
3+
checksum: 01cb2158fea9cbe05449c04efb42d8d416d24868d411bd7300d8fa99c9a4ab01
4+
- filename: packages/contentstack-asset-management/test/unit/export/base.test.ts
5+
checksum: 164fc2e5a4337a2739903499b66eecc66a85bb9b50aa2e71079bdd046a195a94
6+
- filename: packages/contentstack-asset-management/src/import/fields.ts
7+
checksum: 1cd52254ddbfd186d8ade2c73fc799dd1caa0f10bdd3c6b151621c27207ee173
8+
- filename: packages/contentstack-import/src/utils/asset-management-import-options.ts
9+
checksum: 96b61b683109be2cc2dbab5231e1ded19282fbf176cd8492c75cc4861f2efea0
10+
- filename: packages/contentstack-asset-management/src/import/asset-types.ts
11+
checksum: fa2aeea704fd259628f9c86eacd3cf9f6f12543b06b387bd65db7df6b6f5fc49
1812
version: '1.0'

packages/contentstack-asset-management/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
],
3030
"license": "MIT",
3131
"dependencies": {
32-
"@contentstack/cli-utilities": "~2.0.0-beta"
32+
"@contentstack/cli-utilities": "~2.0.0-beta.5"
3333
},
3434
"oclif": {
3535
"commands": "./lib/commands",

packages/contentstack-asset-management/src/constants/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/** Fallback when export/import do not pass `chunkWriteBatchSize`. */
2-
export const FALLBACK_AM_CHUNK_WRITE_BATCH_SIZE = 50;
31
/** Fallback when export/import do not pass `chunkFileSizeMb`. */
42
export const FALLBACK_AM_CHUNK_FILE_SIZE_MB = 1;
53
/** Fallback when import does not pass `apiConcurrency`. */
@@ -27,8 +25,6 @@ export const FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS = [
2725
'category_detail',
2826
] as const;
2927

30-
/** @deprecated Use FALLBACK_AM_CHUNK_WRITE_BATCH_SIZE */
31-
export const BATCH_SIZE = FALLBACK_AM_CHUNK_WRITE_BATCH_SIZE;
3228
/** @deprecated Use FALLBACK_AM_CHUNK_FILE_SIZE_MB */
3329
export const CHUNK_FILE_SIZE_MB = FALLBACK_AM_CHUNK_FILE_SIZE_MB;
3430

packages/contentstack-asset-management/src/export/base.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { FsUtility, log, CLIProgressManager, configHandler } from '@contentstack
55
import type { AssetManagementAPIConfig } from '../types/asset-management-api';
66
import type { ExportContext } from '../types/export-types';
77
import { AssetManagementAdapter } from '../utils/asset-management-api-adapter';
8-
import {
9-
AM_MAIN_PROCESS_NAME,
10-
FALLBACK_AM_CHUNK_FILE_SIZE_MB,
11-
FALLBACK_AM_CHUNK_WRITE_BATCH_SIZE,
12-
} from '../constants/index';
8+
import { AM_MAIN_PROCESS_NAME, FALLBACK_AM_CHUNK_FILE_SIZE_MB } from '../constants/index';
139

1410
export type { ExportContext };
1511

@@ -87,7 +83,6 @@ export class AssetManagementExportAdapter extends AssetManagementAdapter {
8783
return;
8884
}
8985
const chunkMb = this.exportContext.chunkFileSizeMb ?? FALLBACK_AM_CHUNK_FILE_SIZE_MB;
90-
const batchSize = this.exportContext.chunkWriteBatchSize ?? FALLBACK_AM_CHUNK_WRITE_BATCH_SIZE;
9186
const fs = new FsUtility({
9287
basePath: dir,
9388
indexFileName,
@@ -97,10 +92,7 @@ export class AssetManagementExportAdapter extends AssetManagementAdapter {
9792
metaPickKeys,
9893
keepMetadata: true,
9994
});
100-
for (let i = 0; i < items.length; i += batchSize) {
101-
const batch = items.slice(i, i + batchSize);
102-
fs.writeIntoFile(batch as Record<string, string>[], { mapKeyVal: true });
103-
}
95+
fs.writeIntoFile(items as Record<string, string>[], { mapKeyVal: true });
10496
fs.completeFile(true);
10597
}
10698
}

packages/contentstack-asset-management/src/export/spaces.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export class ExportSpaces {
3838
apiKey,
3939
context,
4040
securedAssets,
41-
chunkWriteBatchSize,
4241
chunkFileSizeMb,
4342
} = this.options;
4443

@@ -70,7 +69,6 @@ export class ExportSpaces {
7069
spacesRootPath,
7170
context,
7271
securedAssets,
73-
chunkWriteBatchSize,
7472
chunkFileSizeMb,
7573
};
7674

packages/contentstack-asset-management/src/import/asset-types.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { existsSync } from 'node:fs';
2+
import { join } from 'node:path';
13
import omit from 'lodash/omit';
24
import isEqual from 'lodash/isEqual';
35
import { log } from '@contentstack/cli-utilities';
@@ -6,7 +8,9 @@ import type { AssetManagementAPIConfig, ImportContext } from '../types/asset-man
68
import { AssetManagementImportAdapter } from './base';
79
import { FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS, PROCESS_NAMES, PROCESS_STATUS } from '../constants/index';
810
import { runInBatches } from '../utils/concurrent-batch';
9-
import { readChunkedJsonItems } from '../utils/chunked-json-read';
11+
import { forEachChunkedJsonStore } from '../utils/chunked-json-reader';
12+
13+
type AssetTypeToCreate = { uid: string; payload: Record<string, unknown> };
1014

1115
/**
1216
* Reads shared asset types from `spaces/asset_types/asset-types.json` and POSTs
@@ -30,15 +34,37 @@ export default class ImportAssetTypes extends AssetManagementImportAdapter {
3034
const stripKeys = this.importContext.assetTypesImportInvalidKeys ?? [...FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS];
3135
const dir = this.getAssetTypesDir();
3236
const indexName = this.importContext.assetTypesFileName ?? 'asset-types.json';
33-
const items = await readChunkedJsonItems<Record<string, unknown>>(dir, indexName, this.importContext.context);
37+
const indexPath = join(dir, indexName);
3438

35-
if (items.length === 0) {
36-
log.debug('No shared asset types to import', this.importContext.context);
39+
if (!existsSync(indexPath)) {
40+
log.debug('No shared asset types to import (index missing)', this.importContext.context);
3741
return;
3842
}
3943

40-
// Fetch existing asset types from the target org keyed by uid for diff comparison.
41-
// Asset types are org-level; the spaceUid param in getWorkspaceAssetTypes is unused in the path.
44+
const existingByUid = await this.loadExistingAssetTypesMap();
45+
46+
this.updateStatus(PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].IMPORTING, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES);
47+
48+
await forEachChunkedJsonStore<Record<string, unknown>>(
49+
dir,
50+
indexName,
51+
{
52+
context: this.importContext.context,
53+
chunkReadLogLabel: 'asset-types',
54+
onOpenError: (e) =>
55+
log.debug(`Could not open chunked asset-types index: ${e}`, this.importContext.context),
56+
onEmptyIndexer: () =>
57+
log.debug('No shared asset types to import (empty indexer)', this.importContext.context),
58+
},
59+
async (records) => {
60+
const toCreate = this.buildAssetTypesToCreate(records, existingByUid, stripKeys);
61+
await this.importAssetTypesCreates(toCreate);
62+
},
63+
);
64+
}
65+
66+
/** Org-level asset types keyed by uid for diff; empty map if list API fails. */
67+
private async loadExistingAssetTypesMap(): Promise<Map<string, Record<string, unknown>>> {
4268
const existingByUid = new Map<string, Record<string, unknown>>();
4369
try {
4470
const existing = await this.getWorkspaceAssetTypes('');
@@ -49,11 +75,15 @@ export default class ImportAssetTypes extends AssetManagementImportAdapter {
4975
} catch (e) {
5076
log.debug(`Could not fetch existing asset types, will attempt to create all: ${e}`, this.importContext.context);
5177
}
78+
return existingByUid;
79+
}
5280

53-
this.updateStatus(PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].IMPORTING, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES);
54-
55-
type ToCreate = { uid: string; payload: Record<string, unknown> };
56-
const toCreate: ToCreate[] = [];
81+
private buildAssetTypesToCreate(
82+
items: Record<string, unknown>[],
83+
existingByUid: Map<string, Record<string, unknown>>,
84+
stripKeys: string[],
85+
): AssetTypeToCreate[] {
86+
const toCreate: AssetTypeToCreate[] = [];
5787

5888
for (const assetType of items) {
5989
const uid = assetType.uid as string;
@@ -82,6 +112,10 @@ export default class ImportAssetTypes extends AssetManagementImportAdapter {
82112
toCreate.push({ uid, payload: omit(assetType, stripKeys) as Record<string, unknown> });
83113
}
84114

115+
return toCreate;
116+
}
117+
118+
private async importAssetTypesCreates(toCreate: AssetTypeToCreate[]): Promise<void> {
85119
await runInBatches(toCreate, this.apiConcurrency, async ({ uid, payload }) => {
86120
try {
87121
await this.createAssetType(payload as any);

0 commit comments

Comments
 (0)