@contentstack/cli-cm-import 1.5.11 → 1.6.0
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/README.md +6 -14
- package/bin/dev +17 -0
- package/bin/dev.cmd +3 -0
- package/bin/run +6 -0
- package/bin/run.cmd +3 -0
- package/lib/commands/cm/stacks/import.d.ts +10 -0
- package/lib/commands/cm/stacks/import.js +111 -0
- package/lib/config/index.d.ts +3 -0
- package/lib/config/index.js +372 -0
- package/lib/import/index.d.ts +1 -0
- package/lib/import/index.js +8 -0
- package/lib/import/module-importer.d.ts +13 -0
- package/lib/import/module-importer.js +70 -0
- package/lib/import/modules/assets.d.ts +63 -0
- package/lib/import/modules/assets.js +265 -0
- package/lib/import/modules/base-class.d.ts +69 -0
- package/lib/import/modules/base-class.js +165 -0
- package/lib/import/modules/index.d.ts +2 -0
- package/lib/import/modules/index.js +19 -0
- package/lib/import/modules/locales.d.ts +31 -0
- package/lib/import/modules/locales.js +152 -0
- package/lib/import/modules-js/assets.d.ts +33 -0
- package/lib/import/modules-js/assets.js +415 -0
- package/lib/import/modules-js/content-types.d.ts +33 -0
- package/lib/import/modules-js/content-types.js +176 -0
- package/lib/import/modules-js/custom-roles.d.ts +15 -0
- package/lib/import/modules-js/custom-roles.js +141 -0
- package/lib/import/modules-js/entries.d.ts +54 -0
- package/lib/import/modules-js/entries.js +1260 -0
- package/lib/import/modules-js/environments.d.ts +13 -0
- package/lib/import/modules-js/environments.js +85 -0
- package/lib/import/modules-js/extensions.d.ts +17 -0
- package/lib/import/modules-js/extensions.js +86 -0
- package/lib/import/modules-js/global-fields.d.ts +13 -0
- package/lib/import/modules-js/global-fields.js +109 -0
- package/lib/import/modules-js/index.d.ts +1 -0
- package/lib/import/modules-js/index.js +33 -0
- package/lib/import/modules-js/labels.d.ts +20 -0
- package/lib/import/modules-js/labels.js +148 -0
- package/lib/import/modules-js/locales.d.ts +24 -0
- package/lib/import/modules-js/locales.js +196 -0
- package/lib/import/modules-js/marketplace-apps.d.ts +60 -0
- package/lib/import/modules-js/marketplace-apps.js +409 -0
- package/lib/import/modules-js/webhooks.d.ts +17 -0
- package/lib/import/modules-js/webhooks.js +85 -0
- package/lib/import/modules-js/workflows.d.ts +18 -0
- package/lib/import/modules-js/workflows.js +132 -0
- package/lib/types/default-config.d.ts +130 -0
- package/lib/types/default-config.js +2 -0
- package/lib/types/import-config.d.ts +51 -0
- package/lib/types/import-config.js +2 -0
- package/lib/types/index.d.ts +30 -0
- package/lib/types/index.js +2 -0
- package/lib/utils/asset-helper.d.ts +4 -0
- package/lib/utils/asset-helper.js +387 -0
- package/lib/utils/backup-handler.d.ts +2 -0
- package/lib/utils/backup-handler.js +31 -0
- package/lib/utils/common-helper.d.ts +20 -0
- package/lib/utils/common-helper.js +244 -0
- package/lib/utils/content-type-helper.d.ts +49 -0
- package/lib/utils/content-type-helper.js +143 -0
- package/lib/utils/entries-helper.d.ts +4 -0
- package/lib/utils/entries-helper.js +252 -0
- package/lib/utils/extension-helper.d.ts +10 -0
- package/lib/utils/extension-helper.js +72 -0
- package/lib/utils/file-helper.d.ts +14 -0
- package/lib/utils/file-helper.js +140 -0
- package/lib/utils/import-config-handler.d.ts +3 -0
- package/lib/utils/import-config-handler.js +73 -0
- package/lib/utils/index.d.ts +12 -0
- package/lib/utils/index.js +29 -0
- package/lib/utils/interactive.d.ts +2 -0
- package/lib/utils/interactive.js +23 -0
- package/lib/utils/logger.d.ts +8 -0
- package/lib/utils/logger.js +154 -0
- package/lib/utils/login-handler.d.ts +8 -0
- package/lib/utils/login-handler.js +53 -0
- package/lib/utils/marketplace-app-helper.d.ts +4 -0
- package/lib/utils/marketplace-app-helper.js +31 -0
- package/messages/index.json +1 -7
- package/oclif.manifest.json +2 -2
- package/package.json +47 -21
- package/src/app.js +0 -217
- package/src/commands/cm/stacks/import.js +0 -161
- package/src/config/default.js +0 -352
- package/src/lib/import/assets.js +0 -495
- package/src/lib/import/content-types.js +0 -201
- package/src/lib/import/custom-roles.js +0 -169
- package/src/lib/import/entries.js +0 -1495
- package/src/lib/import/environments.js +0 -106
- package/src/lib/import/extensions.js +0 -108
- package/src/lib/import/global-fields.js +0 -135
- package/src/lib/import/labels.js +0 -175
- package/src/lib/import/locales.js +0 -216
- package/src/lib/import/marketplace-apps.js +0 -542
- package/src/lib/import/webhooks.js +0 -113
- package/src/lib/import/workflows.js +0 -166
- package/src/lib/util/extensionsUidReplace.js +0 -67
- package/src/lib/util/fs.js +0 -124
- package/src/lib/util/import-flags.js +0 -187
- package/src/lib/util/index.js +0 -222
- package/src/lib/util/log.js +0 -144
- package/src/lib/util/login.js +0 -58
- package/src/lib/util/lookupReplaceAssets.js +0 -366
- package/src/lib/util/lookupReplaceEntries.js +0 -250
- package/src/lib/util/marketplace-app-helper.js +0 -31
- package/src/lib/util/removeReferenceFields.js +0 -59
- package/src/lib/util/schemaTemplate.js +0 -38
- package/src/lib/util/supress-mandatory-fields.js +0 -34
- package/src/lib/util/upload.js +0 -56
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Contentstack Import
|
|
3
|
-
* Copyright (c) 2019 Contentstack LLC
|
|
4
|
-
* MIT Licensed
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const chalk = require('chalk');
|
|
10
|
-
const mkdirp = require('mkdirp');
|
|
11
|
-
const Promise = require('bluebird');
|
|
12
|
-
const { isEmpty, merge } = require('lodash');
|
|
13
|
-
|
|
14
|
-
const helper = require('../util/fs');
|
|
15
|
-
let { addlogs } = require('../util/log');
|
|
16
|
-
const { formatError } = require('../util');
|
|
17
|
-
const config = require('../../config/default');
|
|
18
|
-
|
|
19
|
-
module.exports = class ImportEnvironments {
|
|
20
|
-
fails = [];
|
|
21
|
-
success = [];
|
|
22
|
-
envUidMapper = {};
|
|
23
|
-
fetchConcurrency = config.modules.environments.concurrency || config.fetchConcurrency || 2;
|
|
24
|
-
|
|
25
|
-
constructor(importConfig, stackAPIClient) {
|
|
26
|
-
this.config = merge(config, importConfig);
|
|
27
|
-
this.stackAPIClient = stackAPIClient;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
start() {
|
|
31
|
-
addlogs(this.config, 'Migrating environment', 'success');
|
|
32
|
-
|
|
33
|
-
const self = this;
|
|
34
|
-
let environmentConfig = config.modules.environments;
|
|
35
|
-
let environmentsFolderPath = path.resolve(this.config.data, environmentConfig.dirName);
|
|
36
|
-
let envMapperPath = path.resolve(this.config.data, 'mapper', 'environments');
|
|
37
|
-
let envUidMapperPath = path.resolve(this.config.data, 'mapper', 'environments', 'uid-mapping.json');
|
|
38
|
-
let envSuccessPath = path.resolve(this.config.data, 'environments', 'success.json');
|
|
39
|
-
let envFailsPath = path.resolve(this.config.data, 'environments', 'fails.json');
|
|
40
|
-
self.environments = helper.readFileSync(path.resolve(environmentsFolderPath, environmentConfig.fileName));
|
|
41
|
-
|
|
42
|
-
if (fs.existsSync(envUidMapperPath)) {
|
|
43
|
-
self.envUidMapper = helper.readFileSync(envUidMapperPath);
|
|
44
|
-
self.envUidMapper = self.envUidMapper || {};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
mkdirp.sync(envMapperPath);
|
|
48
|
-
return new Promise(function (resolve, reject) {
|
|
49
|
-
if (self.environments === undefined || isEmpty(self.environments)) {
|
|
50
|
-
addlogs(self.config, chalk.yellow('No Environment Found'), 'success');
|
|
51
|
-
return resolve({ empty: true });
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let envUids = Object.keys(self.environments);
|
|
55
|
-
return Promise.map(
|
|
56
|
-
envUids,
|
|
57
|
-
function (envUid) {
|
|
58
|
-
let env = self.environments[envUid];
|
|
59
|
-
if (!self.envUidMapper.hasOwnProperty(envUid)) {
|
|
60
|
-
let requestOption = { environment: env };
|
|
61
|
-
|
|
62
|
-
return self.stackAPIClient
|
|
63
|
-
.environment()
|
|
64
|
-
.create(requestOption)
|
|
65
|
-
.then((environment) => {
|
|
66
|
-
self.success.push(environment.items);
|
|
67
|
-
self.envUidMapper[envUid] = environment.uid;
|
|
68
|
-
helper.writeFileSync(envUidMapperPath, self.envUidMapper);
|
|
69
|
-
})
|
|
70
|
-
.catch(function (err) {
|
|
71
|
-
let error = JSON.parse(err.message);
|
|
72
|
-
|
|
73
|
-
if (error.errors.name) {
|
|
74
|
-
addlogs(self.config, `Environment '${env.name}' already exists`, 'error');
|
|
75
|
-
} else {
|
|
76
|
-
addlogs(
|
|
77
|
-
config,
|
|
78
|
-
`Environment '${env.name}' failed to be import\n ${JSON.stringify(error.errors)}`,
|
|
79
|
-
'error',
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
} else {
|
|
84
|
-
// the environment has already been created
|
|
85
|
-
addlogs(
|
|
86
|
-
config,
|
|
87
|
-
`The environment ${env.name} already exists. Skipping it to avoid duplicates!`,
|
|
88
|
-
'success',
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
{ concurrency: self.fetchConcurrency },
|
|
93
|
-
)
|
|
94
|
-
.then(function () {
|
|
95
|
-
helper.writeFileSync(envSuccessPath, self.success);
|
|
96
|
-
addlogs(self.config, chalk.green('Environments have been imported successfully!'), 'success');
|
|
97
|
-
resolve();
|
|
98
|
-
})
|
|
99
|
-
.catch(function (error) {
|
|
100
|
-
helper.writeFileSync(envFailsPath, self.fails);
|
|
101
|
-
addlogs(self.config, `Failed to import environment, ${formatError(error)}`, 'error');
|
|
102
|
-
reject(error);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
};
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Contentstack Import
|
|
3
|
-
* Copyright (c) 2019 Contentstack LLC
|
|
4
|
-
* MIT Licensed
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const mkdirp = require('mkdirp');
|
|
8
|
-
const fs = require('fs');
|
|
9
|
-
const path = require('path');
|
|
10
|
-
const Promise = require('bluebird');
|
|
11
|
-
const chalk = require('chalk');
|
|
12
|
-
const { isEmpty, merge } = require('lodash');
|
|
13
|
-
|
|
14
|
-
const util = require('../util');
|
|
15
|
-
const helper = require('../util/fs');
|
|
16
|
-
const { addlogs } = require('../util/log');
|
|
17
|
-
|
|
18
|
-
let config = util.getConfig();
|
|
19
|
-
|
|
20
|
-
module.exports = class ImportExtensions {
|
|
21
|
-
fails = [];
|
|
22
|
-
success = [];
|
|
23
|
-
extUidMapper = {};
|
|
24
|
-
extensionsConfig = config.modules.extensions;
|
|
25
|
-
reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
|
|
26
|
-
|
|
27
|
-
constructor(importConfig, stackAPIClient) {
|
|
28
|
-
this.config = merge(config, importConfig);
|
|
29
|
-
this.stackAPIClient = stackAPIClient;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
start() {
|
|
33
|
-
addlogs(this.config, chalk.white('Migrating extensions'), 'success');
|
|
34
|
-
|
|
35
|
-
const self = this;
|
|
36
|
-
let extensionsFolderPath = path.resolve(this.config.data, this.extensionsConfig.dirName);
|
|
37
|
-
let extMapperPath = path.resolve(this.config.data, 'mapper', 'extensions');
|
|
38
|
-
let extUidMapperPath = path.resolve(this.config.data, 'mapper/extensions', 'uid-mapping.json');
|
|
39
|
-
let extSuccessPath = path.resolve(this.config.data, 'extensions', 'success.json');
|
|
40
|
-
let extFailsPath = path.resolve(this.config.data, 'extensions', 'fails.json');
|
|
41
|
-
this.extensions = helper.readFileSync(path.resolve(extensionsFolderPath, this.extensionsConfig.fileName));
|
|
42
|
-
if (fs.existsSync(extUidMapperPath)) {
|
|
43
|
-
self.extUidMapper = helper.readFileSync(extUidMapperPath);
|
|
44
|
-
self.extUidMapper = self.extUidMapper || {};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
mkdirp.sync(extMapperPath);
|
|
48
|
-
|
|
49
|
-
return new Promise(function (resolve, reject) {
|
|
50
|
-
if (self.extensions === undefined || isEmpty(self.extensions)) {
|
|
51
|
-
addlogs(self.config, chalk.white('No Extensions Found'), 'success');
|
|
52
|
-
return resolve({ empty: true });
|
|
53
|
-
}
|
|
54
|
-
let extUids = Object.keys(self.extensions);
|
|
55
|
-
return Promise.map(
|
|
56
|
-
extUids,
|
|
57
|
-
function (extUid) {
|
|
58
|
-
let ext = self.extensions[extUid];
|
|
59
|
-
if (!self.extUidMapper.hasOwnProperty(extUid)) {
|
|
60
|
-
return self.stackAPIClient
|
|
61
|
-
.extension()
|
|
62
|
-
.create({ extension: ext })
|
|
63
|
-
.then((response) => {
|
|
64
|
-
self.success.push(response);
|
|
65
|
-
self.extUidMapper[extUid] = response.uid;
|
|
66
|
-
helper.writeFileSync(extUidMapperPath, self.extUidMapper);
|
|
67
|
-
})
|
|
68
|
-
.catch(function (err) {
|
|
69
|
-
let error = JSON.parse(err.message);
|
|
70
|
-
self.fails.push(ext);
|
|
71
|
-
if (error.errors.title) {
|
|
72
|
-
addlogs(self.config, `Extension '${ext.title}' already exists`, 'error');
|
|
73
|
-
} else {
|
|
74
|
-
addlogs(
|
|
75
|
-
config,
|
|
76
|
-
chalk.white(`Extension '${ext.title}' failed to be import\n ${JSON.stringify(error.errors)}`),
|
|
77
|
-
'error',
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
// the extensions has already been created
|
|
83
|
-
addlogs(
|
|
84
|
-
config,
|
|
85
|
-
chalk.white("The extension: '" + ext.title + "' already exists. Skipping it to avoid duplicates!"),
|
|
86
|
-
'success',
|
|
87
|
-
);
|
|
88
|
-
// import 2 extensions at a time
|
|
89
|
-
},
|
|
90
|
-
{
|
|
91
|
-
concurrency: self.reqConcurrency,
|
|
92
|
-
},
|
|
93
|
-
)
|
|
94
|
-
.then(function () {
|
|
95
|
-
// extensions have imported successfully
|
|
96
|
-
helper.writeFileSync(extSuccessPath, self.success);
|
|
97
|
-
addlogs(self.config, chalk.green('Extensions have been imported successfully!'), 'success');
|
|
98
|
-
resolve();
|
|
99
|
-
})
|
|
100
|
-
.catch(function (error) {
|
|
101
|
-
// error while importing extensions
|
|
102
|
-
helper.writeFileSync(extFailsPath, self.fails);
|
|
103
|
-
addlogs(self.config, `Extension import failed ${util.formatError(error)}`, 'error');
|
|
104
|
-
reject(error);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
};
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Contentstack Import
|
|
3
|
-
* Copyright (c) 2019 Contentstack LLC
|
|
4
|
-
* MIT Licensed
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
let fs = require('fs');
|
|
8
|
-
let path = require('path');
|
|
9
|
-
let chalk = require('chalk');
|
|
10
|
-
let mkdirp = require('mkdirp');
|
|
11
|
-
let Promise = require('bluebird');
|
|
12
|
-
const { isEmpty, merge } = require('lodash');
|
|
13
|
-
|
|
14
|
-
let helper = require('../util/fs');
|
|
15
|
-
let { addlogs } = require('../util/log');
|
|
16
|
-
const { formatError } = require('../util');
|
|
17
|
-
let config = require('../../config/default');
|
|
18
|
-
let extension_supress = require('../util/extensionsUidReplace');
|
|
19
|
-
let removeReferenceFields = require('../util/removeReferenceFields');
|
|
20
|
-
|
|
21
|
-
global._globalField_pending = [];
|
|
22
|
-
|
|
23
|
-
module.exports = class ImportGlobalFields {
|
|
24
|
-
fails = [];
|
|
25
|
-
success = [];
|
|
26
|
-
snipUidMapper = {};
|
|
27
|
-
installedExtensions = [];
|
|
28
|
-
reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
|
|
29
|
-
|
|
30
|
-
constructor(importConfig, stackAPIClient) {
|
|
31
|
-
this.config = merge(config, importConfig);
|
|
32
|
-
this.stackAPIClient = stackAPIClient;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async start() {
|
|
36
|
-
addlogs(this.config, chalk.white('Migrating global-fields'), 'success');
|
|
37
|
-
|
|
38
|
-
let self = this;
|
|
39
|
-
let globalfieldsConfig = config.modules.globalfields;
|
|
40
|
-
let globalfieldsFolderPath = path.resolve(this.config.data, globalfieldsConfig.dirName);
|
|
41
|
-
let globalfieldsMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields');
|
|
42
|
-
let globalfieldsUidMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'uid-mapping.json');
|
|
43
|
-
let globalfieldsSuccessPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'success.json');
|
|
44
|
-
let globalFieldsPending = path.resolve(this.config.data, 'mapper', 'global_fields', 'pending_global_fields.js');
|
|
45
|
-
let globalfieldsFailsPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'fails.json');
|
|
46
|
-
self.globalfields = helper.readFileSync(path.resolve(globalfieldsFolderPath, globalfieldsConfig.fileName));
|
|
47
|
-
const appMapperPath = path.join(this.config.data, 'mapper', 'marketplace_apps', 'uid-mapping.json');
|
|
48
|
-
this.installedExtensions = ((await helper.readFileSync(appMapperPath)) || { extension_uid: {} }).extension_uid;
|
|
49
|
-
|
|
50
|
-
if (fs.existsSync(globalfieldsUidMapperPath)) {
|
|
51
|
-
self.snipUidMapper = helper.readFileSync(globalfieldsUidMapperPath);
|
|
52
|
-
self.snipUidMapper = self.snipUidMapper || {};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (!fs.existsSync(globalfieldsMapperPath)) {
|
|
56
|
-
mkdirp.sync(globalfieldsMapperPath);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return new Promise(function (resolve, reject) {
|
|
60
|
-
if (self.globalfields === undefined || isEmpty(self.globalfields)) {
|
|
61
|
-
addlogs(self.config, chalk.white('No globalfields Found'), 'success');
|
|
62
|
-
helper.writeFileSync(globalFieldsPending, _globalField_pending);
|
|
63
|
-
return resolve({ empty: true });
|
|
64
|
-
}
|
|
65
|
-
let snipUids = Object.keys(self.globalfields);
|
|
66
|
-
return Promise.map(
|
|
67
|
-
snipUids,
|
|
68
|
-
async function (snipUid) {
|
|
69
|
-
let flag = { supressed: false };
|
|
70
|
-
let snip = self.globalfields[snipUid];
|
|
71
|
-
extension_supress(snip.schema, self.config.preserveStackVersion, self.installedExtensions);
|
|
72
|
-
await removeReferenceFields(snip.schema, flag, self.stackAPIClient);
|
|
73
|
-
|
|
74
|
-
if (flag.supressed) {
|
|
75
|
-
// eslint-disable-next-line no-undef
|
|
76
|
-
_globalField_pending.push(snip.uid);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!self.snipUidMapper.hasOwnProperty(snipUid)) {
|
|
80
|
-
let requestOption = { global_field: snip };
|
|
81
|
-
return self.stackAPIClient
|
|
82
|
-
.globalField()
|
|
83
|
-
.create(requestOption)
|
|
84
|
-
.then((globalField) => {
|
|
85
|
-
self.success.push(globalField.items);
|
|
86
|
-
let global_field_uid = globalField.uid;
|
|
87
|
-
self.snipUidMapper[snipUid] = globalField.items;
|
|
88
|
-
helper.writeFileSync(globalfieldsUidMapperPath, self.snipUidMapper);
|
|
89
|
-
addlogs(
|
|
90
|
-
self.config,
|
|
91
|
-
chalk.green('Global field ' + global_field_uid + ' created successfully'),
|
|
92
|
-
'success',
|
|
93
|
-
);
|
|
94
|
-
})
|
|
95
|
-
.catch(function (err) {
|
|
96
|
-
let error = JSON.parse(err.message);
|
|
97
|
-
if (error.errors.title) {
|
|
98
|
-
// eslint-disable-next-line no-undef
|
|
99
|
-
addlogs(self.config, `Globalfield '${snip.uid} already exists'`, 'error');
|
|
100
|
-
} else {
|
|
101
|
-
addlogs(self.config, `Globalfield failed to import ${formatError(error)}`, 'error');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
self.fails.push(snip);
|
|
105
|
-
});
|
|
106
|
-
} else {
|
|
107
|
-
// globalfields has already been created
|
|
108
|
-
addlogs(
|
|
109
|
-
self.config,
|
|
110
|
-
chalk.white('The globalfields already exists. Skipping it to avoid duplicates!'),
|
|
111
|
-
'success',
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
// import 2 globalfields at a time
|
|
115
|
-
},
|
|
116
|
-
{ concurrency: self.reqConcurrency },
|
|
117
|
-
)
|
|
118
|
-
.then(function () {
|
|
119
|
-
// globalfields have imported successfully
|
|
120
|
-
helper.writeFileSync(globalfieldsSuccessPath, self.success);
|
|
121
|
-
helper.writeFileSync(globalFieldsPending, _globalField_pending);
|
|
122
|
-
addlogs(self.config, chalk.green('Globalfields have been imported successfully!'), 'success');
|
|
123
|
-
return resolve();
|
|
124
|
-
})
|
|
125
|
-
.catch(function (err) {
|
|
126
|
-
let error = JSON.parse(err);
|
|
127
|
-
// error while importing globalfields
|
|
128
|
-
addlogs(self.config, err, 'error');
|
|
129
|
-
helper.writeFileSync(globalfieldsFailsPath, self.fails);
|
|
130
|
-
addlogs(self.config, `Globalfields import failed. ${formatError(err)}`, 'error');
|
|
131
|
-
return reject(error);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
};
|
package/src/lib/import/labels.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Contentstack Import
|
|
3
|
-
* Copyright (c) 2019 Contentstack LLC
|
|
4
|
-
* MIT Licensed
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const path = require('path');
|
|
8
|
-
const chalk = require('chalk');
|
|
9
|
-
const mkdirp = require('mkdirp');
|
|
10
|
-
const Promise = require('bluebird');
|
|
11
|
-
const { existsSync } = require('fs');
|
|
12
|
-
const { isEmpty, merge } = require('lodash');
|
|
13
|
-
|
|
14
|
-
const helper = require('../util/fs');
|
|
15
|
-
const { formatError } = require('../util');
|
|
16
|
-
const { addlogs } = require('../util/log');
|
|
17
|
-
const config = require('../../config/default');
|
|
18
|
-
|
|
19
|
-
module.exports = class ImportLabels {
|
|
20
|
-
config;
|
|
21
|
-
fails = [];
|
|
22
|
-
success = [];
|
|
23
|
-
labelUids = [];
|
|
24
|
-
labelsFolderPath;
|
|
25
|
-
labelUidMapper = {};
|
|
26
|
-
labelConfig = config.modules.labels;
|
|
27
|
-
reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
|
|
28
|
-
|
|
29
|
-
constructor(importConfig, stackAPIClient) {
|
|
30
|
-
this.config = merge(config, importConfig);
|
|
31
|
-
this.stackAPIClient = stackAPIClient;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
start() {
|
|
35
|
-
const self = this;
|
|
36
|
-
addlogs(this.config, chalk.white('Migrating labels'), 'success');
|
|
37
|
-
let labelMapperPath = path.resolve(this.config.data, 'mapper', 'labels');
|
|
38
|
-
let labelFailsPath = path.resolve(this.config.data, 'labels', 'fails.json');
|
|
39
|
-
let labelSuccessPath = path.resolve(this.config.data, 'labels', 'success.json');
|
|
40
|
-
this.labelsFolderPath = path.resolve(this.config.data, this.labelConfig.dirName);
|
|
41
|
-
let labelUidMapperPath = path.resolve(this.config.data, 'mapper', 'labels', 'uid-mapping.json');
|
|
42
|
-
|
|
43
|
-
if (existsSync(labelUidMapperPath)) {
|
|
44
|
-
this.labelUidMapper = helper.readFileSync(labelUidMapperPath);
|
|
45
|
-
this.labelUidMapper = this.labelUidMapper || {};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
self.labels = helper.readFileSync(path.resolve(this.labelsFolderPath, this.labelConfig.fileName));
|
|
49
|
-
|
|
50
|
-
mkdirp.sync(labelMapperPath);
|
|
51
|
-
|
|
52
|
-
return new Promise(function (resolve, reject) {
|
|
53
|
-
if (self.labels == undefined || isEmpty(self.labels)) {
|
|
54
|
-
addlogs(self.config, chalk.white('No Label Found'), 'success');
|
|
55
|
-
return resolve({ empty: true });
|
|
56
|
-
}
|
|
57
|
-
self.labelUids = Object.keys(self.labels);
|
|
58
|
-
return Promise.map(
|
|
59
|
-
self.labelUids,
|
|
60
|
-
function (labelUid) {
|
|
61
|
-
let label = self.labels[labelUid];
|
|
62
|
-
|
|
63
|
-
if (label.parent.length != 0) {
|
|
64
|
-
delete label['parent'];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (!self.labelUidMapper.hasOwnProperty(labelUid)) {
|
|
68
|
-
let requestOption = { label: label };
|
|
69
|
-
|
|
70
|
-
return self.stackAPIClient
|
|
71
|
-
.label()
|
|
72
|
-
.create(requestOption)
|
|
73
|
-
.then(function (response) {
|
|
74
|
-
self.labelUidMapper[labelUid] = response;
|
|
75
|
-
})
|
|
76
|
-
.catch(function (error) {
|
|
77
|
-
self.fails.push(label);
|
|
78
|
-
if (error.errors.name) {
|
|
79
|
-
addlogs(self.config,`Label '${label.name}' already exist`, 'error');
|
|
80
|
-
} else {
|
|
81
|
-
addlogs(self.config,`Label '${label.name}' failed to be imported\n`, 'error');
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
} else {
|
|
85
|
-
// the label has already been created
|
|
86
|
-
addlogs(
|
|
87
|
-
self.config,
|
|
88
|
-
chalk.white(`The label '${label.name}' already exists. Skipping it to avoid duplicates!'`),
|
|
89
|
-
'success',
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
// import 1 labels at a time
|
|
93
|
-
},
|
|
94
|
-
{ concurrency: self.reqConcurrency },
|
|
95
|
-
)
|
|
96
|
-
.then(function () {
|
|
97
|
-
helper.writeFileSync(labelUidMapperPath, self.labelUidMapper);
|
|
98
|
-
// eslint-disable-next-line no-undef
|
|
99
|
-
return self
|
|
100
|
-
.updateLabels()
|
|
101
|
-
.then(function () {
|
|
102
|
-
helper.writeFileSync(labelSuccessPath, self.success);
|
|
103
|
-
addlogs(self.config, chalk.green('Labels have been imported successfully!'), 'success');
|
|
104
|
-
return resolve();
|
|
105
|
-
})
|
|
106
|
-
.catch(function (error) {
|
|
107
|
-
addlogs(self.config, `Failed to import label, ${formatError(error)}`, 'error');
|
|
108
|
-
// eslint-disable-next-line no-console
|
|
109
|
-
return reject(error);
|
|
110
|
-
});
|
|
111
|
-
})
|
|
112
|
-
.catch(function (error) {
|
|
113
|
-
// error while importing labels
|
|
114
|
-
helper.writeFileSync(labelUidMapperPath, self.labelUidMapper);
|
|
115
|
-
helper.writeFileSync(labelFailsPath, self.fails);
|
|
116
|
-
addlogs(self.config, `Failed to import label, ${formatError(error)}`, 'error');
|
|
117
|
-
return reject(error);
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
updateLabels() {
|
|
123
|
-
const self = this;
|
|
124
|
-
return new Promise(function (resolve, reject) {
|
|
125
|
-
let labelsObj = helper.readFileSync(path.resolve(self.labelsFolderPath, self.labelConfig.fileName));
|
|
126
|
-
return Promise.map(
|
|
127
|
-
self.labelUids,
|
|
128
|
-
function (labelUid) {
|
|
129
|
-
let label = labelsObj[labelUid];
|
|
130
|
-
if (self.labelUidMapper.hasOwnProperty(labelUid)) {
|
|
131
|
-
let newLabelUid = self.labelUidMapper[labelUid];
|
|
132
|
-
if (label.parent.length > 0) {
|
|
133
|
-
let parentUids = label.parent;
|
|
134
|
-
for (let i = 0; i < parentUids.length; i++) {
|
|
135
|
-
if (self.labelUidMapper.hasOwnProperty(parentUids[i])) {
|
|
136
|
-
label.parent[i] = self.labelUidMapper[parentUids[i]].uid;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return self.stackAPIClient
|
|
140
|
-
.label(newLabelUid.uid)
|
|
141
|
-
.fetch()
|
|
142
|
-
.then(function (response) {
|
|
143
|
-
//Object.assign(response, _.cloneDeep(label))
|
|
144
|
-
response.parent = label.parent;
|
|
145
|
-
response
|
|
146
|
-
.update()
|
|
147
|
-
.then((result) => {
|
|
148
|
-
self.success.push(result);
|
|
149
|
-
})
|
|
150
|
-
.catch((error) => {
|
|
151
|
-
addlogs(self.config, formatError(error), 'error');
|
|
152
|
-
return reject(error);
|
|
153
|
-
});
|
|
154
|
-
})
|
|
155
|
-
.catch(function (error) {
|
|
156
|
-
addlogs(self.config, formatError(error), 'error');
|
|
157
|
-
return reject(error);
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
{
|
|
163
|
-
concurrency: self.reqConcurrency,
|
|
164
|
-
},
|
|
165
|
-
)
|
|
166
|
-
.then(function () {
|
|
167
|
-
return resolve();
|
|
168
|
-
})
|
|
169
|
-
.catch(function (error) {
|
|
170
|
-
// eslint-disable-next-line no-console
|
|
171
|
-
return reject(error);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
};
|