@contentstack/cli-cm-clone 2.0.0-beta → 2.0.0-beta.10
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/LICENSE +1 -1
- package/README.md +2 -2
- package/bin/run.cmd +3 -0
- package/bin/run.js +7 -0
- package/lib/commands/cm/stacks/clone.d.ts +21 -0
- package/lib/commands/cm/stacks/clone.js +315 -0
- package/lib/core/helpers/command-helpers.d.ts +31 -0
- package/lib/core/helpers/command-helpers.js +79 -0
- package/lib/core/util/abort-controller.d.ts +30 -0
- package/lib/core/util/abort-controller.js +58 -0
- package/lib/core/util/clone-handler.d.ts +55 -0
- package/lib/core/util/clone-handler.js +773 -0
- package/lib/core/util/dummyConfig.json +1 -0
- package/lib/types/clone-config.d.ts +36 -0
- package/lib/types/clone-config.js +2 -0
- package/lib/types/clone-context.d.ts +10 -0
- package/lib/types/clone-context.js +2 -0
- package/lib/types/command-types.d.ts +39 -0
- package/lib/types/command-types.js +2 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.js +6 -0
- package/lib/utils/constants.d.ts +25 -0
- package/lib/utils/constants.js +40 -0
- package/oclif.manifest.json +169 -0
- package/package.json +33 -24
- package/src/commands/cm/stacks/clone.js +0 -291
- package/src/lib/helpers/command-helpers.js +0 -67
- package/src/lib/util/abort-controller.js +0 -49
- package/src/lib/util/clone-handler.js +0 -707
- package/src/lib/util/dummyConfig.json +0 -1
- package/src/lib/util/log.js +0 -105
|
@@ -1,707 +0,0 @@
|
|
|
1
|
-
const ora = require('ora');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const inquirer = require('inquirer');
|
|
4
|
-
const chalk = require('chalk');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
let { default: exportCmd } = require('@contentstack/cli-cm-export');
|
|
7
|
-
let { default: importCmd } = require('@contentstack/cli-cm-import');
|
|
8
|
-
const { CustomAbortController } = require('./abort-controller');
|
|
9
|
-
const prompt = require('prompt');
|
|
10
|
-
const colors = require('@colors/colors/safe');
|
|
11
|
-
const cloneDeep = require('lodash/cloneDeep');
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
HandleOrgCommand,
|
|
15
|
-
HandleStackCommand,
|
|
16
|
-
HandleDestinationStackCommand,
|
|
17
|
-
HandleExportCommand,
|
|
18
|
-
SetBranchCommand,
|
|
19
|
-
CreateNewStackCommand,
|
|
20
|
-
CloneTypeSelectionCommand,
|
|
21
|
-
Clone,
|
|
22
|
-
HandleBranchCommand,
|
|
23
|
-
} = require('../helpers/command-helpers');
|
|
24
|
-
const { configHandler, getBranchFromAlias } = require('@contentstack/cli-utilities');
|
|
25
|
-
|
|
26
|
-
let client = {};
|
|
27
|
-
let config;
|
|
28
|
-
let cloneCommand;
|
|
29
|
-
|
|
30
|
-
let stackCreationConfirmation = [
|
|
31
|
-
{
|
|
32
|
-
type: 'confirm',
|
|
33
|
-
name: 'stackCreate',
|
|
34
|
-
message: 'Want to clone content into a new stack ?',
|
|
35
|
-
initial: true,
|
|
36
|
-
},
|
|
37
|
-
];
|
|
38
|
-
|
|
39
|
-
let stackName = {
|
|
40
|
-
type: 'input',
|
|
41
|
-
name: 'stack',
|
|
42
|
-
default: 'ABC',
|
|
43
|
-
message: 'Enter name for the new stack to store the cloned content ?',
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
let orgUidList = {};
|
|
47
|
-
let stackUidList = {};
|
|
48
|
-
let masterLocaleList = {};
|
|
49
|
-
|
|
50
|
-
let structureList = [
|
|
51
|
-
'locales',
|
|
52
|
-
'environments',
|
|
53
|
-
'extensions',
|
|
54
|
-
'marketplace-apps',
|
|
55
|
-
'webhooks',
|
|
56
|
-
'global-fields',
|
|
57
|
-
'content-types',
|
|
58
|
-
'workflows',
|
|
59
|
-
'labels',
|
|
60
|
-
];
|
|
61
|
-
let master_locale;
|
|
62
|
-
|
|
63
|
-
// Overrides prompt's stop method
|
|
64
|
-
prompt.stop = function () {
|
|
65
|
-
if (prompt.stopped) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
prompt.emit('stop');
|
|
69
|
-
prompt.stopped = true;
|
|
70
|
-
return prompt;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
class CloneHandler {
|
|
74
|
-
constructor(opt) {
|
|
75
|
-
config = opt;
|
|
76
|
-
cloneCommand = new Clone();
|
|
77
|
-
this.pathDir = opt.pathDir;
|
|
78
|
-
process.stdin.setMaxListeners(50);
|
|
79
|
-
}
|
|
80
|
-
setClient(managementSDKClient) {
|
|
81
|
-
client = managementSDKClient;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
handleOrgSelection(options = {}) {
|
|
85
|
-
return new Promise(async (resolve, reject) => {
|
|
86
|
-
const { msg = '', isSource = true } = options || {};
|
|
87
|
-
const orgList = await this.getOrganizationChoices(msg).catch(reject);
|
|
88
|
-
|
|
89
|
-
if (orgList) {
|
|
90
|
-
const orgSelected = await inquirer.prompt(orgList);
|
|
91
|
-
|
|
92
|
-
if (isSource) {
|
|
93
|
-
config.sourceOrg = orgUidList[orgSelected.Organization];
|
|
94
|
-
} else {
|
|
95
|
-
config.targetOrg = orgUidList[orgSelected.Organization];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
resolve(orgSelected);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
handleStackSelection(options = {}) {
|
|
104
|
-
return new Promise(async (resolve, reject) => {
|
|
105
|
-
try {
|
|
106
|
-
const { org = {}, msg = '', isSource = true } = options || {};
|
|
107
|
-
|
|
108
|
-
const stackList = await this.getStack(org, msg, isSource).catch(reject);
|
|
109
|
-
|
|
110
|
-
if (stackList) {
|
|
111
|
-
this.displayBackOptionMessage();
|
|
112
|
-
|
|
113
|
-
const selectedStack = await inquirer.prompt(stackList);
|
|
114
|
-
if (this.executingCommand != 1) {
|
|
115
|
-
return reject();
|
|
116
|
-
}
|
|
117
|
-
if (isSource) {
|
|
118
|
-
config.sourceStackName = selectedStack.stack;
|
|
119
|
-
master_locale = masterLocaleList[selectedStack.stack];
|
|
120
|
-
config.source_stack = stackUidList[selectedStack.stack];
|
|
121
|
-
} else {
|
|
122
|
-
config.target_stack = stackUidList[selectedStack.stack];
|
|
123
|
-
config.destinationStackName = selectedStack.stack;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
resolve(selectedStack);
|
|
127
|
-
}
|
|
128
|
-
} catch (error) {
|
|
129
|
-
return reject(error);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
handleBranchSelection = async (options) => {
|
|
135
|
-
const { api_key, isSource = true, returnBranch = false } = options;
|
|
136
|
-
return new Promise(async (resolve, reject) => {
|
|
137
|
-
let spinner;
|
|
138
|
-
try {
|
|
139
|
-
const stackAPIClient = client.stack({
|
|
140
|
-
api_key: isSource ? config.source_stack : config.target_stack,
|
|
141
|
-
management_token: config.management_token,
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
// NOTE validate if source branch is exist
|
|
145
|
-
if (isSource && config.sourceStackBranch) {
|
|
146
|
-
await this.validateIfBranchExist(stackAPIClient, true);
|
|
147
|
-
return resolve();
|
|
148
|
-
} else if(isSource && config.sourceStackBranchAlias) {
|
|
149
|
-
await this.resolveBranchAliases(true);
|
|
150
|
-
return resolve();
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// NOTE Validate target branch is exist
|
|
154
|
-
if (!isSource && config.targetStackBranch) {
|
|
155
|
-
await this.validateIfBranchExist(stackAPIClient, false);
|
|
156
|
-
return resolve();
|
|
157
|
-
} else if (!isSource && config.targetStackBranchAlias) {
|
|
158
|
-
await this.resolveBranchAliases();
|
|
159
|
-
return resolve();
|
|
160
|
-
}
|
|
161
|
-
spinner = ora('Fetching Branches').start();
|
|
162
|
-
const result = await stackAPIClient
|
|
163
|
-
.branch()
|
|
164
|
-
.query()
|
|
165
|
-
.find()
|
|
166
|
-
.then(({ items }) => items)
|
|
167
|
-
.catch((_err) => {});
|
|
168
|
-
|
|
169
|
-
const condition = result && Array.isArray(result) && result.length > 0;
|
|
170
|
-
|
|
171
|
-
// NOTE if want to get only list of branches (Pass param -> returnBranch = true )
|
|
172
|
-
if (returnBranch) {
|
|
173
|
-
resolve(condition ? result : []);
|
|
174
|
-
} else {
|
|
175
|
-
if (condition) {
|
|
176
|
-
spinner.succeed('Fetched Branches');
|
|
177
|
-
const { branch } = await inquirer.prompt({
|
|
178
|
-
type: 'list',
|
|
179
|
-
name: 'branch',
|
|
180
|
-
message: 'Choose a branch',
|
|
181
|
-
choices: result.map((row) => row.uid),
|
|
182
|
-
});
|
|
183
|
-
if (this.executingCommand != 2) {
|
|
184
|
-
return reject();
|
|
185
|
-
}
|
|
186
|
-
if (isSource) {
|
|
187
|
-
config.sourceStackBranch = branch;
|
|
188
|
-
} else {
|
|
189
|
-
config.targetStackBranch = branch;
|
|
190
|
-
}
|
|
191
|
-
} else {
|
|
192
|
-
spinner.succeed('No branches found.!');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
resolve();
|
|
196
|
-
}
|
|
197
|
-
} catch (e) {
|
|
198
|
-
if (spinner) spinner.fail();
|
|
199
|
-
console.error(e && e.message);
|
|
200
|
-
return reject(e);
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
async validateIfBranchExist(stackAPIClient, isSource) {
|
|
206
|
-
let spinner;
|
|
207
|
-
const completeSpinner = (msg, method = 'succeed') => {
|
|
208
|
-
spinner[method](msg);
|
|
209
|
-
spinner.stop();
|
|
210
|
-
};
|
|
211
|
-
try {
|
|
212
|
-
const branch = isSource ? config.sourceStackBranch : config.targetStackBranch;
|
|
213
|
-
spinner = ora(`Validation if ${isSource ? 'source' : 'target'} branch exist.!`).start();
|
|
214
|
-
const isBranchExist = await stackAPIClient
|
|
215
|
-
.branch(branch)
|
|
216
|
-
.fetch()
|
|
217
|
-
.then((data) => data);
|
|
218
|
-
|
|
219
|
-
if (isBranchExist && typeof isBranchExist === 'object') {
|
|
220
|
-
completeSpinner(`${isSource ? 'Source' : 'Target'} branch verified.!`);
|
|
221
|
-
} else {
|
|
222
|
-
completeSpinner(`${isSource ? 'Source' : 'Target'} branch not found.!`, 'fail');
|
|
223
|
-
process.exit();
|
|
224
|
-
}
|
|
225
|
-
} catch (e) {
|
|
226
|
-
completeSpinner(`${isSource ? 'Source' : 'Target'} branch not found.!`, 'fail');
|
|
227
|
-
throw e;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
displayBackOptionMessage() {
|
|
231
|
-
const ui = new inquirer.ui.BottomBar();
|
|
232
|
-
ui.updateBottomBar(chalk.cyan('\nPress shift & left arrow together to undo the operation\n'));
|
|
233
|
-
}
|
|
234
|
-
setBackKeyPressHandler(backKeyPressHandler) {
|
|
235
|
-
this.backKeyPressHandler = backKeyPressHandler;
|
|
236
|
-
}
|
|
237
|
-
removeBackKeyPressHandler() {
|
|
238
|
-
if (this.backKeyPressHandler) {
|
|
239
|
-
process.stdin.removeListener('keypress', this.backKeyPressHandler);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
setExectingCommand(command) {
|
|
243
|
-
// 0 for org, 1 for stack, 1 for branch, 3 stack cancelled, 4 branch cancelled
|
|
244
|
-
this.executingCommand = command;
|
|
245
|
-
}
|
|
246
|
-
execute() {
|
|
247
|
-
return new Promise(async (resolve, reject) => {
|
|
248
|
-
let keyPressHandler;
|
|
249
|
-
try {
|
|
250
|
-
if (!config.source_stack) {
|
|
251
|
-
const orgMsg = 'Choose an organization where your source stack exists:';
|
|
252
|
-
this.setExectingCommand(0);
|
|
253
|
-
this.removeBackKeyPressHandler();
|
|
254
|
-
const org = await cloneCommand.execute(new HandleOrgCommand({ msg: orgMsg, isSource: true }, this));
|
|
255
|
-
let self = this;
|
|
256
|
-
if (org) {
|
|
257
|
-
keyPressHandler = async function (_ch, key) {
|
|
258
|
-
// executingCommand is a tracking property to determine which method invoked this key press.
|
|
259
|
-
if (key.name === 'left' && key.shift) {
|
|
260
|
-
if (self.executingCommand === 1) {
|
|
261
|
-
self.setExectingCommand(3);
|
|
262
|
-
} else if (self.executingCommand === 2) {
|
|
263
|
-
self.setExectingCommand(4);
|
|
264
|
-
}
|
|
265
|
-
config.source_stack = null;
|
|
266
|
-
config.sourceStackBranch = null;
|
|
267
|
-
if (self.executingCommand != 0) {
|
|
268
|
-
console.clear();
|
|
269
|
-
await cloneCommand.undo();
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
process.stdin.addListener('keypress', keyPressHandler);
|
|
274
|
-
this.setBackKeyPressHandler(keyPressHandler);
|
|
275
|
-
|
|
276
|
-
await this.executeStackPrompt({ org, isSource: true, msg: 'Select the source stack' });
|
|
277
|
-
} else {
|
|
278
|
-
return reject('Org not found.');
|
|
279
|
-
}
|
|
280
|
-
} else {
|
|
281
|
-
this.setExectingCommand(2);
|
|
282
|
-
await this.handleBranchSelection({ api_key: config.sourceStack });
|
|
283
|
-
const exportRes = await cloneCommand.execute(new HandleExportCommand(null, this));
|
|
284
|
-
await cloneCommand.execute(new SetBranchCommand(null, this));
|
|
285
|
-
|
|
286
|
-
if (exportRes) {
|
|
287
|
-
this.executeDestination().catch((error) => {
|
|
288
|
-
return reject(error);
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
return resolve();
|
|
293
|
-
} catch (error) {
|
|
294
|
-
return reject(error);
|
|
295
|
-
}
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async executeStackPrompt(params = {}) {
|
|
300
|
-
try {
|
|
301
|
-
this.setExectingCommand(1);
|
|
302
|
-
const sourceStack = await cloneCommand.execute(new HandleStackCommand(params, this));
|
|
303
|
-
if (config.source_stack) {
|
|
304
|
-
await this.executeBranchPrompt(params);
|
|
305
|
-
}
|
|
306
|
-
stackName.default = config.stackName || `Copy of ${sourceStack.stack || config.source_alias}`;
|
|
307
|
-
} catch (error) {
|
|
308
|
-
throw error;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
async executeBranchPrompt(parentParams) {
|
|
313
|
-
try {
|
|
314
|
-
this.setExectingCommand(2);
|
|
315
|
-
await cloneCommand.execute(
|
|
316
|
-
new HandleBranchCommand(
|
|
317
|
-
{ api_key: config.source_stack },
|
|
318
|
-
this,
|
|
319
|
-
this.executeStackPrompt.bind(this, parentParams),
|
|
320
|
-
),
|
|
321
|
-
);
|
|
322
|
-
await this.executeExport();
|
|
323
|
-
} catch (error) {
|
|
324
|
-
throw error;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
async executeExport() {
|
|
329
|
-
try {
|
|
330
|
-
const exportRes = await cloneCommand.execute(new HandleExportCommand(null, this));
|
|
331
|
-
await cloneCommand.execute(new SetBranchCommand(null, this));
|
|
332
|
-
|
|
333
|
-
if (exportRes) {
|
|
334
|
-
this.executeDestination().catch(() => {
|
|
335
|
-
throw '';
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
} catch (error) {
|
|
339
|
-
throw error;
|
|
340
|
-
} finally {
|
|
341
|
-
this.removeBackKeyPressHandler();
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
async executeDestination() {
|
|
346
|
-
return new Promise(async (resolve, reject) => {
|
|
347
|
-
let keyPressHandler;
|
|
348
|
-
try {
|
|
349
|
-
let canCreateStack = false;
|
|
350
|
-
if (!config.target_stack) {
|
|
351
|
-
canCreateStack = await inquirer.prompt(stackCreationConfirmation);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
this.setExectingCommand(0);
|
|
355
|
-
this.removeBackKeyPressHandler();
|
|
356
|
-
|
|
357
|
-
const orgMsgExistingStack = 'Choose an organization where the destination stack exists: ';
|
|
358
|
-
const orgMsgNewStack = 'Choose an organization where you want to create a stack: ';
|
|
359
|
-
|
|
360
|
-
let org;
|
|
361
|
-
if (!config.target_stack) {
|
|
362
|
-
org = await cloneCommand.execute(
|
|
363
|
-
new HandleOrgCommand(
|
|
364
|
-
{
|
|
365
|
-
msg: !canCreateStack.stackCreate ? orgMsgExistingStack : orgMsgNewStack,
|
|
366
|
-
},
|
|
367
|
-
this,
|
|
368
|
-
),
|
|
369
|
-
);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
const params = { org, canCreateStack };
|
|
373
|
-
if (!config.target_stack) {
|
|
374
|
-
let self = this;
|
|
375
|
-
keyPressHandler = async function (_ch, key) {
|
|
376
|
-
if (key.name === 'left' && key.shift) {
|
|
377
|
-
if (self.executingCommand === 1) {
|
|
378
|
-
self.setExectingCommand(3);
|
|
379
|
-
} else if (self.executingCommand === 2) {
|
|
380
|
-
self.setExectingCommand(4);
|
|
381
|
-
}
|
|
382
|
-
if (self.createNewStackPrompt) {
|
|
383
|
-
self.createNewStackPrompt.stop();
|
|
384
|
-
}
|
|
385
|
-
config.target_stack = null;
|
|
386
|
-
config.targetStackBranch = null;
|
|
387
|
-
if (self.executingCommand != 0) {
|
|
388
|
-
console.clear();
|
|
389
|
-
await cloneCommand.undo();
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
process.stdin.addListener('keypress', keyPressHandler);
|
|
394
|
-
this.setBackKeyPressHandler(keyPressHandler);
|
|
395
|
-
await this.executeStackDestinationPrompt(params);
|
|
396
|
-
} else {
|
|
397
|
-
await this.executeBranchDestinationPrompt(params);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
return resolve();
|
|
401
|
-
} catch (error) {
|
|
402
|
-
reject(error);
|
|
403
|
-
}
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
async executeStackDestinationPrompt(params) {
|
|
408
|
-
try {
|
|
409
|
-
this.setExectingCommand(1);
|
|
410
|
-
const { org, canCreateStack } = params;
|
|
411
|
-
if (!canCreateStack.stackCreate) {
|
|
412
|
-
const stackMsg = 'Choose the destination stack:';
|
|
413
|
-
await cloneCommand.execute(new HandleDestinationStackCommand({ org, msg: stackMsg, isSource: false }, this));
|
|
414
|
-
this.executeBranchDestinationPrompt(params);
|
|
415
|
-
} else {
|
|
416
|
-
const orgUid = orgUidList[org.Organization];
|
|
417
|
-
await cloneCommand.execute(new CreateNewStackCommand({ orgUid }, this));
|
|
418
|
-
this.removeBackKeyPressHandler();
|
|
419
|
-
await cloneCommand.execute(new CloneTypeSelectionCommand(null, this));
|
|
420
|
-
}
|
|
421
|
-
} catch (error) {
|
|
422
|
-
throw error;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
async executeBranchDestinationPrompt(parentParams) {
|
|
427
|
-
try {
|
|
428
|
-
this.setExectingCommand(2);
|
|
429
|
-
await cloneCommand.execute(
|
|
430
|
-
new HandleBranchCommand(
|
|
431
|
-
{ isSource: false, api_key: config.target_stack },
|
|
432
|
-
this,
|
|
433
|
-
this.executeStackDestinationPrompt.bind(this, parentParams),
|
|
434
|
-
),
|
|
435
|
-
);
|
|
436
|
-
this.removeBackKeyPressHandler();
|
|
437
|
-
await cloneCommand.execute(new CloneTypeSelectionCommand(null, this));
|
|
438
|
-
} catch (error) {
|
|
439
|
-
throw error;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
setCreateNewStackPrompt(createNewStackPrompt) {
|
|
444
|
-
this.createNewStackPrompt = createNewStackPrompt;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
async setBranch() {
|
|
448
|
-
if (!config.sourceStackBranch) {
|
|
449
|
-
try {
|
|
450
|
-
const branches = await client
|
|
451
|
-
.stack({ api_key: config.source_stack })
|
|
452
|
-
.branch()
|
|
453
|
-
.query()
|
|
454
|
-
.find()
|
|
455
|
-
.catch((_err) => {});
|
|
456
|
-
|
|
457
|
-
if (branches && branches.items && branches.items.length) {
|
|
458
|
-
config.sourceStackBranch = 'main';
|
|
459
|
-
}
|
|
460
|
-
} catch (_error) {}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
async getOrganizationChoices(orgMessage) {
|
|
465
|
-
let orgChoice = {
|
|
466
|
-
type: 'list',
|
|
467
|
-
name: 'Organization',
|
|
468
|
-
message: orgMessage !== undefined ? orgMessage : 'Choose an organization',
|
|
469
|
-
choices: [],
|
|
470
|
-
};
|
|
471
|
-
return new Promise(async (resolve, reject) => {
|
|
472
|
-
const spinner = ora('Fetching Organization').start();
|
|
473
|
-
try {
|
|
474
|
-
let organizations;
|
|
475
|
-
const configOrgUid = configHandler.get('oauthOrgUid');
|
|
476
|
-
|
|
477
|
-
if (configOrgUid) {
|
|
478
|
-
organizations = await client.organization(configOrgUid).fetch();
|
|
479
|
-
} else {
|
|
480
|
-
organizations = await client.organization().fetchAll({ limit: 100 });
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
spinner.succeed('Fetched Organization');
|
|
484
|
-
for (const element of organizations.items || [organizations]) {
|
|
485
|
-
orgUidList[element.name] = element.uid;
|
|
486
|
-
orgChoice.choices.push(element.name);
|
|
487
|
-
}
|
|
488
|
-
return resolve(orgChoice);
|
|
489
|
-
} catch (e) {
|
|
490
|
-
spinner.fail();
|
|
491
|
-
return reject(e);
|
|
492
|
-
}
|
|
493
|
-
});
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
async getStack(answer, stkMessage) {
|
|
497
|
-
return new Promise(async (resolve, reject) => {
|
|
498
|
-
let stackChoice = {
|
|
499
|
-
type: 'list',
|
|
500
|
-
name: 'stack',
|
|
501
|
-
message: stkMessage !== undefined ? stkMessage : 'Select the stack',
|
|
502
|
-
choices: [],
|
|
503
|
-
};
|
|
504
|
-
const spinner = ora('Fetching stacks').start();
|
|
505
|
-
try {
|
|
506
|
-
const organization_uid = orgUidList[answer.Organization];
|
|
507
|
-
const stackList = client.stack().query({ organization_uid }).find();
|
|
508
|
-
stackList
|
|
509
|
-
.then((stacklist) => {
|
|
510
|
-
for (const element of stacklist.items) {
|
|
511
|
-
stackUidList[element.name] = element.api_key;
|
|
512
|
-
masterLocaleList[element.name] = element.master_locale;
|
|
513
|
-
stackChoice.choices.push(element.name);
|
|
514
|
-
}
|
|
515
|
-
spinner.succeed('Fetched stack');
|
|
516
|
-
return resolve(stackChoice);
|
|
517
|
-
})
|
|
518
|
-
.catch((error) => {
|
|
519
|
-
spinner.fail();
|
|
520
|
-
return reject(error);
|
|
521
|
-
});
|
|
522
|
-
} catch (e) {
|
|
523
|
-
spinner.fail();
|
|
524
|
-
return reject(e);
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
async createNewStack(options) {
|
|
530
|
-
return new Promise(async (resolve, reject) => {
|
|
531
|
-
try {
|
|
532
|
-
const { orgUid } = options;
|
|
533
|
-
this.displayBackOptionMessage();
|
|
534
|
-
let inputvalue;
|
|
535
|
-
if (!config.stackName) {
|
|
536
|
-
prompt.start();
|
|
537
|
-
prompt.message = '';
|
|
538
|
-
this.setCreateNewStackPrompt(prompt);
|
|
539
|
-
inputvalue = await this.getNewStackPromptResult();
|
|
540
|
-
this.setCreateNewStackPrompt(null);
|
|
541
|
-
} else {
|
|
542
|
-
inputvalue = { stack: config.stackName };
|
|
543
|
-
}
|
|
544
|
-
if (this.executingCommand === 0 || !inputvalue) {
|
|
545
|
-
return reject();
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
let stack = { name: inputvalue.stack, master_locale: master_locale };
|
|
549
|
-
const spinner = ora('Creating New stack').start();
|
|
550
|
-
let newStack = client.stack().create({ stack }, { organization_uid: orgUid });
|
|
551
|
-
newStack
|
|
552
|
-
.then((result) => {
|
|
553
|
-
spinner.succeed('New Stack created Successfully name as ' + result.name);
|
|
554
|
-
config.target_stack = result.api_key;
|
|
555
|
-
config.destinationStackName = result.name;
|
|
556
|
-
return resolve(result);
|
|
557
|
-
})
|
|
558
|
-
.catch((error) => {
|
|
559
|
-
spinner.fail();
|
|
560
|
-
return reject(error.errorMessage + ' Contact the Organization owner for Stack Creation access.');
|
|
561
|
-
});
|
|
562
|
-
} catch (error) {
|
|
563
|
-
return reject(error);
|
|
564
|
-
}
|
|
565
|
-
});
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
getNewStackPromptResult() {
|
|
569
|
-
return new Promise((resolve) => {
|
|
570
|
-
prompt.get(
|
|
571
|
-
{
|
|
572
|
-
properties: {
|
|
573
|
-
name: { description: colors.white(stackName.message), default: colors.grey(stackName.default) },
|
|
574
|
-
},
|
|
575
|
-
},
|
|
576
|
-
function (_, result) {
|
|
577
|
-
if (prompt.stopped) {
|
|
578
|
-
prompt.stopped = false;
|
|
579
|
-
resolve();
|
|
580
|
-
} else {
|
|
581
|
-
let _name = result.name.replace(/\[\d+m/g, '');
|
|
582
|
-
_name = _name.replace(//g, '');
|
|
583
|
-
resolve({ stack: _name });
|
|
584
|
-
}
|
|
585
|
-
},
|
|
586
|
-
);
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
async resolveBranchAliases(isSource = false) {
|
|
591
|
-
try {
|
|
592
|
-
if (isSource) {
|
|
593
|
-
const sourceStack = client.stack({ api_key: config.source_stack });
|
|
594
|
-
config.sourceStackBranch = await getBranchFromAlias(sourceStack, config.sourceStackBranchAlias);
|
|
595
|
-
} else {
|
|
596
|
-
const targetStack = client.stack({ api_key: config.target_stack });
|
|
597
|
-
config.targetStackBranch = await getBranchFromAlias(targetStack, config.targetStackBranchAlias);
|
|
598
|
-
}
|
|
599
|
-
} catch (error) {
|
|
600
|
-
throw error;
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
async cloneTypeSelection() {
|
|
605
|
-
console.clear();
|
|
606
|
-
return new Promise(async (resolve, reject) => {
|
|
607
|
-
const choices = [
|
|
608
|
-
'Structure (all modules except entries & assets)',
|
|
609
|
-
'Structure with content (all modules including entries & assets)',
|
|
610
|
-
];
|
|
611
|
-
const cloneTypeSelection = [
|
|
612
|
-
{
|
|
613
|
-
choices,
|
|
614
|
-
type: 'list',
|
|
615
|
-
name: 'type',
|
|
616
|
-
message: 'Choose the type of data to clone:',
|
|
617
|
-
},
|
|
618
|
-
];
|
|
619
|
-
let successMsg;
|
|
620
|
-
let selectedValue = {};
|
|
621
|
-
config['data'] = path.join(__dirname.split('src')[0], 'contents', config.sourceStackBranch || '');
|
|
622
|
-
|
|
623
|
-
if (!config.cloneType) {
|
|
624
|
-
selectedValue = await inquirer.prompt(cloneTypeSelection);
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
if (config.cloneType === 'a' || selectedValue.type === 'Structure (all modules except entries & assets)') {
|
|
628
|
-
config['modules'] = structureList;
|
|
629
|
-
successMsg = 'Stack clone Structure completed';
|
|
630
|
-
} else {
|
|
631
|
-
successMsg = 'Stack clone completed with structure and content';
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
this.cmdImport()
|
|
635
|
-
.then(() => resolve(successMsg))
|
|
636
|
-
.catch(reject);
|
|
637
|
-
});
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
async cmdExport() {
|
|
641
|
-
return new Promise((resolve, reject) => {
|
|
642
|
-
// Creating export specific config by merging external configurations
|
|
643
|
-
let exportConfig = Object.assign({}, cloneDeep(config), { ...config?.export });
|
|
644
|
-
delete exportConfig.import;
|
|
645
|
-
delete exportConfig.export;
|
|
646
|
-
|
|
647
|
-
const cmd = ['-k', exportConfig.source_stack, '-d', __dirname.split('src')[0] + 'contents'];
|
|
648
|
-
if (exportConfig.cloneType === 'a') {
|
|
649
|
-
exportConfig.filteredModules = ['stack'].concat(structureList);
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
if (exportConfig.source_alias) {
|
|
653
|
-
cmd.push('-a', exportConfig.source_alias);
|
|
654
|
-
}
|
|
655
|
-
if (exportConfig.sourceStackBranch) {
|
|
656
|
-
cmd.push('--branch', exportConfig.sourceStackBranch);
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
if (exportConfig.forceStopMarketplaceAppsPrompt) cmd.push('-y');
|
|
660
|
-
|
|
661
|
-
cmd.push('-c');
|
|
662
|
-
cmd.push(path.join(__dirname, 'dummyConfig.json'));
|
|
663
|
-
|
|
664
|
-
fs.writeFileSync(path.join(__dirname, 'dummyConfig.json'), JSON.stringify(exportConfig));
|
|
665
|
-
let exportData = exportCmd.run(cmd);
|
|
666
|
-
exportData.then(() => resolve(true)).catch(reject);
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
async cmdImport() {
|
|
671
|
-
return new Promise(async (resolve, _reject) => {
|
|
672
|
-
// Creating export specific config by merging external configurations
|
|
673
|
-
let importConfig = Object.assign({}, cloneDeep(config), { ...config?.import });
|
|
674
|
-
delete importConfig.import;
|
|
675
|
-
delete importConfig.export;
|
|
676
|
-
|
|
677
|
-
const cmd = ['-c', path.join(__dirname, 'dummyConfig.json')];
|
|
678
|
-
|
|
679
|
-
if (importConfig.destination_alias) {
|
|
680
|
-
cmd.push('-a', importConfig.destination_alias);
|
|
681
|
-
}
|
|
682
|
-
if (!importConfig.data && importConfig.sourceStackBranch) {
|
|
683
|
-
cmd.push('-d', path.join(importConfig.pathDir, importConfig.sourceStackBranch));
|
|
684
|
-
}
|
|
685
|
-
if (importConfig.targetStackBranch) {
|
|
686
|
-
cmd.push('--branch', importConfig.targetStackBranch);
|
|
687
|
-
}
|
|
688
|
-
if (importConfig.importWebhookStatus) {
|
|
689
|
-
cmd.push('--import-webhook-status', importConfig.importWebhookStatus);
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
if (importConfig.skipAudit) cmd.push('--skip-audit');
|
|
693
|
-
|
|
694
|
-
if (importConfig.forceStopMarketplaceAppsPrompt) cmd.push('-y');
|
|
695
|
-
|
|
696
|
-
fs.writeFileSync(path.join(__dirname, 'dummyConfig.json'), JSON.stringify(importConfig));
|
|
697
|
-
await importCmd.run(cmd);
|
|
698
|
-
fs.writeFileSync(path.join(__dirname, 'dummyConfig.json'), JSON.stringify({}));
|
|
699
|
-
return resolve();
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
module.exports = {
|
|
705
|
-
CloneHandler,
|
|
706
|
-
client,
|
|
707
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|