|
| 1 | +import { resolve as pResolve } from 'node:path'; |
| 2 | +import { |
| 3 | + cliux, |
| 4 | + isAuthenticated, |
| 5 | + log, |
| 6 | + messageHandler, |
| 7 | + handleAndLogError, |
| 8 | + HttpClient, |
| 9 | + authenticationHandler, |
| 10 | +} from '@contentstack/cli-utilities'; |
| 11 | + |
| 12 | +import { fsUtil, getOrgUid } from '../../utils'; |
| 13 | +import { ModuleClassParams, ComposableStudioConfig, ExportConfig, ComposableStudioProject } from '../../types'; |
| 14 | + |
| 15 | +export default class ExportComposableStudio { |
| 16 | + protected composableStudioConfig: ComposableStudioConfig; |
| 17 | + protected composableStudioProject: ComposableStudioProject | null = null; |
| 18 | + protected apiClient: HttpClient; |
| 19 | + public composableStudioPath: string; |
| 20 | + public exportConfig: ExportConfig; |
| 21 | + |
| 22 | + constructor({ exportConfig }: Omit<ModuleClassParams, 'stackAPIClient' | 'moduleName'>) { |
| 23 | + this.exportConfig = exportConfig; |
| 24 | + this.composableStudioConfig = exportConfig.modules['composable-studio']; |
| 25 | + this.exportConfig.context.module = 'composable-studio'; |
| 26 | + |
| 27 | + // Initialize HttpClient with Composable Studio API base URL |
| 28 | + this.apiClient = new HttpClient(); |
| 29 | + this.apiClient.baseUrl(this.composableStudioConfig.apiBaseUrl); |
| 30 | + } |
| 31 | + |
| 32 | + async start(): Promise<void> { |
| 33 | + log.debug('Starting Composable Studio project export process...', this.exportConfig.context); |
| 34 | + |
| 35 | + if (!isAuthenticated()) { |
| 36 | + cliux.print( |
| 37 | + 'WARNING!!! To export Composable Studio projects, you must be logged in. Please check csdx auth:login --help to log in', |
| 38 | + { color: 'yellow' }, |
| 39 | + ); |
| 40 | + return Promise.resolve(); |
| 41 | + } |
| 42 | + |
| 43 | + this.composableStudioPath = pResolve( |
| 44 | + this.exportConfig.data, |
| 45 | + this.exportConfig.branchName || '', |
| 46 | + this.composableStudioConfig.dirName, |
| 47 | + ); |
| 48 | + log.debug(`Composable Studio folder path: ${this.composableStudioPath}`, this.exportConfig.context); |
| 49 | + |
| 50 | + await fsUtil.makeDirectory(this.composableStudioPath); |
| 51 | + log.debug('Created Composable Studio directory', this.exportConfig.context); |
| 52 | + |
| 53 | + this.exportConfig.org_uid = this.exportConfig.org_uid || (await getOrgUid(this.exportConfig)); |
| 54 | + log.debug(`Organization UID: ${this.exportConfig.org_uid}`, this.exportConfig.context); |
| 55 | + |
| 56 | + await this.exportProjects(); |
| 57 | + log.debug('Composable Studio project export process completed', this.exportConfig.context); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Export Composable Studio projects connected to the current stack |
| 62 | + */ |
| 63 | + async exportProjects(): Promise<void> { |
| 64 | + log.debug('Starting Composable Studio project export...', this.exportConfig.context); |
| 65 | + |
| 66 | + try { |
| 67 | + // Get authentication details - following personalization-api-adapter pattern |
| 68 | + log.debug('Initializing Composable Studio API authentication...', this.exportConfig.context); |
| 69 | + await authenticationHandler.getAuthDetails(); |
| 70 | + const token = authenticationHandler.accessToken; |
| 71 | + log.debug( |
| 72 | + `Authentication type: ${authenticationHandler.isOauthEnabled ? 'OAuth' : 'Token'}`, |
| 73 | + this.exportConfig.context, |
| 74 | + ); |
| 75 | + |
| 76 | + // Set authentication headers based on auth type |
| 77 | + if (authenticationHandler.isOauthEnabled) { |
| 78 | + log.debug('Setting OAuth authorization header', this.exportConfig.context); |
| 79 | + this.apiClient.headers({ authorization: token }); |
| 80 | + } else { |
| 81 | + log.debug('Setting authtoken header', this.exportConfig.context); |
| 82 | + this.apiClient.headers({ authtoken: token }); |
| 83 | + } |
| 84 | + |
| 85 | + // Set organization_uid header |
| 86 | + this.apiClient.headers({ |
| 87 | + organization_uid: this.exportConfig.org_uid, |
| 88 | + Accept: 'application/json', |
| 89 | + }); |
| 90 | + |
| 91 | + const apiUrl = '/projects'; |
| 92 | + log.debug( |
| 93 | + `Fetching projects from: ${this.composableStudioConfig.apiBaseUrl}${apiUrl}`, |
| 94 | + this.exportConfig.context, |
| 95 | + ); |
| 96 | + |
| 97 | + // Make API call to fetch projects using HttpClient |
| 98 | + const response = await this.apiClient.get(apiUrl); |
| 99 | + |
| 100 | + if (response.status < 200 || response.status >= 300) { |
| 101 | + throw new Error(`API call failed with status ${response.status}: ${JSON.stringify(response.data)}`); |
| 102 | + } |
| 103 | + |
| 104 | + const data = response.data; |
| 105 | + log.debug(`Fetched ${data.projects?.length || 0} total projects`, this.exportConfig.context); |
| 106 | + |
| 107 | + // Filter projects connected to this stack |
| 108 | + const connectedProject = data.projects?.filter( |
| 109 | + (project: ComposableStudioProject) => project.connectedStackApiKey === this.exportConfig.apiKey, |
| 110 | + ); |
| 111 | + |
| 112 | + if (!connectedProject || connectedProject.length === 0) { |
| 113 | + log.info(messageHandler.parse('COMPOSABLE_STUDIO_NOT_FOUND'), this.exportConfig.context); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // Use the first connected project (stacks should have only one project) |
| 118 | + this.composableStudioProject = connectedProject[0]; |
| 119 | + log.debug(`Found Composable Studio project: ${this.composableStudioProject.name}`, this.exportConfig.context); |
| 120 | + |
| 121 | + // Write the project to file |
| 122 | + const composableStudioFilePath = pResolve(this.composableStudioPath, this.composableStudioConfig.fileName); |
| 123 | + log.debug(`Writing Composable Studio project to: ${composableStudioFilePath}`, this.exportConfig.context); |
| 124 | + |
| 125 | + fsUtil.writeFile(composableStudioFilePath, this.composableStudioProject as unknown as Record<string, unknown>); |
| 126 | + |
| 127 | + log.success( |
| 128 | + messageHandler.parse('COMPOSABLE_STUDIO_EXPORT_COMPLETE', this.composableStudioProject.name), |
| 129 | + this.exportConfig.context, |
| 130 | + ); |
| 131 | + } catch (error: any) { |
| 132 | + log.debug('Error occurred while exporting Composable Studio project', this.exportConfig.context); |
| 133 | + handleAndLogError(error, { |
| 134 | + ...this.exportConfig.context, |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments