Skip to content

Commit 3cd2bbb

Browse files
authored
Merge pull request #2064 from contentstack/feat/DX-3289
feat: Integrated CLIProgressManager and SummaryManager in import (assets, stack, environments , taxonomies, locales, extensions & global-fields)
2 parents 3afefb1 + 7e74a98 commit 3cd2bbb

File tree

11 files changed

+1124
-532
lines changed

11 files changed

+1124
-532
lines changed

packages/contentstack-import/src/commands/cm/stacks/import.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
flags,
77
FlagInput,
88
ContentstackClient,
9-
pathValidator,
109
log,
1110
handleAndLogError,
1211
configHandler,
1312
getLogPath,
13+
CLIProgressManager,
14+
cliux,
1415
} from '@contentstack/cli-utilities';
1516

1617
import { Context, ImportConfig } from '../../../types';
@@ -181,6 +182,16 @@ export default class ImportCommand extends Command {
181182
}
182183
}
183184

185+
if (flags.branch) {
186+
CLIProgressManager.initializeGlobalSummary(
187+
`IMPORT-${flags.branch}`,
188+
flags.branch,
189+
`IMPORTING DATA INTO "${flags.branch}" BRANCH`,
190+
);
191+
} else {
192+
CLIProgressManager.initializeGlobalSummary(`IMPORT`, flags.branch, 'IMPORTING CONTENT');
193+
}
194+
184195
const moduleImporter = new ModuleImporter(managementAPIClient, importConfig);
185196
const result = await moduleImporter.start();
186197
backupDir = importConfig.backupDir;
@@ -192,16 +203,48 @@ export default class ImportCommand extends Command {
192203
log.success(successMessage, importConfig.context);
193204
}
194205

195-
log.success(`The log has been stored at '${getLogPath()}'`, importConfig.context);
196-
log.info(`The backup content has been stored at '${backupDir}'`, importConfig.context);
206+
CLIProgressManager.printGlobalSummary();
207+
this.logSuccessAndBackupMessages(backupDir, importConfig);
197208
} catch (error) {
198209
handleAndLogError(error);
199-
log.info(`The log has been stored at '${getLogPath()}'`);
200-
if (importConfig?.backupDir) {
201-
log.info(`The backup content has been stored at '${importConfig?.backupDir}'`);
202-
} else {
203-
log.info('No backup directory was created due to early termination');
204-
}
210+
this.logAndPrintErrorDetails(error, importConfig);
211+
}
212+
}
213+
214+
private logAndPrintErrorDetails(error: unknown, importConfig: any) {
215+
cliux.print('\n');
216+
const logPath = getLogPath();
217+
const logMsg = `The log has been stored at '${logPath}'`;
218+
219+
const backupDir = importConfig?.backupDir;
220+
const backupDirMsg = backupDir
221+
? `The backup content has been stored at '${backupDir}'`
222+
: 'No backup directory was created due to early termination';
223+
224+
log.info(logMsg);
225+
log.info(backupDirMsg);
226+
227+
const showConsoleLogs = configHandler.get('log')?.showConsoleLogs;
228+
if (!showConsoleLogs) {
229+
cliux.print(`Error: ${error}`, { color: 'red' });
230+
cliux.print(logMsg, { color: 'blue' });
231+
cliux.print(backupDirMsg, { color: 'blue' });
232+
}
233+
}
234+
235+
private logSuccessAndBackupMessages(backupDir: string, importConfig: any) {
236+
cliux.print('\n');
237+
const logPath = getLogPath();
238+
const logMsg = `The log has been stored at '${logPath}'`;
239+
const backupDirMsg = `The backup content has been stored at '${backupDir}'`;
240+
241+
log.success(logMsg, importConfig.context);
242+
log.info(backupDirMsg, importConfig.context);
243+
244+
const showConsoleLogs = configHandler.get('log')?.showConsoleLogs;
245+
if (!showConsoleLogs) {
246+
cliux.print(logMsg, { color: 'blue' });
247+
cliux.print(backupDirMsg, { color: 'blue' });
205248
}
206249
}
207250

