|
| 1 | +import { existsSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import omit from 'lodash/omit'; |
| 4 | +import isEqual from 'lodash/isEqual'; |
| 5 | +import { log } from '@contentstack/cli-utilities'; |
| 6 | + |
| 7 | +import type { AssetManagementAPIConfig, ImportContext } from '../types/asset-management-api'; |
| 8 | +import { AssetManagementImportAdapter } from './base'; |
| 9 | +import { FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS, PROCESS_NAMES, PROCESS_STATUS } from '../constants/index'; |
| 10 | +import { runInBatches } from '../utils/concurrent-batch'; |
| 11 | +import { forEachChunkedJsonStore } from '../utils/chunked-json-reader'; |
| 12 | + |
| 13 | +type AssetTypeToCreate = { uid: string; payload: Record<string, unknown> }; |
| 14 | + |
| 15 | +/** |
| 16 | + * Reads shared asset types from `spaces/asset_types/asset-types.json` and POSTs |
| 17 | + * each to the target org-level AM endpoint (`POST /api/asset_types`). |
| 18 | + * |
| 19 | + * Strategy: Fetch → Diff → Create only missing, warn on conflict |
| 20 | + * 1. Fetch asset types that already exist in the target org. |
| 21 | + * 2. Skip entries where is_system=true (platform-owned, cannot be created via API). |
| 22 | + * 3. If uid already exists and definition differs → warn and skip. |
| 23 | + * 4. If uid already exists and definition matches → silently skip. |
| 24 | + * 5. Strip read-only/computed keys from the POST body before creating new asset types. |
| 25 | + */ |
| 26 | +export default class ImportAssetTypes extends AssetManagementImportAdapter { |
| 27 | + constructor(apiConfig: AssetManagementAPIConfig, importContext: ImportContext) { |
| 28 | + super(apiConfig, importContext); |
| 29 | + } |
| 30 | + |
| 31 | + async start(): Promise<void> { |
| 32 | + await this.init(); |
| 33 | + |
| 34 | + const stripKeys = this.importContext.assetTypesImportInvalidKeys ?? [...FALLBACK_ASSET_TYPES_IMPORT_INVALID_KEYS]; |
| 35 | + const dir = this.getAssetTypesDir(); |
| 36 | + const indexName = this.importContext.assetTypesFileName ?? 'asset-types.json'; |
| 37 | + const indexPath = join(dir, indexName); |
| 38 | + |
| 39 | + if (!existsSync(indexPath)) { |
| 40 | + log.debug('No shared asset types to import (index missing)', this.importContext.context); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 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>>> { |
| 68 | + const existingByUid = new Map<string, Record<string, unknown>>(); |
| 69 | + try { |
| 70 | + const existing = await this.getWorkspaceAssetTypes(''); |
| 71 | + for (const at of existing.asset_types ?? []) { |
| 72 | + existingByUid.set(at.uid, at as Record<string, unknown>); |
| 73 | + } |
| 74 | + log.debug(`Target org has ${existingByUid.size} existing asset type(s)`, this.importContext.context); |
| 75 | + } catch (e) { |
| 76 | + log.debug(`Could not fetch existing asset types, will attempt to create all: ${e}`, this.importContext.context); |
| 77 | + } |
| 78 | + return existingByUid; |
| 79 | + } |
| 80 | + |
| 81 | + private buildAssetTypesToCreate( |
| 82 | + items: Record<string, unknown>[], |
| 83 | + existingByUid: Map<string, Record<string, unknown>>, |
| 84 | + stripKeys: string[], |
| 85 | + ): AssetTypeToCreate[] { |
| 86 | + const toCreate: AssetTypeToCreate[] = []; |
| 87 | + |
| 88 | + for (const assetType of items) { |
| 89 | + const uid = assetType.uid as string; |
| 90 | + |
| 91 | + if (assetType.is_system) { |
| 92 | + log.debug(`Skipping system asset type: ${uid}`, this.importContext.context); |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const existing = existingByUid.get(uid); |
| 97 | + if (existing) { |
| 98 | + const exportedClean = omit(assetType, stripKeys); |
| 99 | + const existingClean = omit(existing, stripKeys); |
| 100 | + if (!isEqual(exportedClean, existingClean)) { |
| 101 | + log.warn( |
| 102 | + `Asset type "${uid}" already exists in the target org with a different definition. Skipping — to apply the exported definition, delete the asset type from the target org first.`, |
| 103 | + this.importContext.context, |
| 104 | + ); |
| 105 | + } else { |
| 106 | + log.debug(`Asset type "${uid}" already exists with matching definition, skipping`, this.importContext.context); |
| 107 | + } |
| 108 | + this.tick(true, `asset-type: ${uid} (skipped, already exists)`, null, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + toCreate.push({ uid, payload: omit(assetType, stripKeys) as Record<string, unknown> }); |
| 113 | + } |
| 114 | + |
| 115 | + return toCreate; |
| 116 | + } |
| 117 | + |
| 118 | + private async importAssetTypesCreates(toCreate: AssetTypeToCreate[]): Promise<void> { |
| 119 | + await runInBatches(toCreate, this.apiConcurrency, async ({ uid, payload }) => { |
| 120 | + try { |
| 121 | + await this.createAssetType(payload as any); |
| 122 | + this.tick(true, `asset-type: ${uid}`, null, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); |
| 123 | + log.debug(`Imported asset type: ${uid}`, this.importContext.context); |
| 124 | + } catch (e) { |
| 125 | + this.tick( |
| 126 | + false, |
| 127 | + `asset-type: ${uid}`, |
| 128 | + (e as Error)?.message ?? PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].FAILED, |
| 129 | + PROCESS_NAMES.AM_IMPORT_ASSET_TYPES, |
| 130 | + ); |
| 131 | + log.debug(`Failed to import asset type ${uid}: ${e}`, this.importContext.context); |
| 132 | + } |
| 133 | + }); |
| 134 | + } |
| 135 | +} |
0 commit comments