|
| 1 | +import omit from 'lodash/omit'; |
| 2 | +import isEqual from 'lodash/isEqual'; |
| 3 | +import { log } from '@contentstack/cli-utilities'; |
| 4 | + |
| 5 | +import type { AssetManagementAPIConfig, ImportContext } from '../types/asset-management-api'; |
| 6 | +import { AssetManagementImportAdapter } from './base'; |
| 7 | +import { PROCESS_NAMES, PROCESS_STATUS } from '../constants/index'; |
| 8 | + |
| 9 | +const STRIP_KEYS = ['created_at', 'created_by', 'updated_at', 'updated_by', 'is_system', 'category', 'preview_image_url', 'category_detail']; |
| 10 | + |
| 11 | +/** |
| 12 | + * Reads shared asset types from `spaces/asset_types/asset-types.json` and POSTs |
| 13 | + * each to the target org-level AM endpoint (`POST /api/asset_types`). |
| 14 | + * |
| 15 | + * Strategy: Fetch → Diff → Create only missing, warn on conflict |
| 16 | + * 1. Fetch asset types that already exist in the target org. |
| 17 | + * 2. Skip entries where is_system=true (platform-owned, cannot be created via API). |
| 18 | + * 3. If uid already exists and definition differs → warn and skip. |
| 19 | + * 4. If uid already exists and definition matches → silently skip. |
| 20 | + * 5. Strip read-only/computed keys from the POST body before creating new asset types. |
| 21 | + */ |
| 22 | +export default class ImportAssetTypes extends AssetManagementImportAdapter { |
| 23 | + constructor(apiConfig: AssetManagementAPIConfig, importContext: ImportContext) { |
| 24 | + super(apiConfig, importContext); |
| 25 | + } |
| 26 | + |
| 27 | + async start(): Promise<void> { |
| 28 | + await this.init(); |
| 29 | + |
| 30 | + const dir = this.getAssetTypesDir(); |
| 31 | + const items = await this.readAllChunkedJson<Record<string, unknown>>(dir, 'asset-types.json'); |
| 32 | + |
| 33 | + if (items.length === 0) { |
| 34 | + log.debug('No shared asset types to import', this.importContext.context); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Fetch existing asset types from the target org keyed by uid for diff comparison. |
| 39 | + // Asset types are org-level; the spaceUid param in getWorkspaceAssetTypes is unused in the path. |
| 40 | + const existingByUid = new Map<string, Record<string, unknown>>(); |
| 41 | + try { |
| 42 | + const existing = await this.getWorkspaceAssetTypes(''); |
| 43 | + for (const at of existing.asset_types ?? []) { |
| 44 | + existingByUid.set(at.uid, at as Record<string, unknown>); |
| 45 | + } |
| 46 | + log.debug(`Target org has ${existingByUid.size} existing asset type(s)`, this.importContext.context); |
| 47 | + } catch (e) { |
| 48 | + log.debug(`Could not fetch existing asset types, will attempt to create all: ${e}`, this.importContext.context); |
| 49 | + } |
| 50 | + |
| 51 | + this.updateStatus(PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].IMPORTING, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); |
| 52 | + |
| 53 | + for (const assetType of items) { |
| 54 | + const uid = assetType.uid as string; |
| 55 | + |
| 56 | + if (assetType.is_system) { |
| 57 | + log.debug(`Skipping system asset type: ${uid}`, this.importContext.context); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + const existing = existingByUid.get(uid); |
| 62 | + if (existing) { |
| 63 | + const exportedClean = omit(assetType, STRIP_KEYS); |
| 64 | + const existingClean = omit(existing, STRIP_KEYS); |
| 65 | + if (!isEqual(exportedClean, existingClean)) { |
| 66 | + log.warn( |
| 67 | + `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.`, |
| 68 | + this.importContext.context, |
| 69 | + ); |
| 70 | + } else { |
| 71 | + log.debug(`Asset type "${uid}" already exists with matching definition, skipping`, this.importContext.context); |
| 72 | + } |
| 73 | + this.tick(true, `asset-type: ${uid} (skipped, already exists)`, null, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + const payload = omit(assetType, STRIP_KEYS); |
| 78 | + try { |
| 79 | + await this.createAssetType(payload as any); |
| 80 | + this.tick(true, `asset-type: ${uid}`, null, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); |
| 81 | + log.debug(`Imported asset type: ${uid}`, this.importContext.context); |
| 82 | + } catch (e) { |
| 83 | + this.tick(false, `asset-type: ${uid}`, (e as Error)?.message ?? PROCESS_STATUS[PROCESS_NAMES.AM_IMPORT_ASSET_TYPES].FAILED, PROCESS_NAMES.AM_IMPORT_ASSET_TYPES); |
| 84 | + log.debug(`Failed to import asset type ${uid}: ${e}`, this.importContext.context); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments