@contentstack/cli-variants 0.0.1-alpha
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/lib/export/attributes.d.ts +15 -0
- package/lib/export/attributes.js +62 -0
- package/lib/export/audiences.d.ts +15 -0
- package/lib/export/audiences.js +63 -0
- package/lib/export/events.d.ts +15 -0
- package/lib/export/events.js +63 -0
- package/lib/export/experiences.d.ts +9 -0
- package/lib/export/experiences.js +99 -0
- package/lib/export/index.d.ts +9 -0
- package/lib/export/index.js +21 -0
- package/lib/export/projects.d.ts +9 -0
- package/lib/export/projects.js +73 -0
- package/lib/export/variant-entries.d.ts +18 -0
- package/lib/export/variant-entries.js +109 -0
- package/lib/import/attribute.d.ts +17 -0
- package/lib/import/attribute.js +64 -0
- package/lib/import/audiences.d.ts +19 -0
- package/lib/import/audiences.js +71 -0
- package/lib/import/events.d.ts +17 -0
- package/lib/import/events.js +62 -0
- package/lib/import/experiences.d.ts +46 -0
- package/lib/import/experiences.js +214 -0
- package/lib/import/index.d.ts +14 -0
- package/lib/import/index.js +21 -0
- package/lib/import/project.d.ts +13 -0
- package/lib/import/project.js +74 -0
- package/lib/import/variant-entries.d.ts +98 -0
- package/lib/import/variant-entries.js +407 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +21 -0
- package/lib/messages/index.d.ts +35 -0
- package/lib/messages/index.js +55 -0
- package/lib/types/adapter-helper.d.ts +8 -0
- package/lib/types/adapter-helper.js +2 -0
- package/lib/types/content-types.d.ts +19 -0
- package/lib/types/content-types.js +2 -0
- package/lib/types/export-config.d.ts +264 -0
- package/lib/types/export-config.js +2 -0
- package/lib/types/import-config.d.ts +92 -0
- package/lib/types/import-config.js +2 -0
- package/lib/types/index.d.ts +8 -0
- package/lib/types/index.js +24 -0
- package/lib/types/personalization-api-adapter.d.ts +152 -0
- package/lib/types/personalization-api-adapter.js +2 -0
- package/lib/types/utils.d.ts +7 -0
- package/lib/types/utils.js +2 -0
- package/lib/types/variant-api-adapter.d.ts +49 -0
- package/lib/types/variant-api-adapter.js +2 -0
- package/lib/types/variant-entry.d.ts +47 -0
- package/lib/types/variant-entry.js +2 -0
- package/lib/utils/adapter-helper.d.ts +30 -0
- package/lib/utils/adapter-helper.js +95 -0
- package/lib/utils/attributes-helper.d.ts +7 -0
- package/lib/utils/attributes-helper.js +37 -0
- package/lib/utils/audiences-helper.d.ts +8 -0
- package/lib/utils/audiences-helper.js +49 -0
- package/lib/utils/error-helper.d.ts +6 -0
- package/lib/utils/error-helper.js +27 -0
- package/lib/utils/events-helper.d.ts +8 -0
- package/lib/utils/events-helper.js +27 -0
- package/lib/utils/helper.d.ts +4 -0
- package/lib/utils/helper.js +51 -0
- package/lib/utils/index.d.ts +9 -0
- package/lib/utils/index.js +25 -0
- package/lib/utils/logger.d.ts +3 -0
- package/lib/utils/logger.js +175 -0
- package/lib/utils/personalization-api-adapter.d.ts +73 -0
- package/lib/utils/personalization-api-adapter.js +184 -0
- package/lib/utils/variant-api-adapter.d.ts +79 -0
- package/lib/utils/variant-api-adapter.js +263 -0
- package/package.json +38 -0
- package/src/export/attributes.ts +55 -0
- package/src/export/audiences.ts +57 -0
- package/src/export/events.ts +57 -0
- package/src/export/experiences.ts +80 -0
- package/src/export/index.ts +11 -0
- package/src/export/projects.ts +45 -0
- package/src/export/variant-entries.ts +88 -0
- package/src/import/attribute.ts +60 -0
- package/src/import/audiences.ts +69 -0
- package/src/import/events.ts +58 -0
- package/src/import/experiences.ts +224 -0
- package/src/import/index.ts +16 -0
- package/src/import/project.ts +71 -0
- package/src/import/variant-entries.ts +483 -0
- package/src/index.ts +5 -0
- package/src/messages/index.ts +63 -0
- package/src/types/adapter-helper.ts +10 -0
- package/src/types/content-types.ts +41 -0
- package/src/types/export-config.ts +292 -0
- package/src/types/import-config.ts +95 -0
- package/src/types/index.ts +8 -0
- package/src/types/personalization-api-adapter.ts +197 -0
- package/src/types/utils.ts +8 -0
- package/src/types/variant-api-adapter.ts +56 -0
- package/src/types/variant-entry.ts +61 -0
- package/src/utils/adapter-helper.ts +79 -0
- package/src/utils/attributes-helper.ts +31 -0
- package/src/utils/audiences-helper.ts +50 -0
- package/src/utils/error-helper.ts +26 -0
- package/src/utils/events-helper.ts +26 -0
- package/src/utils/helper.ts +34 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/logger.ts +160 -0
- package/src/utils/personalization-api-adapter.ts +188 -0
- package/src/utils/variant-api-adapter.ts +326 -0
- package/test/unit/export/variant-entries.test.ts +80 -0
- package/test/unit/import/variant-entries.test.ts +200 -0
- package/test/unit/mock/contents/content_types/CT-1.json +7 -0
- package/test/unit/mock/contents/entries/CT-1/en-us/variants/E-1/9b0da6xd7et72y-6gv7he23.json +12 -0
- package/test/unit/mock/contents/entries/CT-1/en-us/variants/E-1/index.json +3 -0
- package/test/unit/mock/contents/entries/CT-1/en-us/variants/E-2/9b0da6xd7et72y-6gv7he23.json +12 -0
- package/test/unit/mock/contents/entries/CT-1/en-us/variants/E-2/index.json +3 -0
- package/test/unit/mock/contents/mapper/assets/uid-mapping.json +6 -0
- package/test/unit/mock/contents/mapper/assets/url-mapping.json +6 -0
- package/test/unit/mock/contents/mapper/entries/data-for-variant-entry.json +6 -0
- package/test/unit/mock/contents/mapper/entries/empty-data/data-for-variant-entry.json +1 -0
- package/test/unit/mock/contents/mapper/entries/uid-mapping.json +6 -0
- package/test/unit/mock/contents/mapper/marketplace_apps/uid-mapping.json +3 -0
- package/test/unit/mock/contents/mapper/personalization/experiences/variants-uid-mapping.json +5 -0
- package/test/unit/mock/contents/mapper/taxonomies/terms/success.json +1 -0
- package/test/unit/mock/export-config.json +48 -0
- package/test/unit/mock/import-config.json +63 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
15
|
+
const utils_1 = require("../utils");
|
|
16
|
+
class Attribute extends utils_1.PersonalizationAdapter {
|
|
17
|
+
constructor(config, log = console.log) {
|
|
18
|
+
const conf = {
|
|
19
|
+
config,
|
|
20
|
+
baseURL: config.modules.personalization.baseURL[config.region.name],
|
|
21
|
+
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
|
|
22
|
+
};
|
|
23
|
+
super(Object.assign(config, conf));
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.log = log;
|
|
26
|
+
this.personalizationConfig = this.config.modules.personalization;
|
|
27
|
+
this.attributeConfig = this.personalizationConfig.attributes;
|
|
28
|
+
this.mapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName));
|
|
29
|
+
this.attrMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.attributeConfig.dirName));
|
|
30
|
+
this.attributesUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.attrMapperDirPath), 'uid-mapping.json');
|
|
31
|
+
this.attributesUidMapper = {};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The function asynchronously imports attributes from a JSON file and creates them in the system.
|
|
35
|
+
*/
|
|
36
|
+
import() {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
var _a;
|
|
39
|
+
this.log(this.config, this.$t(this.messages.IMPORT_MSG, { module: 'Attributes' }), 'info');
|
|
40
|
+
yield utils_1.fsUtil.makeDirectory(this.attrMapperDirPath);
|
|
41
|
+
const { dirName, fileName } = this.attributeConfig;
|
|
42
|
+
const attributesPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName), (0, cli_utilities_1.sanitizePath)(dirName), (0, cli_utilities_1.sanitizePath)(fileName));
|
|
43
|
+
if ((0, fs_1.existsSync)(attributesPath)) {
|
|
44
|
+
try {
|
|
45
|
+
const attributes = utils_1.fsUtil.readFile(attributesPath, true);
|
|
46
|
+
for (const attribute of attributes) {
|
|
47
|
+
const { key, name, description, uid } = attribute;
|
|
48
|
+
const attributeRes = yield this.createAttribute({ key, name, description });
|
|
49
|
+
//map old attribute uid to new attribute uid
|
|
50
|
+
//mapper file is used to check whether attribute created or not before creating audience
|
|
51
|
+
this.attributesUidMapper[uid] = (_a = attributeRes === null || attributeRes === void 0 ? void 0 : attributeRes.uid) !== null && _a !== void 0 ? _a : '';
|
|
52
|
+
}
|
|
53
|
+
utils_1.fsUtil.writeFile(this.attributesUidMapperPath, this.attributesUidMapper);
|
|
54
|
+
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Attributes' }), 'info');
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Attributes' }), 'error');
|
|
58
|
+
this.log(this.config, error, 'error');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.default = Attribute;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ImportConfig, LogType } from '../types';
|
|
2
|
+
import { PersonalizationAdapter } from '../utils';
|
|
3
|
+
export default class Audiences extends PersonalizationAdapter<ImportConfig> {
|
|
4
|
+
readonly config: ImportConfig;
|
|
5
|
+
private readonly log;
|
|
6
|
+
private mapperDirPath;
|
|
7
|
+
private audienceMapperDirPath;
|
|
8
|
+
private attributesMapperPath;
|
|
9
|
+
private audiencesUidMapperPath;
|
|
10
|
+
private audiencesUidMapper;
|
|
11
|
+
private personalizationConfig;
|
|
12
|
+
private audienceConfig;
|
|
13
|
+
attributeConfig: ImportConfig['modules']['personalization']['attributes'];
|
|
14
|
+
constructor(config: ImportConfig, log?: LogType);
|
|
15
|
+
/**
|
|
16
|
+
* The function asynchronously imports audiences from a JSON file and creates them in the system.
|
|
17
|
+
*/
|
|
18
|
+
import(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
15
|
+
const utils_1 = require("../utils");
|
|
16
|
+
class Audiences extends utils_1.PersonalizationAdapter {
|
|
17
|
+
constructor(config, log = console.log) {
|
|
18
|
+
const conf = {
|
|
19
|
+
config,
|
|
20
|
+
baseURL: config.modules.personalization.baseURL[config.region.name],
|
|
21
|
+
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
|
|
22
|
+
};
|
|
23
|
+
super(Object.assign(config, conf));
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.log = log;
|
|
26
|
+
this.personalizationConfig = this.config.modules.personalization;
|
|
27
|
+
this.audienceConfig = this.personalizationConfig.audiences;
|
|
28
|
+
this.attributeConfig = this.personalizationConfig.attributes;
|
|
29
|
+
this.mapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName));
|
|
30
|
+
this.audienceMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.audienceConfig.dirName));
|
|
31
|
+
this.audiencesUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.audienceMapperDirPath), 'uid-mapping.json');
|
|
32
|
+
this.attributesMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.attributeConfig.dirName), 'uid-mapping.json');
|
|
33
|
+
this.audiencesUidMapper = {};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The function asynchronously imports audiences from a JSON file and creates them in the system.
|
|
37
|
+
*/
|
|
38
|
+
import() {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
this.log(this.config, this.$t(this.messages.IMPORT_MSG, { module: 'Audiences' }), 'info');
|
|
42
|
+
yield utils_1.fsUtil.makeDirectory(this.audienceMapperDirPath);
|
|
43
|
+
const { dirName, fileName } = this.audienceConfig;
|
|
44
|
+
const audiencesPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName), (0, cli_utilities_1.sanitizePath)(dirName), (0, cli_utilities_1.sanitizePath)(fileName));
|
|
45
|
+
if ((0, fs_1.existsSync)(audiencesPath)) {
|
|
46
|
+
try {
|
|
47
|
+
const audiences = utils_1.fsUtil.readFile(audiencesPath, true);
|
|
48
|
+
const attributesUid = utils_1.fsUtil.readFile(this.attributesMapperPath, true) || {};
|
|
49
|
+
for (const audience of audiences) {
|
|
50
|
+
let { name, definition, description, uid } = audience;
|
|
51
|
+
//check whether reference attributes exists or not
|
|
52
|
+
if ((_a = definition.rules) === null || _a === void 0 ? void 0 : _a.length) {
|
|
53
|
+
definition.rules = (0, utils_1.lookUpAttributes)(definition.rules, attributesUid);
|
|
54
|
+
}
|
|
55
|
+
const audienceRes = yield this.createAudience({ definition, name, description });
|
|
56
|
+
//map old audience uid to new audience uid
|
|
57
|
+
//mapper file is used to check whether audience created or not before creating experience
|
|
58
|
+
this.audiencesUidMapper[uid] = (_b = audienceRes === null || audienceRes === void 0 ? void 0 : audienceRes.uid) !== null && _b !== void 0 ? _b : '';
|
|
59
|
+
}
|
|
60
|
+
utils_1.fsUtil.writeFile(this.audiencesUidMapperPath, this.audiencesUidMapper);
|
|
61
|
+
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Audiences' }), 'info');
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Audiences' }), 'error');
|
|
65
|
+
this.log(this.config, error, 'error');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.default = Audiences;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PersonalizationAdapter } from '../utils';
|
|
2
|
+
import { ImportConfig, LogType } from '../types';
|
|
3
|
+
export default class Events extends PersonalizationAdapter<ImportConfig> {
|
|
4
|
+
readonly config: ImportConfig;
|
|
5
|
+
private readonly log;
|
|
6
|
+
private mapperDirPath;
|
|
7
|
+
private eventMapperDirPath;
|
|
8
|
+
private eventsUidMapperPath;
|
|
9
|
+
private eventsUidMapper;
|
|
10
|
+
private personalizationConfig;
|
|
11
|
+
private eventsConfig;
|
|
12
|
+
constructor(config: ImportConfig, log?: LogType);
|
|
13
|
+
/**
|
|
14
|
+
* The function asynchronously imports attributes from a JSON file and creates them in the system.
|
|
15
|
+
*/
|
|
16
|
+
import(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
15
|
+
const utils_1 = require("../utils");
|
|
16
|
+
class Events extends utils_1.PersonalizationAdapter {
|
|
17
|
+
constructor(config, log = console.log) {
|
|
18
|
+
const conf = {
|
|
19
|
+
config,
|
|
20
|
+
baseURL: config.modules.personalization.baseURL[config.region.name],
|
|
21
|
+
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
|
|
22
|
+
};
|
|
23
|
+
super(Object.assign(config, conf));
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.log = log;
|
|
26
|
+
this.personalizationConfig = this.config.modules.personalization;
|
|
27
|
+
this.eventsConfig = this.personalizationConfig.events;
|
|
28
|
+
this.mapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName));
|
|
29
|
+
this.eventMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.eventsConfig.dirName));
|
|
30
|
+
this.eventsUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.eventMapperDirPath), 'uid-mapping.json');
|
|
31
|
+
this.eventsUidMapper = {};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The function asynchronously imports attributes from a JSON file and creates them in the system.
|
|
35
|
+
*/
|
|
36
|
+
import() {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
var _a;
|
|
39
|
+
this.log(this.config, this.$t(this.messages.IMPORT_MSG, { module: 'Events' }), 'info');
|
|
40
|
+
yield utils_1.fsUtil.makeDirectory(this.eventMapperDirPath);
|
|
41
|
+
const { dirName, fileName } = this.eventsConfig;
|
|
42
|
+
const eventsPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName), (0, cli_utilities_1.sanitizePath)(dirName), (0, cli_utilities_1.sanitizePath)(fileName));
|
|
43
|
+
if ((0, fs_1.existsSync)(eventsPath)) {
|
|
44
|
+
try {
|
|
45
|
+
const events = utils_1.fsUtil.readFile(eventsPath, true);
|
|
46
|
+
for (const event of events) {
|
|
47
|
+
const { key, description, uid } = event;
|
|
48
|
+
const eventsResponse = yield this.createEvents({ key, description });
|
|
49
|
+
this.eventsUidMapper[uid] = (_a = eventsResponse === null || eventsResponse === void 0 ? void 0 : eventsResponse.uid) !== null && _a !== void 0 ? _a : '';
|
|
50
|
+
}
|
|
51
|
+
utils_1.fsUtil.writeFile(this.eventsUidMapperPath, this.eventsUidMapper);
|
|
52
|
+
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Events' }), 'info');
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Events' }), 'error');
|
|
56
|
+
this.log(this.config, error, 'error');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.default = Events;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { PersonalizationAdapter } from '../utils';
|
|
2
|
+
import { ImportConfig, LogType } from '../types';
|
|
3
|
+
export default class Experiences extends PersonalizationAdapter<ImportConfig> {
|
|
4
|
+
readonly config: ImportConfig;
|
|
5
|
+
private readonly log;
|
|
6
|
+
private createdCTs;
|
|
7
|
+
private mapperDirPath;
|
|
8
|
+
private cmsVariantPath;
|
|
9
|
+
private cTsSuccessPath;
|
|
10
|
+
private failedCmsExpPath;
|
|
11
|
+
private expMapperDirPath;
|
|
12
|
+
private eventsMapperPath;
|
|
13
|
+
private experiencesPath;
|
|
14
|
+
private experiencesDirPath;
|
|
15
|
+
private audiencesMapperPath;
|
|
16
|
+
private cmsVariantGroupPath;
|
|
17
|
+
private experienceVariantsIdsPath;
|
|
18
|
+
private variantUidMapperFilePath;
|
|
19
|
+
private expThresholdTimer;
|
|
20
|
+
private maxValidateRetry;
|
|
21
|
+
private experiencesUidMapperPath;
|
|
22
|
+
private experienceCTsPath;
|
|
23
|
+
private expCheckIntervalDuration;
|
|
24
|
+
private cmsVariants;
|
|
25
|
+
private cmsVariantGroups;
|
|
26
|
+
private experiencesUidMapper;
|
|
27
|
+
private pendingVariantAndVariantGrpForExperience;
|
|
28
|
+
private personalizationConfig;
|
|
29
|
+
private audienceConfig;
|
|
30
|
+
private experienceConfig;
|
|
31
|
+
constructor(config: ImportConfig, log?: LogType);
|
|
32
|
+
/**
|
|
33
|
+
* The function asynchronously imports experiences from a JSON file and creates them in the system.
|
|
34
|
+
*/
|
|
35
|
+
import(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* function to validate if all variant groups and variants have been created using personalization background job
|
|
38
|
+
* store the variant groups data in mapper/personalization/experiences/cms-variant-groups.json and the variants data
|
|
39
|
+
* in mapper/personalization/experiences/cms-variants.json. If not, invoke validateVariantGroupAndVariantsCreated after some delay.
|
|
40
|
+
* @param retryCount Counter to track the number of times the function has been called
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
validateVariantGroupAndVariantsCreated(retryCount?: number): Promise<any>;
|
|
44
|
+
attachCTsInExperience(): Promise<PromiseSettledResult<void | import("../types").VariantGroup>[] | undefined>;
|
|
45
|
+
createVariantIdMapper(): Promise<void>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const path_1 = require("path");
|
|
27
|
+
const fs_1 = require("fs");
|
|
28
|
+
const values_1 = __importDefault(require("lodash/values"));
|
|
29
|
+
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
30
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
31
|
+
const utils_1 = require("../utils");
|
|
32
|
+
class Experiences extends utils_1.PersonalizationAdapter {
|
|
33
|
+
constructor(config, log = console.log) {
|
|
34
|
+
var _a, _b, _c, _d;
|
|
35
|
+
const conf = {
|
|
36
|
+
config,
|
|
37
|
+
baseURL: config.modules.personalization.baseURL[config.region.name],
|
|
38
|
+
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
|
|
39
|
+
cmaConfig: {
|
|
40
|
+
baseURL: config.region.cma + `/v3`,
|
|
41
|
+
headers: { authtoken: config.auth_token, api_key: config.apiKey },
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
super(Object.assign(config, conf));
|
|
45
|
+
this.config = config;
|
|
46
|
+
this.log = log;
|
|
47
|
+
this.personalizationConfig = this.config.modules.personalization;
|
|
48
|
+
this.experiencesDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.experiences.dirName));
|
|
49
|
+
this.experiencesPath = (0, path_1.join)((0, cli_utilities_1.sanitizePath)(this.experiencesDirPath), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.experiences.fileName));
|
|
50
|
+
this.experienceConfig = this.personalizationConfig.experiences;
|
|
51
|
+
this.audienceConfig = this.personalizationConfig.audiences;
|
|
52
|
+
this.mapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName));
|
|
53
|
+
this.expMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.experienceConfig.dirName));
|
|
54
|
+
this.experiencesUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.expMapperDirPath), 'uid-mapping.json');
|
|
55
|
+
this.cmsVariantGroupPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.expMapperDirPath), 'cms-variant-groups.json');
|
|
56
|
+
this.cmsVariantPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.expMapperDirPath), 'cms-variants.json');
|
|
57
|
+
this.audiencesMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.audienceConfig.dirName), 'uid-mapping.json');
|
|
58
|
+
this.eventsMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), 'events', 'uid-mapping.json');
|
|
59
|
+
this.failedCmsExpPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.expMapperDirPath), 'failed-cms-experience.json');
|
|
60
|
+
this.failedCmsExpPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.expMapperDirPath), 'failed-cms-experience.json');
|
|
61
|
+
this.experienceCTsPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.experiencesDirPath), 'experiences-content-types.json');
|
|
62
|
+
this.experienceVariantsIdsPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(this.personalizationConfig.dirName), (0, cli_utilities_1.sanitizePath)(this.experienceConfig.dirName), 'experiences-variants-ids.json');
|
|
63
|
+
this.variantUidMapperFilePath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.expMapperDirPath), 'variants-uid-mapping.json');
|
|
64
|
+
this.experiencesUidMapper = {};
|
|
65
|
+
this.cmsVariantGroups = {};
|
|
66
|
+
this.cmsVariants = {};
|
|
67
|
+
this.expThresholdTimer = (_b = (_a = this.experienceConfig) === null || _a === void 0 ? void 0 : _a.thresholdTimer) !== null && _b !== void 0 ? _b : 30000;
|
|
68
|
+
this.expCheckIntervalDuration = (_d = (_c = this.experienceConfig) === null || _c === void 0 ? void 0 : _c.checkIntervalDuration) !== null && _d !== void 0 ? _d : 5000;
|
|
69
|
+
this.maxValidateRetry = Math.round(this.expThresholdTimer / this.expCheckIntervalDuration);
|
|
70
|
+
this.pendingVariantAndVariantGrpForExperience = [];
|
|
71
|
+
this.cTsSuccessPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', 'content_types', 'success.json');
|
|
72
|
+
this.createdCTs = [];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The function asynchronously imports experiences from a JSON file and creates them in the system.
|
|
76
|
+
*/
|
|
77
|
+
import() {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
var _a;
|
|
80
|
+
this.log(this.config, this.$t(this.messages.IMPORT_MSG, { module: 'Experiences' }), 'info');
|
|
81
|
+
yield utils_1.fsUtil.makeDirectory(this.expMapperDirPath);
|
|
82
|
+
if ((0, fs_1.existsSync)(this.experiencesPath)) {
|
|
83
|
+
try {
|
|
84
|
+
const experiences = utils_1.fsUtil.readFile(this.experiencesPath, true);
|
|
85
|
+
const audiencesUid = utils_1.fsUtil.readFile(this.audiencesMapperPath, true) || {};
|
|
86
|
+
const eventsUid = utils_1.fsUtil.readFile(this.eventsMapperPath, true) || {};
|
|
87
|
+
for (const experience of experiences) {
|
|
88
|
+
const { uid } = experience, restExperienceData = __rest(experience, ["uid"]);
|
|
89
|
+
//check whether reference audience exists or not that referenced in variations having __type equal to AudienceBasedVariation & targeting
|
|
90
|
+
let experienceReqObj = (0, utils_1.lookUpAudiences)(restExperienceData, audiencesUid);
|
|
91
|
+
//check whether events exists or not that referenced in metrics
|
|
92
|
+
experienceReqObj = (0, utils_1.lookUpEvents)(experienceReqObj, eventsUid);
|
|
93
|
+
const expRes = yield this.createExperience(experienceReqObj);
|
|
94
|
+
//map old experience uid to new experience uid
|
|
95
|
+
this.experiencesUidMapper[uid] = (_a = expRes === null || expRes === void 0 ? void 0 : expRes.uid) !== null && _a !== void 0 ? _a : '';
|
|
96
|
+
}
|
|
97
|
+
utils_1.fsUtil.writeFile(this.experiencesUidMapperPath, this.experiencesUidMapper);
|
|
98
|
+
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Experiences' }), 'info');
|
|
99
|
+
this.log(this.config, this.messages.VALIDATE_VARIANT_AND_VARIANT_GRP, 'info');
|
|
100
|
+
this.pendingVariantAndVariantGrpForExperience = (0, values_1.default)((0, cloneDeep_1.default)(this.experiencesUidMapper));
|
|
101
|
+
const jobRes = yield this.validateVariantGroupAndVariantsCreated();
|
|
102
|
+
utils_1.fsUtil.writeFile(this.cmsVariantPath, this.cmsVariants);
|
|
103
|
+
utils_1.fsUtil.writeFile(this.cmsVariantGroupPath, this.cmsVariantGroups);
|
|
104
|
+
if (jobRes)
|
|
105
|
+
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Variant & Variant groups' }), 'info');
|
|
106
|
+
if (this.personalizationConfig.importData) {
|
|
107
|
+
this.log(this.config, this.messages.UPDATING_CT_IN_EXP, 'info');
|
|
108
|
+
yield this.attachCTsInExperience();
|
|
109
|
+
}
|
|
110
|
+
yield this.createVariantIdMapper();
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Experiences' }), 'error');
|
|
114
|
+
this.log(this.config, error, 'error');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* function to validate if all variant groups and variants have been created using personalization background job
|
|
121
|
+
* store the variant groups data in mapper/personalization/experiences/cms-variant-groups.json and the variants data
|
|
122
|
+
* in mapper/personalization/experiences/cms-variants.json. If not, invoke validateVariantGroupAndVariantsCreated after some delay.
|
|
123
|
+
* @param retryCount Counter to track the number of times the function has been called
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
validateVariantGroupAndVariantsCreated() {
|
|
127
|
+
return __awaiter(this, arguments, void 0, function* (retryCount = 0) {
|
|
128
|
+
var _a;
|
|
129
|
+
try {
|
|
130
|
+
const promises = this.pendingVariantAndVariantGrpForExperience.map((expUid) => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
var _a, _b, _c, _d;
|
|
132
|
+
const expRes = yield this.getExperience(expUid);
|
|
133
|
+
if (expRes === null || expRes === void 0 ? void 0 : expRes._cms) {
|
|
134
|
+
this.cmsVariants[expUid] = (_b = (_a = expRes._cms) === null || _a === void 0 ? void 0 : _a.variants) !== null && _b !== void 0 ? _b : {};
|
|
135
|
+
this.cmsVariantGroups[expUid] = (_d = (_c = expRes._cms) === null || _c === void 0 ? void 0 : _c.variantGroup) !== null && _d !== void 0 ? _d : {};
|
|
136
|
+
return expUid; // Return the expUid for filtering later
|
|
137
|
+
}
|
|
138
|
+
}));
|
|
139
|
+
yield Promise.all(promises);
|
|
140
|
+
retryCount++;
|
|
141
|
+
if ((_a = this.pendingVariantAndVariantGrpForExperience) === null || _a === void 0 ? void 0 : _a.length) {
|
|
142
|
+
if (retryCount !== this.maxValidateRetry) {
|
|
143
|
+
yield this.delay(this.expCheckIntervalDuration);
|
|
144
|
+
// Filter out the processed elements
|
|
145
|
+
this.pendingVariantAndVariantGrpForExperience = this.pendingVariantAndVariantGrpForExperience.filter((uid) => !this.cmsVariants[uid]);
|
|
146
|
+
return this.validateVariantGroupAndVariantsCreated(retryCount);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
this.log(this.config, this.messages.PERSONALIZATION_JOB_FAILURE, 'error');
|
|
150
|
+
utils_1.fsUtil.writeFile(this.failedCmsExpPath, this.pendingVariantAndVariantGrpForExperience);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
attachCTsInExperience() {
|
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
try {
|
|
166
|
+
// Read the created content types from the file
|
|
167
|
+
this.createdCTs = utils_1.fsUtil.readFile(this.cTsSuccessPath, true);
|
|
168
|
+
if (!this.createdCTs) {
|
|
169
|
+
this.log(this.config, 'No Content types created, skipping following process', 'error');
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const experienceCTsMap = utils_1.fsUtil.readFile(this.experienceCTsPath, true);
|
|
173
|
+
return yield Promise.allSettled(Object.entries(this.experiencesUidMapper).map((_a) => __awaiter(this, [_a], void 0, function* ([oldExpUid, newExpUid]) {
|
|
174
|
+
var _b;
|
|
175
|
+
if ((_b = experienceCTsMap[oldExpUid]) === null || _b === void 0 ? void 0 : _b.length) {
|
|
176
|
+
// Filter content types that were created
|
|
177
|
+
const updatedContentTypes = experienceCTsMap[oldExpUid].filter((ct) => this.createdCTs.includes(ct === null || ct === void 0 ? void 0 : ct.uid));
|
|
178
|
+
if (updatedContentTypes === null || updatedContentTypes === void 0 ? void 0 : updatedContentTypes.length) {
|
|
179
|
+
const { variant_groups: [variantGroup] = [] } = (yield this.getVariantGroup({ experienceUid: newExpUid })) || {};
|
|
180
|
+
variantGroup.content_types = updatedContentTypes;
|
|
181
|
+
// Update content types detail in the new experience asynchronously
|
|
182
|
+
return yield this.updateVariantGroup(variantGroup);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
})));
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
this.log(this.config, `Error while attaching content type with experience`, 'error');
|
|
189
|
+
this.log(this.config, error, 'error');
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
createVariantIdMapper() {
|
|
194
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
195
|
+
var _a;
|
|
196
|
+
try {
|
|
197
|
+
const experienceVariantIds = utils_1.fsUtil.readFile(this.experienceVariantsIdsPath, true) || [];
|
|
198
|
+
const variantUIDMapper = {};
|
|
199
|
+
for (let experienceVariantId of experienceVariantIds) {
|
|
200
|
+
const [experienceId, variantShortId, oldVariantId] = experienceVariantId.split('-');
|
|
201
|
+
const latestVariantId = (_a = this.cmsVariants[this.experiencesUidMapper[experienceId]]) === null || _a === void 0 ? void 0 : _a[variantShortId];
|
|
202
|
+
if (latestVariantId) {
|
|
203
|
+
variantUIDMapper[oldVariantId] = latestVariantId;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
utils_1.fsUtil.writeFile(this.variantUidMapperFilePath, variantUIDMapper);
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.default = Experiences;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Events from './events';
|
|
2
|
+
import Project from './project';
|
|
3
|
+
import Attribute from './attribute';
|
|
4
|
+
import Audiences from './audiences';
|
|
5
|
+
import Experiences from './experiences';
|
|
6
|
+
import VariantEntries from './variant-entries';
|
|
7
|
+
export declare const Import: {
|
|
8
|
+
Events: typeof Events;
|
|
9
|
+
Project: typeof Project;
|
|
10
|
+
Attribute: typeof Attribute;
|
|
11
|
+
Audiences: typeof Audiences;
|
|
12
|
+
Experiences: typeof Experiences;
|
|
13
|
+
VariantEntries: typeof VariantEntries;
|
|
14
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Import = void 0;
|
|
7
|
+
const events_1 = __importDefault(require("./events"));
|
|
8
|
+
const project_1 = __importDefault(require("./project"));
|
|
9
|
+
const attribute_1 = __importDefault(require("./attribute"));
|
|
10
|
+
const audiences_1 = __importDefault(require("./audiences"));
|
|
11
|
+
const experiences_1 = __importDefault(require("./experiences"));
|
|
12
|
+
const variant_entries_1 = __importDefault(require("./variant-entries"));
|
|
13
|
+
// NOTE Acting as namespace to avoid the same class name conflicts in other modules
|
|
14
|
+
exports.Import = {
|
|
15
|
+
Events: events_1.default,
|
|
16
|
+
Project: project_1.default,
|
|
17
|
+
Attribute: attribute_1.default,
|
|
18
|
+
Audiences: audiences_1.default,
|
|
19
|
+
Experiences: experiences_1.default,
|
|
20
|
+
VariantEntries: variant_entries_1.default,
|
|
21
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PersonalizationAdapter } from '../utils';
|
|
2
|
+
import { ImportConfig, LogType } from '../types';
|
|
3
|
+
export default class Project extends PersonalizationAdapter<ImportConfig> {
|
|
4
|
+
readonly config: ImportConfig;
|
|
5
|
+
private readonly log;
|
|
6
|
+
private projectMapperFolderPath;
|
|
7
|
+
constructor(config: ImportConfig, log?: LogType);
|
|
8
|
+
/**
|
|
9
|
+
* The function asynchronously imports projects data from a file and creates projects based on the
|
|
10
|
+
* data.
|
|
11
|
+
*/
|
|
12
|
+
import(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
const fs_1 = require("fs");
|
|
14
|
+
const cli_utilities_1 = require("@contentstack/cli-utilities");
|
|
15
|
+
const utils_1 = require("../utils");
|
|
16
|
+
class Project extends utils_1.PersonalizationAdapter {
|
|
17
|
+
constructor(config, log = console.log) {
|
|
18
|
+
const conf = {
|
|
19
|
+
config,
|
|
20
|
+
baseURL: config.modules.personalization.baseURL[config.region.name],
|
|
21
|
+
headers: { organization_uid: config.org_uid, authtoken: config.auth_token },
|
|
22
|
+
};
|
|
23
|
+
super(Object.assign(config, conf));
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.log = log;
|
|
26
|
+
this.projectMapperFolderPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.config.modules.personalization.dirName), 'projects');
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The function asynchronously imports projects data from a file and creates projects based on the
|
|
30
|
+
* data.
|
|
31
|
+
*/
|
|
32
|
+
import() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const personalization = this.config.modules.personalization;
|
|
35
|
+
const { dirName, fileName } = personalization.projects;
|
|
36
|
+
const projectPath = (0, path_1.join)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(personalization.dirName), (0, cli_utilities_1.sanitizePath)(dirName), (0, cli_utilities_1.sanitizePath)(fileName));
|
|
37
|
+
if ((0, fs_1.existsSync)(projectPath)) {
|
|
38
|
+
const projects = JSON.parse((0, fs_1.readFileSync)(projectPath, 'utf8'));
|
|
39
|
+
if (!projects || projects.length < 1) {
|
|
40
|
+
this.config.modules.personalization.importData = false; // Stop personalization import if stack not connected to any project
|
|
41
|
+
this.log(this.config, 'No project found!', 'info');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
for (const project of projects) {
|
|
45
|
+
const createProject = (newName) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
return yield this.createProject({
|
|
47
|
+
name: newName || project.name,
|
|
48
|
+
description: project.description,
|
|
49
|
+
connectedStackApiKey: this.config.apiKey,
|
|
50
|
+
}).catch((error) => __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
if (error === 'personalization.PROJECTS.DUPLICATE_NAME') {
|
|
52
|
+
const projectName = yield (0, utils_1.askProjectName)('Copy Of ' + (newName || project.name));
|
|
53
|
+
return yield createProject(projectName);
|
|
54
|
+
}
|
|
55
|
+
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Projects' }), 'error');
|
|
56
|
+
throw error;
|
|
57
|
+
}));
|
|
58
|
+
});
|
|
59
|
+
const projectRes = yield createProject(this.config.personalizeProjectName);
|
|
60
|
+
this.config.modules.personalization.project_id = projectRes.uid;
|
|
61
|
+
this.config.modules.personalization.importData = true;
|
|
62
|
+
yield utils_1.fsUtil.makeDirectory(this.projectMapperFolderPath);
|
|
63
|
+
utils_1.fsUtil.writeFile((0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.projectMapperFolderPath), 'projects.json'), projectRes);
|
|
64
|
+
this.log(this.config, `Project Created Successfully: ${projectRes.uid}`, 'info');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.config.modules.personalization.importData = false; // Stop personalization import if stack not connected to any project
|
|
69
|
+
this.log(this.config, 'No project found!', 'info');
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.default = Project;
|