Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: improves cli logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Mar 9, 2020
1 parent a476e4d commit 2f41f8b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
27 changes: 15 additions & 12 deletions src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs';
import yargs = require('yargs');

import { CleanService } from '../clean';
import { ICliFileConfig, fileHelper } from '../core';
import { ICliFileConfig, fileHelper, getFilenameWithoutExtension } from '../core';
import { ExportService, IExportAllResult } from '../export';
import { IImportSource, ImportService } from '../import';
import { ZipService } from '../zip';
Expand Down Expand Up @@ -36,22 +36,25 @@ const backup = async (config: ICliFileConfig) => {

const report = await exportService.exportProjectValidationAsync();

if (canExport(report, config)) {
const response = await exportService.exportAllAsync();
await zipService.createZipAsync(response);
const response = await exportService.exportAllAsync();
await zipService.createZipAsync(response);

console.log('Completed');
} else {
const logFilename: string = 'backup_data_inconsistencies_log.json';
if (exportContainsInconsistencies(report, config)) {
const logFilename: string = getLogFilename(config.zipFilename);

await fileHelper.createFileInCurrentFolderAsync(logFilename, JSON.stringify(report));

console.log(`Project could not be exported due to data inconsistencies.`);
console.log(`A log file '${logFilename}' with issues was created in current folder.`);
console.log(`To export data regardless of issues, set 'force' config parameter to true`);
console.log(`Project contains inconsistencies which may cause future import to not work.`);
console.log(`See '${logFilename}' for more details.`);
}

console.log('Completed');
};

const getLogFilename = (filename: string) => {
return`${getFilenameWithoutExtension(filename)}_log.json`;
}

const clean = async (config: ICliFileConfig) => {
const cleanService = new CleanService({
onDelete: item => {
Expand Down Expand Up @@ -99,7 +102,7 @@ const restore = async (config: ICliFileConfig) => {

console.log('Completed');
} else {
const logFilename: string = 'import_data_inconsistencies_log.json';
const logFilename: string = getLogFilename(config.zipFilename);

await fileHelper.createFileInCurrentFolderAsync(logFilename, JSON.stringify(data.validation));

Expand Down Expand Up @@ -149,7 +152,7 @@ const process = async () => {
}
};

const canExport = (projectReport: ProjectContracts.IProjectReportResponseContract, config: ICliFileConfig) => {
const exportContainsInconsistencies = (projectReport: ProjectContracts.IProjectReportResponseContract, config: ICliFileConfig) => {
const projectHasIssues = projectReport.variant_issues.length > 0 || projectReport.type_issues.length > 0;
if (!projectHasIssues) {
return true;
Expand Down
14 changes: 14 additions & 0 deletions src/core/global-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function getFilenameWithoutExtension(filename: string): string {
if (!filename) {
throw Error(`Invalid filename`);
}

if (!filename.includes('.')) {
return filename
};

return filename
.split('.')
.slice(0, -1)
.join('.');
}
3 changes: 2 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './core.models';
export * from './translation-helper';
export * from './id-translate-helper';
export * from './file-helper';
export * from './file-helper';
export * from './global-helper';
3 changes: 1 addition & 2 deletions src/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class ImportService {
const importedTaxonomies = await this.importTaxonomiesAsync(sourceData.importData.taxonomies);
importedItems.push(...importedTaxonomies);

// ### Dummy types & snippets
// ### Content types & snippets
await this.importContentTypeSnippetsAsync(sourceData.importData.contentTypeSnippets);
await this.importContentTypesAsync(sourceData.importData.contentTypes);

Expand Down Expand Up @@ -540,7 +540,6 @@ export class ImportService {
>[] = [];

for (const contentTypeSnippet of contentTypeSnippets) {
// first create dummy types to handle circular references between types & types that reference
// not yet processed ones
const createdContentTypeSnippet = await this.client
.addContentTypeSnippet()
Expand Down

0 comments on commit 2f41f8b

Please sign in to comment.