packages/contentstack-import/src/import/module-importer.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { resolve } from 'path';
22
import { AuditFix } from '@contentstack/cli-audit';
33
import messages, { $t } from '@contentstack/cli-audit/lib/messages';
4-
import { addLocale, cliux, ContentstackClient, Logger } from '@contentstack/cli-utilities';
4+
import { addLocale, cliux, ContentstackClient, Logger, log, handleAndLogError } from '@contentstack/cli-utilities';
55

66
import startModuleImport from './modules';
77
import startJSModuleImport from './modules-js';
88
import { ImportConfig, Modules } from '../types';
9-
import { backupHandler, log, validateBranch, masterLocalDetails, sanitizeStack, initLogger, trace } from '../utils';
9+
import { backupHandler, validateBranch, masterLocalDetails, sanitizeStack, initLogger, trace } from '../utils';
1010

1111
class ModuleImporter {
1212
private managementAPIClient: ContentstackClient;
@@ -50,15 +50,9 @@ class ModuleImporter {
5050
if (
5151
!this.importConfig.skipAudit &&
5252
(!this.importConfig.moduleName ||
53-
[
54-
'content-types',
55-
'global-fields',
56-
'entries',
57-
'extensions',
58-
'workflows',
59-
'custom-roles',
60-
'assets'
61-
].includes(this.importConfig.moduleName))
53+
['content-types', 'global-fields', 'entries', 'extensions', 'workflows', 'custom-roles', 'assets'].includes(
54+
this.importConfig.moduleName,
55+
))
6256
) {
6357
if (!(await this.auditImportData(logger))) {
6458
return { noSuccessMsg: true };
@@ -77,7 +71,7 @@ class ModuleImporter {
7771
}
7872

7973
async import() {
80-
log(this.importConfig, `Starting to import content version ${this.importConfig.contentVersion}`, 'info');
74+
log.info(`Starting to import content version ${this.importConfig.contentVersion}`, this.importConfig.context);
8175

8276
// checks for single module or all modules
8377
if (this.importConfig.singleModuleImport) {
@@ -87,7 +81,7 @@ class ModuleImporter {
8781
}
8882

8983
async importByModuleByName(moduleName: Modules) {
90-
log(this.importConfig, `Starting import of ${moduleName} module`, 'info');
84+
log.info(`Starting import of ${moduleName} module`, this.importConfig.context);
9185
// import the modules by name
9286
// calls the module runner which inturn calls the module itself
9387
// NOTE: Implement a mechanism to determine whether module is new or old
@@ -113,10 +107,9 @@ class ModuleImporter {
113107
// use the algorithm to determine the parallel and sequential execution of modules
114108
for (let moduleName of this.importConfig.modules.types) {
115109
if (this.importConfig.globalModules.includes(moduleName) && this.importConfig['exclude-global-modules']) {
116-
log(
117-
this.importConfig,
110+
log.warn(
118111
`Skipping the import of the global module '${moduleName}', as it already exists in the stack.`,
119-
'warn',
112+
this.importConfig.context,
120113
);
121114
continue;
122115
}
@@ -150,24 +143,18 @@ class ModuleImporter {
150143
} else if (this.importConfig.modules.types.length) {
151144
this.importConfig.modules.types
152145
.filter((val) =>
153-
[
154-
'content-types',
155-
'global-fields',
156-
'entries',
157-
'extensions',
158-
'workflows',
159-
'custom-roles',
160-
'assets'
161-
].includes(val),
146+
['content-types', 'global-fields', 'entries', 'extensions', 'workflows', 'custom-roles', 'assets'].includes(
147+
val,
148+
),
162149
)
163150
.forEach((val) => {
164151
args.push('--modules', val);
165152
});
166153
}
167154
args.push('--modules', 'field-rules');
168-
log(this.importConfig, 'Starting audit process', 'info');
155+
log.info('Starting audit process', this.importConfig.context);
169156
const result = await AuditFix.run(args);
170-
log(this.importConfig, 'Audit process completed', 'info');
157+
log.info('Audit process completed', this.importConfig.context);
171158

172159
if (result) {
173160
const { hasFix, config } = result;
@@ -193,7 +180,7 @@ class ModuleImporter {
193180

194181
return true;
195182
} catch (error) {
196-
log(this.importConfig, `Audit failed with following error. ${error}`, 'error');
183+
handleAndLogError(error, { ...this.importConfig.context });
197184
}
198185
}
199186
}

0 commit comments

Comments
 (0)