Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions packages/contentstack-import/src/commands/cm/stacks/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
flags,
FlagInput,
ContentstackClient,
pathValidator,
log,
handleAndLogError,
configHandler,
getLogPath,
CLIProgressManager,
cliux,
} from '@contentstack/cli-utilities';

import { Context, ImportConfig } from '../../../types';
Expand Down Expand Up @@ -181,6 +182,16 @@ export default class ImportCommand extends Command {
}
}

if (flags.branch) {
CLIProgressManager.initializeGlobalSummary(
`IMPORT-${flags.branch}`,
flags.branch,
`IMPORTING DATA INTO "${flags.branch}" BRANCH`,
);
} else {
CLIProgressManager.initializeGlobalSummary(`IMPORT`, flags.branch, 'IMPORTING CONTENT');
}

const moduleImporter = new ModuleImporter(managementAPIClient, importConfig);
const result = await moduleImporter.start();
backupDir = importConfig.backupDir;
Expand All @@ -192,16 +203,48 @@ export default class ImportCommand extends Command {
log.success(successMessage, importConfig.context);
}

log.success(`The log has been stored at '${getLogPath()}'`, importConfig.context);
log.info(`The backup content has been stored at '${backupDir}'`, importConfig.context);
CLIProgressManager.printGlobalSummary();
this.logSuccessAndBackupMessages(backupDir, importConfig);
} catch (error) {
handleAndLogError(error);
log.info(`The log has been stored at '${getLogPath()}'`);
if (importConfig?.backupDir) {
log.info(`The backup content has been stored at '${importConfig?.backupDir}'`);
} else {
log.info('No backup directory was created due to early termination');
}
this.logAndPrintErrorDetails(error, importConfig);
}
}

private logAndPrintErrorDetails(error: unknown, importConfig: any) {
cliux.print('\n');
const logPath = getLogPath();
const logMsg = `The log has been stored at '${logPath}'`;

const backupDir = importConfig?.backupDir;
const backupDirMsg = backupDir
? `The backup content has been stored at '${backupDir}'`
: 'No backup directory was created due to early termination';

log.info(logMsg);
log.info(backupDirMsg);

const showConsoleLogs = configHandler.get('log')?.showConsoleLogs;
if (!showConsoleLogs) {
cliux.print(`Error: ${error}`, { color: 'red' });
cliux.print(logMsg, { color: 'blue' });
cliux.print(backupDirMsg, { color: 'blue' });
}
}

private logSuccessAndBackupMessages(backupDir: string, importConfig: any) {
cliux.print('\n');
const logPath = getLogPath();
const logMsg = `The log has been stored at '${logPath}'`;
const backupDirMsg = `The backup content has been stored at '${backupDir}'`;

log.success(logMsg, importConfig.context);
log.info(backupDirMsg, importConfig.context);

const showConsoleLogs = configHandler.get('log')?.showConsoleLogs;
if (!showConsoleLogs) {
cliux.print(logMsg, { color: 'blue' });
cliux.print(backupDirMsg, { color: 'blue' });
}
}

Expand Down
43 changes: 15 additions & 28 deletions packages/contentstack-import/src/import/module-importer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { resolve } from 'path';
import { AuditFix } from '@contentstack/cli-audit';
import messages, { $t } from '@contentstack/cli-audit/lib/messages';
import { addLocale, cliux, ContentstackClient, Logger } from '@contentstack/cli-utilities';
import { addLocale, cliux, ContentstackClient, Logger, log, handleAndLogError } from '@contentstack/cli-utilities';

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

class ModuleImporter {
private managementAPIClient: ContentstackClient;
Expand Down Expand Up @@ -50,15 +50,9 @@ class ModuleImporter {
if (
!this.importConfig.skipAudit &&
(!this.importConfig.moduleName ||
[
'content-types',
'global-fields',
'entries',
'extensions',
'workflows',
'custom-roles',
'assets'
].includes(this.importConfig.moduleName))
['content-types', 'global-fields', 'entries', 'extensions', 'workflows', 'custom-roles', 'assets'].includes(
this.importConfig.moduleName,
))
) {
if (!(await this.auditImportData(logger))) {
return { noSuccessMsg: true };
Expand All @@ -77,7 +71,7 @@ class ModuleImporter {
}

async import() {
log(this.importConfig, `Starting to import content version ${this.importConfig.contentVersion}`, 'info');
log.info(`Starting to import content version ${this.importConfig.contentVersion}`, this.importConfig.context);

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

async importByModuleByName(moduleName: Modules) {
log(this.importConfig, `Starting import of ${moduleName} module`, 'info');
log.info(`Starting import of ${moduleName} module`, this.importConfig.context);
// import the modules by name
// calls the module runner which inturn calls the module itself
// NOTE: Implement a mechanism to determine whether module is new or old
Expand All @@ -113,10 +107,9 @@ class ModuleImporter {
// use the algorithm to determine the parallel and sequential execution of modules
for (let moduleName of this.importConfig.modules.types) {
if (this.importConfig.globalModules.includes(moduleName) && this.importConfig['exclude-global-modules']) {
log(
this.importConfig,
log.warn(
`Skipping the import of the global module '${moduleName}', as it already exists in the stack.`,
'warn',
this.importConfig.context,
);
continue;
}
Expand Down Expand Up @@ -150,24 +143,18 @@ class ModuleImporter {
} else if (this.importConfig.modules.types.length) {
this.importConfig.modules.types
.filter((val) =>
[
'content-types',
'global-fields',
'entries',
'extensions',
'workflows',
'custom-roles',
'assets'
].includes(val),
['content-types', 'global-fields', 'entries', 'extensions', 'workflows', 'custom-roles', 'assets'].includes(
val,
),
)
.forEach((val) => {
args.push('--modules', val);
});
}
args.push('--modules', 'field-rules');
log(this.importConfig, 'Starting audit process', 'info');
log.info('Starting audit process', this.importConfig.context);
const result = await AuditFix.run(args);
log(this.importConfig, 'Audit process completed', 'info');
log.info('Audit process completed', this.importConfig.context);

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

return true;
} catch (error) {
log(this.importConfig, `Audit failed with following error. ${error}`, 'error');
handleAndLogError(error, { ...this.importConfig.context });
}
}
}
Expand Down
Loading