|
| 1 | +import { readdirSync, statSync } from 'node:fs'; |
| 2 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 3 | +import { join, resolve } from 'node:path'; |
| 4 | + |
| 5 | +import { formatError, log } from '@contentstack/cli-utilities'; |
| 6 | + |
| 7 | +import { IMPORT_ASSETS_MAPPER_FILES, PROCESS_NAMES, PROCESS_STATUS } from '../constants/index'; |
| 8 | +import type { CSAssetsAPIConfig, ImportContext } from '../types/cs-assets-api'; |
| 9 | +import type { AssetMapperImportSetupResult, RunAssetMapperImportSetupParams } from '../types/import-setup-asset-mapper'; |
| 10 | +import ImportAssets from '../import/assets'; |
| 11 | +import { CSAssetsAdapter } from '../utils/cs-assets-api-adapter'; |
| 12 | +import { AssetManagementImportSetupAdapter } from './base'; |
| 13 | + |
| 14 | +const PROCESS = PROCESS_NAMES.AM_IMPORT_SETUP_ASSET_MAPPERS; |
| 15 | + |
| 16 | +/** |
| 17 | + * Builds identity uid/url and space-uid mapper files from an Asset Management export layout |
| 18 | + * for spaces that already exist in the target org (reuse path). |
| 19 | + */ |
| 20 | +export default class ImportSetupAssetMappers extends AssetManagementImportSetupAdapter { |
| 21 | + constructor(params: RunAssetMapperImportSetupParams) { |
| 22 | + super(params); |
| 23 | + } |
| 24 | + |
| 25 | + private async fetchExistingSpaceUidsInOrg(apiConfig: CSAssetsAPIConfig): Promise<Set<string>> { |
| 26 | + const adapter = new CSAssetsAdapter(apiConfig); |
| 27 | + await adapter.init(); |
| 28 | + const { spaces } = await adapter.listSpaces(); |
| 29 | + const uids = new Set<string>(); |
| 30 | + for (const s of spaces) { |
| 31 | + if (s.uid) { |
| 32 | + uids.add(s.uid); |
| 33 | + } |
| 34 | + } |
| 35 | + return uids; |
| 36 | + } |
| 37 | + |
| 38 | + private listExportedSpaceDirectories(spacesRootPath: string): { spaceDirs: string[]; readFailed: boolean } { |
| 39 | + try { |
| 40 | + const spaceDirs = readdirSync(spacesRootPath).filter((entry) => { |
| 41 | + try { |
| 42 | + return statSync(join(spacesRootPath, entry)).isDirectory() && entry.startsWith('am'); |
| 43 | + } catch { |
| 44 | + return false; |
| 45 | + } |
| 46 | + }); |
| 47 | + return { spaceDirs, readFailed: false }; |
| 48 | + } catch { |
| 49 | + log.info(`Could not read Asset Management spaces directory: ${spacesRootPath}`, this.params.context); |
| 50 | + return { spaceDirs: [], readFailed: true }; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + async start(): Promise<AssetMapperImportSetupResult> { |
| 55 | + const p = this.params; |
| 56 | + const { contentDir, mapperBaseDir, assetManagementUrl, org_uid, source_stack, apiKey, host, context } = p; |
| 57 | + |
| 58 | + const apiConcurrencyResolved = p.apiConcurrency ?? p.fetchConcurrency; |
| 59 | + |
| 60 | + if (!assetManagementUrl) { |
| 61 | + log.info( |
| 62 | + 'AM 2.0 export detected but assetManagementUrl is not configured in the region settings. Skipping AM 2.0 asset mapper setup.', |
| 63 | + context, |
| 64 | + ); |
| 65 | + return { kind: 'skipped', reason: 'missing_asset_management_url' }; |
| 66 | + } |
| 67 | + if (!org_uid) { |
| 68 | + log.error('Cannot run Asset Management import-setup: organization UID is missing.', context); |
| 69 | + return { kind: 'skipped', reason: 'missing_organization_uid' }; |
| 70 | + } |
| 71 | + |
| 72 | + const parentProgressManager = this.resolveParentProgress(); |
| 73 | + |
| 74 | + const spacesDirSegment = p.spacesDirName ?? 'spaces'; |
| 75 | + const spacesRootPath = resolve(contentDir, spacesDirSegment); |
| 76 | + const mapperRoot = p.mapperRootDir ?? 'mapper'; |
| 77 | + const mapperAssetsMod = p.mapperAssetsModuleDir ?? 'assets'; |
| 78 | + const mapperDirPath = join(mapperBaseDir, mapperRoot, mapperAssetsMod); |
| 79 | + const uidFile = p.mapperUidFileName ?? IMPORT_ASSETS_MAPPER_FILES.UID_MAPPING; |
| 80 | + const urlFile = p.mapperUrlFileName ?? IMPORT_ASSETS_MAPPER_FILES.URL_MAPPING; |
| 81 | + const spaceUidFile = p.mapperSpaceUidFileName ?? IMPORT_ASSETS_MAPPER_FILES.SPACE_UID_MAPPING; |
| 82 | + const duplicateAssetMapperPath = join(mapperDirPath, IMPORT_ASSETS_MAPPER_FILES.DUPLICATE_ASSETS); |
| 83 | + |
| 84 | + const apiConfig: CSAssetsAPIConfig = { |
| 85 | + baseURL: assetManagementUrl, |
| 86 | + headers: { organization_uid: org_uid }, |
| 87 | + context, |
| 88 | + }; |
| 89 | + |
| 90 | + const importContext: ImportContext = { |
| 91 | + spacesRootPath, |
| 92 | + sourceApiKey: source_stack, |
| 93 | + apiKey, |
| 94 | + host, |
| 95 | + org_uid, |
| 96 | + context, |
| 97 | + apiConcurrency: apiConcurrencyResolved, |
| 98 | + spacesDirName: p.spacesDirName, |
| 99 | + fieldsDir: p.fieldsDir, |
| 100 | + assetTypesDir: p.assetTypesDir, |
| 101 | + fieldsFileName: p.fieldsFileName, |
| 102 | + assetTypesFileName: p.assetTypesFileName, |
| 103 | + foldersFileName: p.foldersFileName, |
| 104 | + assetsFileName: p.assetsFileName, |
| 105 | + fieldsImportInvalidKeys: p.fieldsImportInvalidKeys, |
| 106 | + assetTypesImportInvalidKeys: p.assetTypesImportInvalidKeys, |
| 107 | + uploadAssetsConcurrency: p.uploadAssetsConcurrency, |
| 108 | + importFoldersConcurrency: p.importFoldersConcurrency, |
| 109 | + mapperRootDir: mapperRoot, |
| 110 | + mapperAssetsModuleDir: mapperAssetsMod, |
| 111 | + mapperUidFileName: uidFile, |
| 112 | + mapperUrlFileName: urlFile, |
| 113 | + mapperSpaceUidFileName: spaceUidFile, |
| 114 | + }; |
| 115 | + |
| 116 | + try { |
| 117 | + if (parentProgressManager) { |
| 118 | + parentProgressManager.addProcess(PROCESS, 1); |
| 119 | + parentProgressManager.startProcess(PROCESS).updateStatus(PROCESS_STATUS[PROCESS].GENERATING, PROCESS); |
| 120 | + } |
| 121 | + |
| 122 | + const existingSpaceUids = await this.fetchExistingSpaceUidsInOrg(apiConfig); |
| 123 | + |
| 124 | + const { spaceDirs, readFailed } = this.listExportedSpaceDirectories(spacesRootPath); |
| 125 | + if (spaceDirs.length === 0 && !readFailed) { |
| 126 | + log.info(`No Asset Management space directories (am*) found under ${spacesDirSegment}/.`, context); |
| 127 | + } |
| 128 | + |
| 129 | + const allUidMap: Record<string, string> = {}; |
| 130 | + const allUrlMap: Record<string, string> = {}; |
| 131 | + const spaceUidMap: Record<string, string> = {}; |
| 132 | + |
| 133 | + const assetsImporter = new ImportAssets(apiConfig, importContext); |
| 134 | + |
| 135 | + for (const spaceUid of spaceDirs) { |
| 136 | + const spaceDir = join(spacesRootPath, spaceUid); |
| 137 | + if (existingSpaceUids.has(spaceUid)) { |
| 138 | + const { uidMap, urlMap } = await assetsImporter.buildIdentityMappersFromExport(spaceDir); |
| 139 | + Object.assign(allUidMap, uidMap); |
| 140 | + Object.assign(allUrlMap, urlMap); |
| 141 | + spaceUidMap[spaceUid] = spaceUid; |
| 142 | + parentProgressManager?.tick(true, `Asset Management space reused: ${spaceUid}`, null, PROCESS); |
| 143 | + log.info( |
| 144 | + `Asset Management space "${spaceUid}" exists in org; identity asset mappers merged from export.`, |
| 145 | + context, |
| 146 | + ); |
| 147 | + } else { |
| 148 | + log.info( |
| 149 | + `Asset Management space "${spaceUid}" is not in the target org yet. Import assets first, then re-run import-setup to refresh mappers after upload.`, |
| 150 | + context, |
| 151 | + ); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + await mkdir(mapperDirPath, { recursive: true }); |
| 156 | + |
| 157 | + await writeFile(join(mapperDirPath, uidFile), JSON.stringify(allUidMap), 'utf8'); |
| 158 | + await writeFile(join(mapperDirPath, urlFile), JSON.stringify(allUrlMap), 'utf8'); |
| 159 | + await writeFile(join(mapperDirPath, spaceUidFile), JSON.stringify(spaceUidMap), 'utf8'); |
| 160 | + |
| 161 | + await writeFile(duplicateAssetMapperPath, JSON.stringify({}), 'utf8'); |
| 162 | + |
| 163 | + parentProgressManager?.completeProcess(PROCESS, true); |
| 164 | + log.success('The required Asset Management setup files for assets have been generated successfully.', context); |
| 165 | + |
| 166 | + return { kind: 'success' }; |
| 167 | + } catch (error) { |
| 168 | + parentProgressManager?.completeProcess(PROCESS, false); |
| 169 | + log.error(`Error occurred while generating Asset Management asset mappers: ${formatError(error)}.`, context); |
| 170 | + return { |
| 171 | + kind: 'error', |
| 172 | + errorMessage: (error as Error)?.message || 'Asset Management asset mapper generation failed', |
| 173 | + }; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments