-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1663 from contentstack/feat/import-setup-asset-ma…
…pper asset mapper, marketplace
- Loading branch information
Showing
5 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/contentstack-import-setup/src/import/modules/marketplace-apps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { log, fsUtil } from '../../utils'; | ||
import { join } from 'path'; | ||
import { ImportConfig, ModuleClassParams } from '../../types'; | ||
import { get, isEmpty } from 'lodash'; | ||
import { | ||
formatError, | ||
isAuthenticated, | ||
marketplaceSDKClient, | ||
ContentstackMarketplaceClient, | ||
NodeCrypto, | ||
createDeveloperHubUrl, | ||
} from '@contentstack/cli-utilities'; | ||
|
||
export default class marketplaceAppImportSetup { | ||
private config: ImportConfig; | ||
private marketplaceAppsFilePath: string; | ||
private marketplaceAppMapper: any; | ||
private stackAPIClient: ModuleClassParams['stackAPIClient']; | ||
private dependencies: ModuleClassParams['dependencies']; | ||
private marketplaceAppsConfig: ImportConfig['modules']['marketplace-apps']; | ||
private mapperDirPath: string; | ||
private marketplaceAppsFolderPath: string; | ||
private marketplaceAppsUidMapperPath: string; | ||
public developerHubBaseUrl: string; | ||
public marketplaceAppPath: string; | ||
public nodeCrypto: NodeCrypto; | ||
public appSdk: ContentstackMarketplaceClient; | ||
|
||
constructor({ config, stackAPIClient }: ModuleClassParams) { | ||
this.config = config; | ||
this.stackAPIClient = stackAPIClient; | ||
this.marketplaceAppsFilePath = join(this.config.contentDir, 'marketplace_apps', 'marketplace_apps.json'); | ||
this.marketplaceAppsConfig = config.modules['marketplace-apps']; | ||
this.marketplaceAppsUidMapperPath = join(this.config.backupDir, 'mapper', 'marketplace_apps'); | ||
this.marketplaceAppMapper = { app_uid: {}, installation_uid: {}, extension_uid: {} }; | ||
} | ||
|
||
/** | ||
* Start the marketplaceApp import setup | ||
* This method reads the marketplaceApps from the content folder and generates a mapper file | ||
* @returns {Promise<void>} | ||
*/ | ||
async start() { | ||
try { | ||
const sourceMarketplaceApps: any = await fsUtil.readFile(this.marketplaceAppsFilePath); | ||
if (!isEmpty(sourceMarketplaceApps)) { | ||
fsUtil.makeDirectory(this.marketplaceAppsUidMapperPath); // Use fsUtil | ||
this.developerHubBaseUrl = this.config.developerHubBaseUrl || (await createDeveloperHubUrl(this.config.host)); | ||
// NOTE init marketplace app sdk | ||
const host = this.developerHubBaseUrl.split('://').pop(); | ||
this.appSdk = await marketplaceSDKClient({ host }); | ||
const targetMarketplaceApps: any = await this.getMarketplaceApps(); | ||
this.createMapper(sourceMarketplaceApps, targetMarketplaceApps); | ||
await fsUtil.writeFile(join(this.marketplaceAppsUidMapperPath, 'uid-mapping.json'), this.marketplaceAppMapper); | ||
|
||
log(this.config, `Generated required setup files for marketplaceApp`, 'success'); | ||
} else { | ||
log(this.config, 'No marketplaceApps found in the content folder!', 'error'); | ||
} | ||
} catch (error) { | ||
log(this.config, `Error generating marketplaceApp mapper: ${formatError(error)}`, 'error'); | ||
} | ||
} | ||
|
||
async getMarketplaceApps() { | ||
// Implement this method to get the marketplaceApp from the stack | ||
return new Promise(async (resolve, reject) => { | ||
const { items: marketplaceApps = [] } = | ||
(await this.appSdk | ||
.marketplace(this.config.org_uid) | ||
.installation() | ||
.fetchAll({ target_uids: this.config.apiKey }) | ||
.catch((error: Error) => { | ||
reject(error); | ||
})) || {}; | ||
resolve(marketplaceApps); | ||
}); | ||
} | ||
|
||
createMapper(sourceMarketplaceApps: any, targetMarketplaceApps: any) { | ||
sourceMarketplaceApps.forEach((sourceApp: any) => { | ||
// Find matching target item based on manifest.name | ||
const targetApp = targetMarketplaceApps.find( | ||
(targetApp: any) => get(targetApp, 'manifest.name') === get(sourceApp, 'manifest.name'), | ||
); | ||
|
||
if (targetApp) { | ||
// Map app_uid from source and target | ||
this.marketplaceAppMapper.app_uid[sourceApp.manifest.uid] = targetApp.manifest.uid; | ||
|
||
// Map installation_uid from source and target | ||
this.marketplaceAppMapper.installation_uid[sourceApp.installation_uid] = targetApp.installation_uid; | ||
|
||
// Map extension_uid by comparing meta.uid in source and target's ui_location.locations | ||
sourceApp.ui_location.locations.forEach((sourceAppLocation: any) => { | ||
const targetAppLocation = targetApp.ui_location.locations.find( | ||
(targetAppLocation: any) => targetAppLocation.type === sourceAppLocation.type, | ||
); | ||
|
||
if (targetAppLocation) { | ||
sourceAppLocation.meta.forEach((sourceAppMeta: any) => { | ||
const targetAppMeta = targetAppLocation.meta.find( | ||
(targetAppMeta: any) => targetAppMeta.uid === sourceAppMeta.uid, | ||
); | ||
|
||
if (targetAppMeta && sourceAppMeta.extension_uid && targetAppMeta.extension_uid) { | ||
this.marketplaceAppMapper.extension_uid[sourceAppMeta.extension_uid] = targetAppMeta.extension_uid; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters