Skip to content

Commit

Permalink
added other files
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-raj committed Sep 10, 2024
1 parent 1475af1 commit 7e1a169
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import { Import, ImportHelperMethodsConfig, LogType, ProjectStruct } from '@contentstack/cli-variants';
import { sanitizePath } from '@contentstack/cli-utilities';
import { ImportConfig, ModuleClassParams } from '../../types';
import {
log,
Expand All @@ -20,9 +21,9 @@ export default class ImportVarientEntries {
this.config = importConfig;
this.personalization = importConfig.modules.personalization;
this.projectMapperFilePath = path.resolve(
this.config.data,
sanitizePath(this.config.data),
'mapper',
this.personalization.dirName,
sanitizePath(this.personalization.dirName),
'projects',
'projects.json',
);
Expand Down
12 changes: 6 additions & 6 deletions packages/contentstack-variants/src/export/attributes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import omit from 'lodash/omit';
import { resolve as pResolve } from 'node:path';

import { sanitizePath } from '@contentstack/cli-utilities';
import { formatError, fsUtil, PersonalizationAdapter, log } from '../utils';
import { PersonalizationConfig, ExportConfig, AttributesConfig, AttributeStruct } from '../types';

Expand All @@ -19,10 +19,10 @@ export default class ExportAttributes extends PersonalizationAdapter<ExportConfi
this.personalizationConfig = exportConfig.modules.personalization;
this.attributesConfig = exportConfig.modules.attributes;
this.attributesFolderPath = pResolve(
exportConfig.data,
exportConfig.branchName || '',
this.personalizationConfig.dirName,
this.attributesConfig.dirName,
sanitizePath(exportConfig.data),
sanitizePath(exportConfig.branchName || ''),
sanitizePath(this.personalizationConfig.dirName),
sanitizePath(this.attributesConfig.dirName),
);
this.attributes = [];
}
Expand All @@ -37,7 +37,7 @@ export default class ExportAttributes extends PersonalizationAdapter<ExportConfi
log(this.exportConfig, 'No Attributes found with the given project!', 'info');
} else {
this.sanitizeAttribs();
fsUtil.writeFile(pResolve(this.attributesFolderPath, this.attributesConfig.fileName), this.attributes);
fsUtil.writeFile(pResolve(sanitizePath(this.attributesFolderPath), sanitizePath(this.attributesConfig.fileName)), this.attributes);
log(this.exportConfig, 'All the attributes have been exported successfully!', 'success');
}
} catch (error) {
Expand Down
13 changes: 7 additions & 6 deletions packages/contentstack-variants/src/export/experiences.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path';
import { sanitizePath } from '@contentstack/cli-utilities';
import { PersonalizationConfig, ExportConfig, ExperienceStruct } from '../types';
import { formatError, fsUtil, log, PersonalizationAdapter } from '../utils';

Expand All @@ -19,9 +20,9 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
this.exportConfig = exportConfig;
this.personalizationConfig = exportConfig.modules.personalization;
this.experiencesFolderPath = path.resolve(
exportConfig.data,
exportConfig.branchName || '',
this.personalizationConfig.dirName,
sanitizePath(exportConfig.data),
sanitizePath(exportConfig.branchName || ''),
sanitizePath(this.personalizationConfig.dirName),
'experiences',
);
}
Expand All @@ -38,7 +39,7 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
log(this.exportConfig, 'No Experiences found with the give project', 'info');
return;
}
fsUtil.writeFile(path.resolve(this.experiencesFolderPath, 'experiences.json'), experiences);
fsUtil.writeFile(path.resolve(sanitizePath(this.experiencesFolderPath), 'experiences.json'), experiences);

const experienceToVariantsStrList: Array<string> = [];
const experienceToContentTypesMap: Record<string, string[]> = {};
Expand All @@ -62,12 +63,12 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
}
}
fsUtil.writeFile(
path.resolve(this.experiencesFolderPath, 'experiences-variants-ids.json'),
path.resolve(sanitizePath(this.experiencesFolderPath), 'experiences-variants-ids.json'),
experienceToVariantsStrList,
);

fsUtil.writeFile(
path.resolve(this.experiencesFolderPath, 'experiences-content-types.json'),
path.resolve(sanitizePath(this.experiencesFolderPath), 'experiences-content-types.json'),
experienceToContentTypesMap,
);
log(this.exportConfig, 'All the experiences have been exported successfully!', 'success');
Expand Down
10 changes: 5 additions & 5 deletions packages/contentstack-variants/src/export/projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';

import { sanitizePath } from '@contentstack/cli-utilities';
import { ExportConfig, PersonalizationConfig } from '../types';
import { PersonalizationAdapter, log, fsUtil, formatError } from '../utils';

Expand All @@ -16,9 +16,9 @@ export default class ExportProjects extends PersonalizationAdapter<ExportConfig>
this.exportConfig = exportConfig;
this.personalizationConfig = exportConfig.modules.personalization;
this.projectFolderPath = path.resolve(
exportConfig.data,
exportConfig.branchName || '',
this.personalizationConfig.dirName,
sanitizePath(exportConfig.data),
sanitizePath(exportConfig.branchName || ''),
sanitizePath(this.personalizationConfig.dirName),
'projects',
);
}
Expand All @@ -35,7 +35,7 @@ export default class ExportProjects extends PersonalizationAdapter<ExportConfig>
}
this.exportConfig.personalizationEnabled = true;
this.exportConfig.project_id = project[0]?.uid;
fsUtil.writeFile(path.resolve(this.projectFolderPath, 'projects.json'), project);
fsUtil.writeFile(path.resolve(sanitizePath(this.projectFolderPath), 'projects.json'), project);
log(this.exportConfig, 'Project exported successfully!', 'success');
} catch (error) {
log(this.exportConfig, `Failed to export projects!`, 'error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Exp
},
};
super(Object.assign(config, conf));
this.entriesDirPath = resolve(sanitizePath(config.data), config.branchName || '', sanitizePath(config.modules.entries.dirName));
this.entriesDirPath = resolve(sanitizePath(config.data), sanitizePath(config.branchName || ''), sanitizePath(config.modules.entries.dirName));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/contentstack-variants/src/import/attribute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path';
import { existsSync } from 'fs';

import { sanitizePath } from '@contentstack/cli-utilities';
import { PersonalizationAdapter, fsUtil } from '../utils';
import { APIConfig, AttributeStruct, ImportConfig, LogType } from '../types';

Expand All @@ -21,9 +21,9 @@ export default class Attribute extends PersonalizationAdapter<ImportConfig> {
super(Object.assign(config, conf));
this.personalizationConfig = this.config.modules.personalization;
this.attributeConfig = this.personalizationConfig.attributes;
this.mapperDirPath = resolve(this.config.backupDir, 'mapper', this.personalizationConfig.dirName);
this.attrMapperDirPath = resolve(this.mapperDirPath, this.attributeConfig.dirName);
this.attributesUidMapperPath = resolve(this.attrMapperDirPath, 'uid-mapping.json');
this.mapperDirPath = resolve(sanitizePath(this.config.backupDir), 'mapper', sanitizePath(this.personalizationConfig.dirName));
this.attrMapperDirPath = resolve(sanitizePath(this.mapperDirPath), sanitizePath(this.attributeConfig.dirName));
this.attributesUidMapperPath = resolve(sanitizePath(this.attrMapperDirPath), 'uid-mapping.json');
this.attributesUidMapper = {};
}

