1+ import { existsSync } from 'node:fs' ;
2+ import { join } from 'node:path' ;
13import omit from 'lodash/omit' ;
24import isEqual from 'lodash/isEqual' ;
35import { log } from '@contentstack/cli-utilities' ;
@@ -6,7 +8,9 @@ import type { AssetManagementAPIConfig, ImportContext } from '../types/asset-man
68import { AssetManagementImportAdapter } from './base' ;
79import { FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS , PROCESS_NAMES , PROCESS_STATUS } from '../constants/index' ;
810import { 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