-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcomposable-studio.ts
More file actions
311 lines (256 loc) · 11.1 KB
/
composable-studio.ts
File metadata and controls
311 lines (256 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { join } from 'node:path';
import {
cliux,
log,
messageHandler,
handleAndLogError,
HttpClient,
authenticationHandler,
} from '@contentstack/cli-utilities';
import isEmpty from 'lodash/isEmpty';
import { fsUtil, fileHelper } from '../../utils';
import { ImportConfig, ModuleClassParams, ComposableStudioConfig, ComposableStudioProject } from '../../types';
export default class ImportComposableStudio {
private importConfig: ImportConfig;
private composableStudioConfig: ComposableStudioConfig;
private composableStudioPath: string;
private composableStudioFilePath: string;
private apiClient: HttpClient;
private envUidMapperPath: string;
private envUidMapper: Record<string, string>;
private projectMapperPath: string;
constructor({ importConfig }: ModuleClassParams) {
this.importConfig = importConfig;
this.importConfig.context.module = 'composable-studio';
this.composableStudioConfig = importConfig.modules['composable-studio'];
// Setup paths
this.composableStudioPath = join(this.importConfig.backupDir, this.composableStudioConfig.dirName);
this.projectMapperPath = join(this.importConfig.backupDir, 'mapper', this.composableStudioConfig.dirName);
this.composableStudioFilePath = join(this.composableStudioPath, this.composableStudioConfig.fileName);
this.envUidMapperPath = join(this.importConfig.backupDir, 'mapper', 'environments', 'uid-mapping.json');
this.envUidMapper = {};
// Initialize HttpClient with Studio API base URL
this.apiClient = new HttpClient();
this.apiClient.baseUrl(`${this.composableStudioConfig.apiBaseUrl}/${this.composableStudioConfig.apiVersion}`);
}
/**
* Entry point for Studio import
*/
async start(): Promise<void> {
if (this.importConfig.management_token) {
log.warn('Skipping Studio project import when using management token', this.importConfig.context);
return;
}
log.debug('Starting Studio project import process...', this.importConfig.context);
try {
// Initialize authentication
const authInitialized = await this.addAuthHeaders();
if (!authInitialized) {
log.warn('Skipping Studio project import when using OAuth authentication', this.importConfig.context);
return;
}
// Load environment UID mapper
await this.loadEnvironmentMapper();
// Read exported project data
const exportedProject = await this.readExportedProject();
if (!exportedProject) {
log.warn(messageHandler.parse('COMPOSABLE_STUDIO_NOT_FOUND'), this.importConfig.context);
return;
}
log.debug(`Exported project found: ${exportedProject.name}`, this.importConfig.context);
// Check if target stack already has a connected project
const existingProject = await this.getExistingProject();
if (existingProject) {
log.warn(messageHandler.parse('COMPOSABLE_STUDIO_SKIP_EXISTING'), this.importConfig.context);
return;
}
// Import the project with name conflict handling
await this.importProject(exportedProject);
log.success(
messageHandler.parse('COMPOSABLE_STUDIO_IMPORT_COMPLETE', exportedProject.name),
this.importConfig.context,
);
} catch (error) {
handleAndLogError(error, { ...this.importConfig.context });
}
}
/**
* Initialize authentication headers for API calls
*/
async addAuthHeaders(): Promise<boolean> {
log.debug('Initializing Studio API authentication...', this.importConfig.context);
// Get authentication details - following personalization-api-adapter pattern
await authenticationHandler.getAuthDetails();
const token = authenticationHandler.accessToken;
log.debug(
`Authentication type: ${authenticationHandler.isOauthEnabled ? 'OAuth' : 'Token'}`,
this.importConfig.context,
);
// Set authentication headers based on auth type
if (authenticationHandler.isOauthEnabled) {
log.debug(
'Skipping setting OAuth authorization header when using OAuth authentication',
this.importConfig.context,
);
return false;
} else {
// TODO: Currenlty assuming if auth type is not OAuth, it is Basic Auth and we are setting authtoken header
log.debug('Setting authtoken header', this.importConfig.context);
this.apiClient.headers({ authtoken: token });
}
// Set organization_uid header
this.apiClient.headers({
organization_uid: this.importConfig.org_uid,
'Content-Type': 'application/json',
Accept: 'application/json',
});
log.debug('Studio API authentication initialized', this.importConfig.context);
return true;
}
/**
* Load environment UID mapper from backup directory
*/
async loadEnvironmentMapper(): Promise<void> {
log.debug('Loading environment UID mapper...', this.importConfig.context);
if (fileHelper.fileExistsSync(this.envUidMapperPath)) {
this.envUidMapper = fileHelper.readFileSync(this.envUidMapperPath) as Record<string, string>;
log.debug(
`Environment mapper loaded with ${Object.keys(this.envUidMapper).length} mappings`,
this.importConfig.context,
);
} else {
log.debug('No environment UID mapper found', this.importConfig.context);
}
}
/**
* Read exported project from file system
*/
async readExportedProject(): Promise<ComposableStudioProject | null> {
log.debug(`Reading exported project from: ${this.composableStudioFilePath}`, this.importConfig.context);
if (!fileHelper.fileExistsSync(this.composableStudioFilePath)) {
log.debug('Studio project file does not exist', this.importConfig.context);
return null;
}
const projectData = fileHelper.readFileSync(this.composableStudioFilePath) as ComposableStudioProject;
if (!projectData || isEmpty(projectData)) {
log.debug('Studio project file is empty', this.importConfig.context);
return null;
}
return projectData;
}
/**
* Check if target stack already has a connected project
*/
async getExistingProject(): Promise<ComposableStudioProject | null> {
log.debug('Checking if target stack already has a connected project...', this.importConfig.context);
try {
const apiUrl = '/projects';
log.debug(
`Fetching projects from: ${this.composableStudioConfig.apiBaseUrl}${apiUrl}`,
this.importConfig.context,
);
const response = await this.apiClient.get(apiUrl);
if (response.status < 200 || response.status >= 300) {
throw new Error(`API call failed with status ${response.status}: ${JSON.stringify(response.data)}`);
}
const projects = response.data?.projects || [];
log.debug(`Found ${projects.length} projects in organization`, this.importConfig.context);
// Filter projects by connected stack API key
const connectedProject = projects.find(
(project: ComposableStudioProject) => project.connectedStackApiKey === this.importConfig.apiKey,
);
if (connectedProject) {
log.debug(`Target stack already has connected project: ${connectedProject.name}`, this.importConfig.context);
return connectedProject;
}
log.debug('Target stack does not have a connected project', this.importConfig.context);
return null;
} catch (error) {
log.debug(`Error checking for existing project: ${error.message}`, this.importConfig.context);
throw error;
}
}
/**
* Import project with name conflict handling
*/
async importProject(exportedProject: ComposableStudioProject): Promise<void> {
log.debug('Starting project import...', this.importConfig.context);
// Map environment UID
const mappedEnvironmentUid = this.mapEnvironmentUid(exportedProject.settings.configuration.environment);
// Prepare project data for import
const projectData = {
name: exportedProject.name,
connectedStackApiKey: this.importConfig.apiKey,
contentTypeUid: exportedProject.contentTypeUid,
description: exportedProject.description || '',
canvasUrl: exportedProject.canvasUrl || '/',
settings: {
configuration: {
environment: mappedEnvironmentUid,
locale: exportedProject?.settings?.configuration?.locale || '',
},
},
};
log.debug(`Project data prepared: ${JSON.stringify(projectData, null, 2)}`, this.importConfig.context);
// Try to create project with name conflict retry loop
let projectCreated = false;
let currentName = projectData.name;
let attemptCount = 0;
while (!projectCreated) {
attemptCount++;
log.debug(`Attempt ${attemptCount} to create project with name: ${currentName}`, this.importConfig.context);
projectData.name = currentName;
const response = await this.apiClient.post('/projects', projectData);
if (response.status >= 200 && response.status < 300) {
projectCreated = true;
log.debug(`Project created successfully with UID: ${response.data?.uid}`, this.importConfig.context);
// Create mapper directory if it doesn't exist
await fsUtil.makeDirectory(this.projectMapperPath);
// write the project to file
const projectFileSuccessPath = join(this.projectMapperPath, this.composableStudioConfig.fileName);
fsUtil.writeFile(projectFileSuccessPath, response.data as unknown as Record<string, unknown>);
log.debug(`Project written to: ${projectFileSuccessPath}`, this.importConfig.context);
} else {
throw new Error(`API call failed with status ${response.status}: ${JSON.stringify(response.data)}`);
}
}
}
/**
* Map environment UID from source to target
*/
mapEnvironmentUid(sourceEnvUid: string): string {
if (!sourceEnvUid) {
log.debug('Source environment UID is empty', this.importConfig.context);
return '';
}
log.debug(`Mapping source environment UID: ${sourceEnvUid}`, this.importConfig.context);
if (isEmpty(this.envUidMapper)) {
log.warn(messageHandler.parse('COMPOSABLE_STUDIO_ENV_MAPPING_FAILED', sourceEnvUid), this.importConfig.context);
return '';
}
const mappedUid = this.envUidMapper[sourceEnvUid];
if (!mappedUid) {
log.warn(messageHandler.parse('COMPOSABLE_STUDIO_ENV_MAPPING_FAILED', sourceEnvUid), this.importConfig.context);
return '';
}
log.debug(`Mapped environment UID: ${sourceEnvUid} → ${mappedUid}`, this.importConfig.context);
return mappedUid;
}
/**
* Prompt user for a new project name when conflict occurs
*/
async promptForNewProjectName(currentName: string): Promise<string> {
const suggestedName = `Copy of ${currentName}`;
log.warn(messageHandler.parse('COMPOSABLE_STUDIO_NAME_CONFLICT', currentName), this.importConfig.context);
log.info(messageHandler.parse('COMPOSABLE_STUDIO_SUGGEST_NAME', suggestedName), this.importConfig.context);
const response: any = await cliux.inquire({
type: 'input',
name: 'projectName',
message: 'Enter new project name:',
default: suggestedName,
});
const newName = response.projectName || suggestedName;
log.debug(`User provided new project name: ${newName}`, this.importConfig.context);
return newName;
}
}