Expand All @@ -35,7 +35,7 @@ export default class Attribute extends PersonalizationAdapter<ImportConfig> {

await fsUtil.makeDirectory(this.attrMapperDirPath);
const { dirName, fileName } = this.attributeConfig;
const attributesPath = resolve(this.config.data, this.personalizationConfig.dirName, dirName, fileName);
const attributesPath = resolve(sanitizePath(this.config.data), sanitizePath(this.personalizationConfig.dirName), sanitizePath(dirName), sanitizePath(fileName));

if (existsSync(attributesPath)) {
try {
Expand Down
12 changes: 6 additions & 6 deletions packages/contentstack-variants/src/import/audiences.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path';
import { existsSync } from 'fs';

import { sanitizePath } from '@contentstack/cli-utilities';
import { APIConfig, AudienceStruct, ImportConfig, LogType } from '../types';
import { PersonalizationAdapter, fsUtil, lookUpAttributes } from '../utils';

Expand All @@ -24,10 +24,10 @@ export default class Audiences extends PersonalizationAdapter<ImportConfig> {
this.personalizationConfig = this.config.modules.personalization;
this.audienceConfig = this.personalizationConfig.audiences;
this.attributeConfig = this.personalizationConfig.attributes;
this.mapperDirPath = resolve(this.config.backupDir, 'mapper', this.personalizationConfig.dirName);
this.audienceMapperDirPath = resolve(this.mapperDirPath, this.audienceConfig.dirName);
this.audiencesUidMapperPath = resolve(this.audienceMapperDirPath, 'uid-mapping.json');
this.attributesMapperPath = resolve(this.mapperDirPath, this.attributeConfig.dirName, 'uid-mapping.json');
this.mapperDirPath = resolve(sanitizePath(this.config.backupDir), 'mapper', sanitizePath(this.personalizationConfig.dirName));
this.audienceMapperDirPath = resolve(sanitizePath(this.mapperDirPath), sanitizePath(this.audienceConfig.dirName));
this.audiencesUidMapperPath = resolve(sanitizePath(this.audienceMapperDirPath), 'uid-mapping.json');
this.attributesMapperPath = resolve(sanitizePath(this.mapperDirPath), sanitizePath(this.attributeConfig.dirName), 'uid-mapping.json');
this.audiencesUidMapper = {};
}

Expand All @@ -39,7 +39,7 @@ export default class Audiences extends PersonalizationAdapter<ImportConfig> {

await fsUtil.makeDirectory(this.audienceMapperDirPath);
const { dirName, fileName } = this.audienceConfig;
const audiencesPath = resolve(this.config.data, this.personalizationConfig.dirName, dirName, fileName);
const audiencesPath = resolve(sanitizePath(this.config.data), sanitizePath(this.personalizationConfig.dirName), sanitizePath(dirName), sanitizePath(fileName));

if (existsSync(audiencesPath)) {
try {
Expand Down
10 changes: 5 additions & 5 deletions packages/contentstack-variants/src/import/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path';
import { existsSync } from 'fs';

import { sanitizePath } from '@contentstack/cli-utilities';
import { PersonalizationAdapter, fsUtil } from '../utils';
import { APIConfig, EventStruct, ImportConfig, LogType } from '../types';

Expand All @@ -21,9 +21,9 @@ export default class Events extends PersonalizationAdapter<ImportConfig> {
super(Object.assign(config, conf));
this.personalizationConfig = this.config.modules.personalization;
this.eventsConfig = this.personalizationConfig.events;
this.mapperDirPath = resolve(this.config.backupDir, 'mapper', this.personalizationConfig.dirName);
this.eventMapperDirPath = resolve(this.mapperDirPath, this.eventsConfig.dirName);
this.eventsUidMapperPath = resolve(this.eventMapperDirPath, 'uid-mapping.json');
this.mapperDirPath = resolve(sanitizePath(this.config.backupDir), 'mapper', sanitizePath(this.personalizationConfig.dirName));
this.eventMapperDirPath = resolve(sanitizePath(this.mapperDirPath), sanitizePath(this.eventsConfig.dirName));
this.eventsUidMapperPath = resolve(sanitizePath(this.eventMapperDirPath), 'uid-mapping.json');
this.eventsUidMapper = {};
}

Expand All @@ -35,7 +35,7 @@ export default class Events extends PersonalizationAdapter<ImportConfig> {

await fsUtil.makeDirectory(this.eventMapperDirPath);
const { dirName, fileName } = this.eventsConfig;
const eventsPath = resolve(this.config.data, this.personalizationConfig.dirName, dirName, fileName);
const eventsPath = resolve(sanitizePath(this.config.data), sanitizePath(this.personalizationConfig.dirName), sanitizePath(dirName), sanitizePath(fileName));

if (existsSync(eventsPath)) {
try {
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-variants/src/import/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
super(Object.assign(config, conf));
this.personalizationConfig = this.config.modules.personalization;
this.experiencesDirPath = resolve(
this.config.data,
this.personalizationConfig.dirName,
this.personalizationConfig.experiences.dirName,
sanitizePath(this.config.data),
sanitizePath(this.personalizationConfig.dirName),
sanitizePath(this.personalizationConfig.experiences.dirName),
);
this.experiencesPath = join(sanitizePath(this.experiencesDirPath), sanitizePath(this.personalizationConfig.experiences.fileName));
this.experienceConfig = this.personalizationConfig.experiences;
Expand Down
8 changes: 4 additions & 4 deletions packages/contentstack-variants/src/import/variant-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
},
};
super(Object.assign(omit(config, ['helpers']), conf));
this.entriesMapperPath = resolve(sanitizePath(config.backupDir), config.branchName || '', 'mapper', 'entries');
this.entriesMapperPath = resolve(sanitizePath(config.backupDir), sanitizePath(config.branchName || ''), 'mapper', 'entries');
this.personalizationConfig = this.config.modules.personalization;
this.entriesDirPath = resolve(sanitizePath(config.backupDir), config.branchName || '', sanitizePath(config.modules.entries.dirName));
this.entriesDirPath = resolve(sanitizePath(config.backupDir), sanitizePath(config.branchName || ''), sanitizePath(config.modules.entries.dirName));
this.failedVariantPath = resolve(sanitizePath(this.entriesMapperPath), 'failed-entry-variants.json');
this.failedVariantEntries = new Map();
}
Expand Down Expand Up @@ -141,7 +141,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
const { content_type, locale, entry_uid } = entriesForVariant;
const ctConfig = this.config.modules['content-types'];
const contentType: ContentTypeStruct = JSON.parse(
readFileSync(resolve(sanitizePath(this.config.backupDir), sanitizePath(ctConfig.dirName), `${content_type}.json`), 'utf8'),
readFileSync(resolve(sanitizePath(this.config.backupDir), sanitizePath(ctConfig.dirName), `${sanitizePath(content_type)}.json`), 'utf8'),
);
const variantEntryBasePath = join(sanitizePath(this.entriesDirPath), sanitizePath(content_type), sanitizePath(locale), sanitizePath(variantEntry.dirName), sanitizePath(entry_uid));
const fs = new FsUtility({ basePath: variantEntryBasePath });
Expand Down Expand Up @@ -308,7 +308,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
content_type: contentType,
},
this.entriesUidMapper,
resolve(this.entriesMapperPath, contentType.uid, variantEntry.locale),
resolve(sanitizePath(this.entriesMapperPath), sanitizePath(contentType.uid), sanitizePath(variantEntry.locale)),
);

// NOTE: will remove term if term doesn't exists in taxonomy
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let errorTransport;

function init(_logPath: string) {
if (!logger || !errorLogger) {
const logsDir = path.resolve(_logPath, 'logs', 'export');
const logsDir = path.resolve(sanitizePath(_logPath), 'logs', 'export');
// Create dir if doesn't already exist
mkdirp.sync(logsDir);

Expand Down

0 comments on commit 7e1a169

Please sign in to comment.