@contentstack/cli-cm-clone 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,7 +15,7 @@ $ npm install -g @contentstack/cli-cm-clone
15
15
  $ csdx COMMAND
16
16
  running command...
17
17
  $ csdx (--version)
18
- @contentstack/cli-cm-clone/1.4.2 linux-x64 node-v16.20.0
18
+ @contentstack/cli-cm-clone/1.4.4 linux-x64 node-v16.20.0
19
19
  $ csdx --help [COMMAND]
20
20
  USAGE
21
21
  $ csdx COMMAND
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.2",
2
+ "version": "1.4.4",
3
3
  "commands": {
4
4
  "cm:stacks:clone": {
5
5
  "id": "cm:stacks:clone",
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-clone",
3
3
  "description": "Contentstack stack clone plugin",
4
- "version": "1.4.2",
4
+ "version": "1.4.4",
5
5
  "author": "Contentstack",
6
6
  "bugs": "https://github.com/rohitmishra209/cli-cm-clone/issues",
7
7
  "dependencies": {
8
- "@contentstack/cli-cm-export": "^1.5.2",
9
- "@contentstack/cli-cm-import": "^1.5.2",
10
- "@contentstack/cli-command": "^1.2.3",
11
- "@contentstack/cli-utilities": "^1.4.0",
8
+ "@contentstack/cli-cm-export": "^1.5.4",
9
+ "@contentstack/cli-cm-import": "^1.5.4",
10
+ "@contentstack/cli-command": "^1.2.4",
11
+ "@contentstack/cli-utilities": "^1.4.1",
12
12
  "async": "^3.2.4",
13
13
  "chalk": "^4.1.0",
14
14
  "child_process": "^1.0.2",
@@ -12,8 +12,8 @@ const HandleStackCommand = function (params, parentContext) {
12
12
  return new CloneCommand(parentContext.handleStackSelection, parentContext.execute, params, parentContext);
13
13
  };
14
14
 
15
- const HandleBranchCommand = function (params, parentContext) {
16
- return new CloneCommand(parentContext.handleBranchSelection, parentContext.execute, params, parentContext);
15
+ const HandleBranchCommand = function (params, parentContext, backStepHandler) {
16
+ return new CloneCommand(parentContext.handleBranchSelection, backStepHandler, params, parentContext);
17
17
  };
18
18
 
19
19
  const HandleDestinationStackCommand = function (params, parentContext) {
@@ -88,30 +88,17 @@ class CloneHandler {
88
88
  }
89
89
 
90
90
  handleStackSelection(options = {}) {
91
- let keyPressHandler;
92
91
  return new Promise(async (resolve, reject) => {
93
92
  try {
94
- const { org = {}, msg = '', isSource = true, stackAbortController } = options || {};
95
-
96
- keyPressHandler = async function (_ch, key) {
97
- if (key.name === 'left' && key.shift) {
98
- stackAbortController.abort();
99
- console.clear();
100
- process.stdin.removeListener('keypress', keyPressHandler);
101
- await cloneCommand.undo();
102
- }
103
- };
104
- process.stdin.addListener('keypress', keyPressHandler);
93
+ const { org = {}, msg = '', isSource = true } = options || {};
105
94
 
106
95
  const stackList = await this.getStack(org, msg, isSource).catch(reject);
107
96
 
108
97
  if (stackList) {
109
- const ui = new inquirer.ui.BottomBar();
110
- ui.updateBottomBar(chalk.cyan('\nPress shift & left arrow together to undo the operation\n'));
98
+ this.displayBackOptionMessage();
111
99
 
112
100
  const selectedStack = await inquirer.prompt(stackList);
113
-
114
- if (stackAbortController.signal.aborted) {
101
+ if (this.executingCommand != 1) {
115
102
  return reject();
116
103
  }
117
104
  if (isSource) {
@@ -127,10 +114,6 @@ class CloneHandler {
127
114
  }
128
115
  } catch (error) {
129
116
  return reject(error);
130
- } finally {
131
- if (keyPressHandler) {
132
- process.stdin.removeListener('keypress', keyPressHandler);
133
- }
134
117
  }
135
118
  });
136
119
  }
@@ -170,7 +153,6 @@ class CloneHandler {
170
153
  if (returnBranch) {
171
154
  resolve(condition ? result : []);
172
155
  } else {
173
- // NOTE list options to use to select branch
174
156
  if (condition) {
175
157
  spinner.succeed('Fetched Branches');
176
158
  const { branch } = await inquirer.prompt({
@@ -179,7 +161,9 @@ class CloneHandler {
179
161
  message: 'Choose a branch',
180
162
  choices: result.map((row) => row.uid),
181
163
  });
182
-
164
+ if (this.executingCommand != 2) {
165
+ return reject();
166
+ }
183
167
  if (isSource) {
184
168
  config.sourceStackBranch = branch;
185
169
  } else {
@@ -224,55 +208,119 @@ class CloneHandler {
224
208
  throw e;
225
209
  }
226
210
  }
227
-
211
+ setAbortController(abortController) {
212
+ this.abortController = abortController;
213
+ }
214
+ abort() {
215
+ this.abortController && this.abortController.abort();
216
+ }
217
+ displayBackOptionMessage() {
218
+ const ui = new inquirer.ui.BottomBar();
219
+ ui.updateBottomBar(chalk.cyan('\nPress shift & left arrow together to undo the operation\n'));
220
+ }
221
+ setBackKeyPressHandler(backKeyPressHandler) {
222
+ this.backKeyPressHandler = backKeyPressHandler;
223
+ }
224
+ removeBackKeyPressHandler() {
225
+ if (this.backKeyPressHandler) {
226
+ process.stdin.removeListener('keypress', this.backKeyPressHandler);
227
+ }
228
+ }
229
+ setExectingCommand(command) {
230
+ // 0 for org, 1 for stack, 1 for branch, 3 stack cancelled, 4 branch cancelled
231
+ this.executingCommand = command;
232
+ }
228
233
  execute() {
229
234
  return new Promise(async (resolve, reject) => {
230
- let stackAbortController;
231
-
235
+ let keyPressHandler;
232
236
  try {
233
237
  if (!config.source_stack) {
234
238
  const orgMsg = 'Choose an organization where your source stack exists:';
235
- const stackMsg = 'Select the source stack';
236
-
237
- stackAbortController = new CustomAbortController();
238
-
239
+ this.setExectingCommand(0);
240
+ this.removeBackKeyPressHandler();
239
241
  const org = await cloneCommand.execute(new HandleOrgCommand({ msg: orgMsg, isSource: true }, this));
242
+ let self = this;
240
243
  if (org) {
241
- const sourceStack = await cloneCommand.execute(
242
- new HandleStackCommand({ org, isSource: true, msg: stackMsg, stackAbortController }, this),
243
- );
244
-
245
- if (config.source_stack) {
246
- await cloneCommand.execute(new HandleBranchCommand({ api_key: config.source_stack }, this));
247
- }
248
-
249
- if (stackAbortController.signal.aborted) {
250
- return reject();
251
- }
252
- stackName.default = config.stackName || `Copy of ${sourceStack.stack || config.source_alias}`;
244
+ keyPressHandler = async function (_ch, key) {
245
+ // executingCommand is a tracking property to determine which method invoked this key press.
246
+ if (key.name === 'left' && key.shift) {
247
+ if (self.executingCommand === 1) {
248
+ self.setExectingCommand(3);
249
+ } else if (self.executingCommand === 2) {
250
+ self.setExectingCommand(4);
251
+ }
252
+ config.source_stack = null;
253
+ config.sourceStackBranch = null;
254
+ if (self.executingCommand != 0) {
255
+ self.abort();
256
+ console.clear();
257
+ await cloneCommand.undo();
258
+ }
259
+ }
260
+ };
261
+ process.stdin.addListener('keypress', keyPressHandler);
262
+ this.setBackKeyPressHandler(keyPressHandler);
263
+
264
+ const params = { org, isSource: true, msg: 'Select the source stack' };
265
+ await this.executeStackPrompt(params);
253
266
  } else {
254
267
  return reject('Org not found.');
255
268
  }
256
- }
257
- const exportRes = await cloneCommand.execute(new HandleExportCommand(null, this));
258
- await cloneCommand.execute(new SetBranchCommand(null, this));
269
+ } else {
270
+ const exportRes = await cloneCommand.execute(new HandleExportCommand(null, this));
271
+ await cloneCommand.execute(new SetBranchCommand(null, this));
259
272
 
260
- if (exportRes) {
261
- this.executeDestination().catch(() => {
262
- reject();
263
- });
273
+ if (exportRes) {
274
+ this.executeDestination().catch(() => {
275
+ reject();
276
+ });
277
+ }
264
278
  }
265
279
  return resolve();
266
280
  } catch (error) {
267
281
  return reject(error);
268
- } finally {
269
- if (stackAbortController) {
270
- stackAbortController.abort();
271
- }
272
282
  }
273
283
  });
274
284
  }
275
285
 
286
+ async executeStackPrompt(params = {}) {
287
+ try {
288
+ this.setExectingCommand(1);
289
+ const sourceStack = await cloneCommand.execute(new HandleStackCommand(params, this));
290
+ if (config.source_stack) {
291
+ await this.executeBranchPrompt(params);
292
+ }
293
+ stackName.default = config.stackName || `Copy of ${sourceStack.stack || config.source_alias}`;
294
+ } catch (error) {
295
+ throw error;
296
+ }
297
+ }
298
+
299
+ async executeBranchPrompt(parentParams) {
300
+ try {
301
+ this.setExectingCommand(2);
302
+ await cloneCommand.execute(new HandleBranchCommand({ api_key: config.source_stack }, this, this.executeStackPrompt.bind(this, parentParams)));
303
+ await this.executeExport();
304
+ } catch (error) {
305
+ throw error;
306
+ }
307
+ }
308
+
309
+ async executeExport() {
310
+ try {
311
+ const exportRes = await cloneCommand.execute(new HandleExportCommand(null, this));
312
+ await cloneCommand.execute(new SetBranchCommand(null, this));
313
+
314
+ if (exportRes) {
315
+ this.executeDestination().catch(() => reject());
316
+ }
317
+ } catch (error) {
318
+ throw error;
319
+ } finally {
320
+ this.removeBackKeyPressHandler();
321
+ }
322
+ }
323
+
276
324
  async executeDestination() {
277
325
  return new Promise(async (resolve, reject) => {
278
326
  let stackAbortController;
@@ -285,6 +333,7 @@ class CloneHandler {
285
333
  canCreateStack = await inquirer.prompt(stackCreationConfirmation);
286
334
  }
287
335
 
336
+ this.setExectingCommand(0);
288
337
  if (!canCreateStack.stackCreate) {
289
338
  if (!config.target_stack) {
290
339
  const orgMsg = 'Choose an organization where the destination stack exists: ';