Skip to content

Commit 42828b5

Browse files
author
naman-contentstack
committed
resolved comments
1 parent fac8313 commit 42828b5

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

packages/contentstack-clone/src/commands/cm/stacks/clone.js

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,47 @@ const { readdirSync, readFileSync } = require('fs');
99
let config = {};
1010

1111
class StackCloneCommand extends Command {
12+
/**
13+
* Determine authentication method based on user preference
14+
*/
15+
determineAuthenticationMethod(sourceManagementTokenAlias, destinationManagementTokenAlias) {
16+
// Track authentication method
17+
let authenticationMethod = 'unknown';
18+
19+
// Determine authentication method based on user preference
20+
if (sourceManagementTokenAlias || destinationManagementTokenAlias) {
21+
authenticationMethod = 'Management Token';
22+
} else if (isAuthenticated()) {
23+
// Check if user is authenticated via OAuth
24+
const isOAuthUser = configHandler.get('authorisationType') === 'OAUTH' || false;
25+
if (isOAuthUser) {
26+
authenticationMethod = 'OAuth';
27+
} else {
28+
authenticationMethod = 'Basic Auth';
29+
}
30+
} else {
31+
authenticationMethod = 'Basic Auth';
32+
}
33+
34+
return authenticationMethod;
35+
}
36+
1237
/**
1338
* Create clone context object for logging
1439
*/
15-
createCloneContext() {
40+
createCloneContext(authenticationMethod) {
1641
return {
1742
command: this.context?.info?.command || 'cm:stacks:clone',
1843
module: 'clone',
1944
email: configHandler.get('email') || '',
2045
sessionId: this.context?.sessionId || '',
21-
authenticationMethod: configHandler.get('authenticationMethod') || '',
46+
authenticationMethod: authenticationMethod || 'Basic Auth',
2247
};
2348
}
2449

2550
async run() {
2651
try {
2752
let self = this;
28-
const cloneContext = this.createCloneContext();
2953
const { flags: cloneCommandFlags } = await self.parse(StackCloneCommand);
3054
const {
3155
yes,
@@ -44,8 +68,13 @@ class StackCloneCommand extends Command {
4468
} = cloneCommandFlags;
4569

4670
const handleClone = async () => {
47-
log.debug('Starting clone operation setup', cloneContext);
4871
const listOfTokens = configHandler.get('tokens');
72+
const authenticationMethod = this.determineAuthenticationMethod(
73+
sourceManagementTokenAlias,
74+
destinationManagementTokenAlias,
75+
);
76+
const cloneContext = this.createCloneContext(authenticationMethod);
77+
log.debug('Starting clone operation setup', cloneContext);
4978

5079
if (externalConfigPath) {
5180
log.debug(`Loading external configuration from: ${externalConfigPath}`, cloneContext);
@@ -55,7 +84,8 @@ class StackCloneCommand extends Command {
5584
}
5685
config.forceStopMarketplaceAppsPrompt = yes;
5786
config.skipAudit = cloneCommandFlags['skip-audit'];
58-
log.debug('Clone configuration prepared', cloneContext, {
87+
log.debug('Clone configuration prepared', {
88+
...cloneContext,
5989
cloneType: config.cloneType,
6090
skipAudit: config.skipAudit,
6191
forceStopMarketplaceAppsPrompt: config.forceStopMarketplaceAppsPrompt
@@ -123,7 +153,7 @@ class StackCloneCommand extends Command {
123153
cloneHandler.setClient(managementAPIClient);
124154
log.debug('Starting clone operation', cloneContext);
125155
cloneHandler.execute().catch((error) => {
126-
log.error('Clone operation failed', cloneContext, { error });
156+
log.error('Clone operation failed', { ...cloneContext, error });
127157
});
128158
};
129159

@@ -132,7 +162,7 @@ class StackCloneCommand extends Command {
132162
if (isAuthenticated()) {
133163
handleClone();
134164
} else {
135-
log.warn('Please login to execute this command, csdx auth:login', cloneContext);
165+
log.error('Please login to execute this command, csdx auth:login', cloneContext);
136166
this.exit(1);
137167
}
138168
} else {
@@ -141,13 +171,13 @@ class StackCloneCommand extends Command {
141171
} else if (isAuthenticated()) {
142172
handleClone();
143173
} else {
144-
log.warn('Please login to execute this command, csdx auth:login', cloneContext);
174+
log.error('Please login to execute this command, csdx auth:login', cloneContext);
145175
this.exit(1);
146176
}
147177
} catch (error) {
148178
if (error) {
149179
await this.cleanUp(pathdir, null, cloneContext);
150-
log.error('Stack clone command failed', cloneContext, { error: error.message || error });
180+
log.error('Stack clone command failed', { ...cloneContext, error: error?.message || error });
151181
}
152182
}
153183
}
@@ -156,37 +186,37 @@ class StackCloneCommand extends Command {
156186

157187
async removeContentDirIfNotEmptyBeforeClone(dir, cloneContext) {
158188
try {
159-
log.debug('Checking if content directory is empty', cloneContext, { dir });
189+
log.debug('Checking if content directory is empty', { ...cloneContext, dir });
160190
const dirNotEmpty = readdirSync(dir).length;
161191

162192
if (dirNotEmpty) {
163-
log.debug('Content directory is not empty, cleaning up', cloneContext, { dir });
193+
log.debug('Content directory is not empty, cleaning up', { ...cloneContext, dir });
164194
await this.cleanUp(dir, null, cloneContext);
165195
}
166196
} catch (error) {
167197
const omit = ['ENOENT']; // NOTE add emittable error codes in the array
168198

169199
if (!omit.includes(error.code)) {
170-
log.error('Error checking content directory', cloneContext, { error: error.message, code: error.code });
200+
log.error('Error checking content directory', { ...cloneContext, error: error?.message, code: error.code });
171201
}
172202
}
173203
}
174204

175205
async cleanUp(pathDir, message, cloneContext) {
176206
try {
177-
log.debug('Starting cleanup', cloneContext, { pathDir });
207+
log.debug('Starting cleanup', { ...cloneContext, pathDir });
178208
await rimraf(pathDir);
179209
if (message) {
180210
log.info(message, cloneContext);
181211
}
182-
log.debug('Cleanup completed', cloneContext, { pathDir });
212+
log.debug('Cleanup completed', { ...cloneContext, pathDir });
183213
} catch (err) {
184214
if (err) {
185215
log.debug('Cleaning up', cloneContext);
186216
const skipCodeArr = ['ENOENT', 'EBUSY', 'EPERM', 'EMFILE', 'ENOTEMPTY'];
187217

188218
if (skipCodeArr.includes(err.code)) {
189-
log.debug('Cleanup error code is in skip list, exiting', cloneContext, { code: err.code });
219+
log.debug('Cleanup error code is in skip list, exiting', { ...cloneContext, code: err?.code });
190220
process.exit();
191221
}
192222
}
@@ -205,12 +235,12 @@ class StackCloneCommand extends Command {
205235

206236
if (exitOrError instanceof Promise) {
207237
exitOrError.catch((error) => {
208-
log.error('Error during cleanup', cloneContext, { error: (error && error.message) || '' });
238+
log.error('Error during cleanup', { ...cloneContext, error: (error && error?.message) || '' });
209239
});
210240
} else if (exitOrError.message) {
211-
log.error('Cleanup error', cloneContext, { error: exitOrError.message });
241+
log.error('Cleanup error', { ...cloneContext, error: exitOrError?.message });
212242
} else if (exitOrError.errorMessage) {
213-
log.error('Cleanup error', cloneContext, { error: exitOrError.message });
243+
log.error('Cleanup error', { ...cloneContext, error: exitOrError?.errorMessage });
214244
}
215245

216246
if (exitOrError === true) process.exit();

packages/contentstack-clone/src/lib/util/clone-handler.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,22 @@ class CloneHandler {
155155

156156
// NOTE validate if source branch is exist
157157
if (isSource && config.sourceStackBranch) {
158-
log.debug('Validating source branch exists', config.cloneContext, { branch: config.sourceStackBranch });
158+
log.debug('Validating source branch exists', { ...config.cloneContext, branch: config.sourceStackBranch });
159159
await this.validateIfBranchExist(stackAPIClient, true);
160160
return resolve();
161161
} else if(isSource && config.sourceStackBranchAlias) {
162-
log.debug('Resolving source branch alias', config.cloneContext, { alias: config.sourceStackBranchAlias });
162+
log.debug('Resolving source branch alias', { ...config.cloneContext, alias: config.sourceStackBranchAlias });
163163
await this.resolveBranchAliases(true);
164164
return resolve();
165165
}
166166

167167
// NOTE Validate target branch is exist
168168
if (!isSource && config.targetStackBranch) {
169-
log.debug('Validating target branch exists', config.cloneContext, { branch: config.targetStackBranch });
169+
log.debug('Validating target branch exists', { ...config.cloneContext, branch: config.targetStackBranch });
170170
await this.validateIfBranchExist(stackAPIClient, false);
171171
return resolve();
172172
} else if (!isSource && config.targetStackBranchAlias) {
173-
log.debug('Resolving target branch alias', config.cloneContext, { alias: config.targetStackBranchAlias });
173+
log.debug('Resolving target branch alias', { ...config.cloneContext, alias: config.targetStackBranchAlias });
174174
await this.resolveBranchAliases();
175175
return resolve();
176176
}
@@ -216,7 +216,6 @@ class CloneHandler {
216216
}
217217
} catch (e) {
218218
if (spinner) spinner.fail();
219-
log.error('Error in handleBranchSelection', config.cloneContext, { error: e && e.message, isSource });
220219
return reject(e);
221220
}
222221
});
@@ -246,7 +245,6 @@ class CloneHandler {
246245
process.exit();
247246
}
248247
} catch (e) {
249-
log.error('Branch validation failed', config.cloneContext, { isSource, branch, error: e.message });
250248
completeSpinner(`${isSource ? 'Source' : 'Target'} branch not found.!`, 'fail');
251249
throw e;
252250
}
@@ -271,7 +269,7 @@ class CloneHandler {
271269
return new Promise(async (resolve, reject) => {
272270
let keyPressHandler;
273271
try {
274-
log.debug('Starting clone execution', config.cloneContext, { sourceStack: config.source_stack, targetStack: config.target_stack });
272+
log.debug('Starting clone execution', { ...config.cloneContext, sourceStack: config.source_stack, targetStack: config.target_stack });
275273
if (!config.source_stack) {
276274
const orgMsg = 'Choose an organization where your source stack exists:';
277275
log.debug('Source stack not provided, prompting for organization', config.cloneContext);
@@ -518,7 +516,6 @@ class CloneHandler {
518516
}
519517

520518
spinner.succeed('Fetched Organization');
521-
const orgCount = organizations.items ? organizations.items.length : 1;
522519
log.debug('Fetched organizations', config.cloneContext);
523520
for (const element of organizations.items || [organizations]) {
524521
orgUidList[element.name] = element.uid;
@@ -559,7 +556,6 @@ class CloneHandler {
559556
})
560557
.catch((error) => {
561558
spinner.fail();
562-
log.error('Failed to fetch stacks', { error: error.message || error });
563559
return reject(error);
564560
});
565561
} catch (e) {
@@ -641,15 +637,15 @@ class CloneHandler {
641637

642638
async resolveBranchAliases(isSource = false) {
643639
try {
644-
log.debug('Resolving branch aliases', config.cloneContext, { isSource, alias: isSource ? config.sourceStackBranchAlias : config.targetStackBranchAlias });
640+
log.debug('Resolving branch aliases', { ...config.cloneContext, isSource, alias: isSource ? config.sourceStackBranchAlias : config.targetStackBranchAlias });
645641
if (isSource) {
646642
const sourceStack = client.stack({ api_key: config.source_stack });
647643
config.sourceStackBranch = await getBranchFromAlias(sourceStack, config.sourceStackBranchAlias);
648-
log.debug('Source branch alias resolved', config.cloneContext, { alias: config.sourceStackBranchAlias, branch: config.sourceStackBranch });
644+
log.debug('Source branch alias resolved', { ...config.cloneContext, alias: config.sourceStackBranchAlias, branch: config.sourceStackBranch });
649645
} else {
650646
const targetStack = client.stack({ api_key: config.target_stack });
651647
config.targetStackBranch = await getBranchFromAlias(targetStack, config.targetStackBranchAlias);
652-
log.debug('Target branch alias resolved', config.cloneContext, { alias: config.targetStackBranchAlias, branch: config.targetStackBranch });
648+
log.debug('Target branch alias resolved', { ...config.cloneContext, alias: config.targetStackBranchAlias, branch: config.targetStackBranch });
653649
}
654650
} catch (error) {
655651
throw error;
@@ -704,7 +700,7 @@ class CloneHandler {
704700

705701
async cmdExport() {
706702
return new Promise((resolve, reject) => {
707-
log.debug('Preparing export command', config.cloneContext, { sourceStack: config.source_stack, cloneType: config.cloneType });
703+
log.debug('Preparing export command', { ...config.cloneContext, sourceStack: config.source_stack, cloneType: config.cloneType });
708704
// Creating export specific config by merging external configurations
709705
let exportConfig = Object.assign({}, cloneDeep(config), { ...config?.export });
710706
delete exportConfig.import;
@@ -751,15 +747,14 @@ class CloneHandler {
751747
log.debug('Export command completed successfully', config.cloneContext);
752748
resolve(true);
753749
}).catch((error) => {
754-
log.error('Export command failed', config.cloneContext, { error: error.message || error });
755750
reject(error);
756751
});
757752
});
758753
}
759754

760755
async cmdImport() {
761756
return new Promise(async (resolve, _reject) => {
762-
log.debug('Preparing import command', config.cloneContext, { targetStack: config.target_stack, targetBranch: config.targetStackBranch });
757+
log.debug('Preparing import command', { ...config.cloneContext, targetStack: config.target_stack, targetBranch: config.targetStackBranch });
763758
// Creating export specific config by merging external configurations
764759
let importConfig = Object.assign({}, cloneDeep(config), { ...config?.import });
765760
delete importConfig.import;

0 commit comments

Comments
 (0)