@contentstack/cli-cm-import 0.1.1-beta.7 → 1.0.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/LICENSE +21 -0
- package/README.md +54 -23
- package/oclif.manifest.json +1 -1
- package/package.json +70 -66
- package/src/app.js +170 -103
- package/src/commands/cm/stacks/import.js +186 -0
- package/src/config/default.js +17 -57
- package/src/lib/import/assets.js +347 -281
- package/src/lib/import/content-types.js +203 -172
- package/src/lib/import/entries.js +1274 -681
- package/src/lib/import/environments.js +97 -82
- package/src/lib/import/extensions.js +95 -77
- package/src/lib/import/global-fields.js +114 -103
- package/src/lib/import/labels.js +118 -98
- package/src/lib/import/locales.js +124 -111
- package/src/lib/import/webhooks.js +76 -59
- package/src/lib/import/workflows.js +88 -68
- package/src/lib/util/contentstack-management-sdk.js +21 -8
- package/src/lib/util/extensionsUidReplace.js +34 -22
- package/src/lib/util/fs.js +3 -4
- package/src/lib/util/import-flags.js +150 -111
- package/src/lib/util/index.js +134 -130
- package/src/lib/util/log.js +73 -35
- package/src/lib/util/login.js +37 -36
- package/src/lib/util/lookupReplaceAssets.js +167 -61
- package/src/lib/util/lookupReplaceEntries.js +144 -66
- package/src/lib/util/removeReferenceFields.js +29 -26
- package/src/lib/util/schemaTemplate.js +31 -33
- package/src/lib/util/supress-mandatory-fields.js +14 -8
- package/src/lib/util/upload.js +29 -28
- package/src/commands/cm/import.js +0 -159
- package/src/lib/import/.DS_Store +0 -0
- package/src/lib/util/request.js +0 -82
|
@@ -4,102 +4,117 @@
|
|
|
4
4
|
* MIT Licensed
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
const mkdirp = require('mkdirp')
|
|
8
|
-
const fs = require('fs')
|
|
9
|
-
const path = require('path')
|
|
10
|
-
const Promise = require('bluebird')
|
|
7
|
+
const mkdirp = require('mkdirp');
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const Promise = require('bluebird');
|
|
11
|
+
const { isEmpty } = require('lodash');
|
|
11
12
|
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
let
|
|
16
|
-
const chalk = require('chalk')
|
|
17
|
-
let stack = require('../util/contentstack-management-sdk')
|
|
13
|
+
const helper = require('../util/fs');
|
|
14
|
+
let { addlogs } = require('../util/log');
|
|
15
|
+
const chalk = require('chalk');
|
|
16
|
+
let stack = require('../util/contentstack-management-sdk');
|
|
18
17
|
|
|
19
|
-
let config = require('../../config/default')
|
|
20
|
-
let environmentConfig = config.modules.environments
|
|
21
|
-
let environmentsFolderPath
|
|
22
|
-
let envMapperPath
|
|
23
|
-
let envUidMapperPath
|
|
24
|
-
let envSuccessPath
|
|
25
|
-
let envFailsPath
|
|
26
|
-
let client
|
|
18
|
+
let config = require('../../config/default');
|
|
19
|
+
let environmentConfig = config.modules.environments;
|
|
20
|
+
let environmentsFolderPath;
|
|
21
|
+
let envMapperPath;
|
|
22
|
+
let envUidMapperPath;
|
|
23
|
+
let envSuccessPath;
|
|
24
|
+
let envFailsPath;
|
|
25
|
+
let client;
|
|
27
26
|
|
|
28
|
-
function importEnvironments
|
|
29
|
-
this.fails = []
|
|
30
|
-
this.success = []
|
|
31
|
-
this.envUidMapper = {}
|
|
27
|
+
function importEnvironments() {
|
|
28
|
+
this.fails = [];
|
|
29
|
+
this.success = [];
|
|
30
|
+
this.envUidMapper = {};
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
importEnvironments.prototype = {
|
|
35
34
|
start: function (credentialConfig) {
|
|
36
|
-
let self = this
|
|
37
|
-
config = credentialConfig
|
|
38
|
-
addlogs(config, 'Migrating environment', 'success')
|
|
39
|
-
environmentsFolderPath = path.resolve(config.data, environmentConfig.dirName)
|
|
40
|
-
envMapperPath = path.resolve(config.data, 'mapper', 'environments')
|
|
41
|
-
envUidMapperPath = path.resolve(config.data, 'mapper', 'environments', 'uid-mapping.json')
|
|
42
|
-
envSuccessPath = path.resolve(config.data, 'environments', 'success.json')
|
|
43
|
-
envFailsPath = path.resolve(config.data, 'environments', 'fails.json')
|
|
44
|
-
self.environments = helper.readFile(path.resolve(environmentsFolderPath, environmentConfig.fileName))
|
|
45
|
-
client = stack.Client(config)
|
|
35
|
+
let self = this;
|
|
36
|
+
config = credentialConfig;
|
|
37
|
+
addlogs(config, 'Migrating environment', 'success');
|
|
38
|
+
environmentsFolderPath = path.resolve(config.data, environmentConfig.dirName);
|
|
39
|
+
envMapperPath = path.resolve(config.data, 'mapper', 'environments');
|
|
40
|
+
envUidMapperPath = path.resolve(config.data, 'mapper', 'environments', 'uid-mapping.json');
|
|
41
|
+
envSuccessPath = path.resolve(config.data, 'environments', 'success.json');
|
|
42
|
+
envFailsPath = path.resolve(config.data, 'environments', 'fails.json');
|
|
43
|
+
self.environments = helper.readFile(path.resolve(environmentsFolderPath, environmentConfig.fileName));
|
|
44
|
+
client = stack.Client(config);
|
|
46
45
|
if (fs.existsSync(envUidMapperPath)) {
|
|
47
|
-
self.envUidMapper = helper.readFile(envUidMapperPath)
|
|
48
|
-
self.envUidMapper = self.envUidMapper || {}
|
|
46
|
+
self.envUidMapper = helper.readFile(envUidMapperPath);
|
|
47
|
+
self.envUidMapper = self.envUidMapper || {};
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
mkdirp.sync(envMapperPath)
|
|
50
|
+
mkdirp.sync(envMapperPath);
|
|
52
51
|
return new Promise(function (resolve, reject) {
|
|
53
|
-
if (self.environments === undefined) {
|
|
54
|
-
addlogs(config, chalk.yellow('No Environment Found'), 'success')
|
|
55
|
-
return resolve()
|
|
52
|
+
if (self.environments === undefined || isEmpty(self.environments)) {
|
|
53
|
+
addlogs(config, chalk.yellow('No Environment Found'), 'success');
|
|
54
|
+
return resolve({ empty: true });
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
let envUids = Object.keys(self.environments)
|
|
59
|
-
return Promise.map(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
let
|
|
57
|
+
let envUids = Object.keys(self.environments);
|
|
58
|
+
return Promise.map(
|
|
59
|
+
envUids,
|
|
60
|
+
function (envUid) {
|
|
61
|
+
let env = self.environments[envUid];
|
|
62
|
+
if (!self.envUidMapper.hasOwnProperty(envUid)) {
|
|
63
|
+
let requestOption = {
|
|
63
64
|
environment: env,
|
|
64
|
-
|
|
65
|
+
};
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
67
|
+
return client
|
|
68
|
+
.stack({ api_key: config.target_stack, management_token: config.management_token })
|
|
69
|
+
.environment()
|
|
70
|
+
.create(requestOption)
|
|
71
|
+
.then((environment) => {
|
|
72
|
+
self.success.push(environment.items);
|
|
73
|
+
self.envUidMapper[envUid] = environment.uid;
|
|
74
|
+
helper.writeFile(envUidMapperPath, self.envUidMapper);
|
|
75
|
+
})
|
|
76
|
+
.catch(function (err) {
|
|
77
|
+
let error = JSON.parse(err.message);
|
|
78
|
+
if (error.errors.name) {
|
|
79
|
+
addlogs(config, chalk.white("Environment: '" + env.name + "' already exists"), 'error');
|
|
80
|
+
} else {
|
|
81
|
+
addlogs(
|
|
82
|
+
config,
|
|
83
|
+
chalk.white(
|
|
84
|
+
"Environment: '" + env.name + "' failed to be import\n " + JSON.stringify(error.errors),
|
|
85
|
+
),
|
|
86
|
+
'error',
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
// the environment has already been created
|
|
92
|
+
addlogs(
|
|
93
|
+
config,
|
|
94
|
+
chalk.white("The environment: '" + env.name + "' already exists. Skipping it to avoid duplicates!"),
|
|
95
|
+
'success',
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
// import 2 environments at a time
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
concurrency: 2,
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
.then(function () {
|
|
105
|
+
// environments have imported successfully
|
|
106
|
+
helper.writeFile(envSuccessPath, self.success);
|
|
107
|
+
addlogs(config, chalk.green('Environments have been imported successfully!'), 'success');
|
|
108
|
+
return resolve();
|
|
109
|
+
})
|
|
110
|
+
.catch(function (error) {
|
|
111
|
+
// error while importing environments
|
|
112
|
+
helper.writeFile(envFailsPath, self.fails);
|
|
113
|
+
addlogs(config, chalk.red('Environment import failed'), 'error');
|
|
114
|
+
return reject(error);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
102
117
|
},
|
|
103
|
-
}
|
|
118
|
+
};
|
|
104
119
|
|
|
105
|
-
module.exports = new importEnvironments()
|
|
120
|
+
module.exports = new importEnvironments();
|
|
@@ -4,96 +4,114 @@
|
|
|
4
4
|
* MIT Licensed
|
|
5
5
|
*/
|
|
6
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')
|
|
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} = require('lodash');
|
|
12
13
|
|
|
13
|
-
const helper = require('../util/fs')
|
|
14
|
-
const {addlogs} = require('../util/log')
|
|
15
|
-
const util = require('../util')
|
|
14
|
+
const helper = require('../util/fs');
|
|
15
|
+
const { addlogs } = require('../util/log');
|
|
16
|
+
const util = require('../util');
|
|
16
17
|
|
|
17
|
-
let config = util.getConfig()
|
|
18
|
-
const reqConcurrency = config.concurrency
|
|
19
|
-
const extensionsConfig = config.modules.extensions
|
|
20
|
-
const stack = require('../util/contentstack-management-sdk')
|
|
21
|
-
let extensionsFolderPath
|
|
22
|
-
let extMapperPath
|
|
23
|
-
let extUidMapperPath
|
|
24
|
-
let extSuccessPath
|
|
25
|
-
let extFailsPath
|
|
26
|
-
let client
|
|
18
|
+
let config = util.getConfig();
|
|
19
|
+
const reqConcurrency = config.concurrency;
|
|
20
|
+
const extensionsConfig = config.modules.extensions;
|
|
21
|
+
const stack = require('../util/contentstack-management-sdk');
|
|
22
|
+
let extensionsFolderPath;
|
|
23
|
+
let extMapperPath;
|
|
24
|
+
let extUidMapperPath;
|
|
25
|
+
let extSuccessPath;
|
|
26
|
+
let extFailsPath;
|
|
27
|
+
let client;
|
|
27
28
|
|
|
28
|
-
function importExtensions
|
|
29
|
-
this.fails = []
|
|
30
|
-
this.success = []
|
|
31
|
-
this.extUidMapper = {}
|
|
29
|
+
function importExtensions() {
|
|
30
|
+
this.fails = [];
|
|
31
|
+
this.success = [];
|
|
32
|
+
this.extUidMapper = {};
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
importExtensions.prototype = {
|
|
35
36
|
start: function (credential) {
|
|
36
|
-
addlogs(config, chalk.white('Migrating extensions'), 'success')
|
|
37
|
-
let self = this
|
|
38
|
-
config = credential
|
|
39
|
-
client = stack.Client(config)
|
|
37
|
+
addlogs(config, chalk.white('Migrating extensions'), 'success');
|
|
38
|
+
let self = this;
|
|
39
|
+
config = credential;
|
|
40
|
+
client = stack.Client(config);
|
|
40
41
|
|
|
41
|
-
extensionsFolderPath = path.resolve(config.data, extensionsConfig.dirName)
|
|
42
|
-
extMapperPath = path.resolve(config.data, 'mapper', 'extensions')
|
|
43
|
-
extUidMapperPath = path.resolve(config.data, 'mapper/extensions', 'uid-mapping.json')
|
|
44
|
-
extSuccessPath = path.resolve(config.data, 'extensions', 'success.json')
|
|
45
|
-
extFailsPath = path.resolve(config.data, 'extensions', 'fails.json')
|
|
46
|
-
self.extensions = helper.readFile(path.resolve(extensionsFolderPath, extensionsConfig.fileName))
|
|
42
|
+
extensionsFolderPath = path.resolve(config.data, extensionsConfig.dirName);
|
|
43
|
+
extMapperPath = path.resolve(config.data, 'mapper', 'extensions');
|
|
44
|
+
extUidMapperPath = path.resolve(config.data, 'mapper/extensions', 'uid-mapping.json');
|
|
45
|
+
extSuccessPath = path.resolve(config.data, 'extensions', 'success.json');
|
|
46
|
+
extFailsPath = path.resolve(config.data, 'extensions', 'fails.json');
|
|
47
|
+
self.extensions = helper.readFile(path.resolve(extensionsFolderPath, extensionsConfig.fileName));
|
|
47
48
|
if (fs.existsSync(extUidMapperPath)) {
|
|
48
|
-
self.extUidMapper = helper.readFile(extUidMapperPath)
|
|
49
|
-
self.extUidMapper = self.extUidMapper || {}
|
|
49
|
+
self.extUidMapper = helper.readFile(extUidMapperPath);
|
|
50
|
+
self.extUidMapper = self.extUidMapper || {};
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
mkdirp.sync(extMapperPath)
|
|
53
|
+
mkdirp.sync(extMapperPath);
|
|
53
54
|
|
|
54
55
|
return new Promise(function (resolve, reject) {
|
|
55
|
-
if (self.extensions === undefined) {
|
|
56
|
-
addlogs(config, chalk.white('No Extensions Found'), 'success')
|
|
57
|
-
return resolve()
|
|
56
|
+
if (self.extensions === undefined || isEmpty(self.extensions)) {
|
|
57
|
+
addlogs(config, chalk.white('No Extensions Found'), 'success');
|
|
58
|
+
return resolve({ empty: true });
|
|
58
59
|
}
|
|
59
|
-
let extUids = Object.keys(self.extensions)
|
|
60
|
-
return Promise.map(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
60
|
+
let extUids = Object.keys(self.extensions);
|
|
61
|
+
return Promise.map(
|
|
62
|
+
extUids,
|
|
63
|
+
function (extUid) {
|
|
64
|
+
let ext = self.extensions[extUid];
|
|
65
|
+
if (!self.extUidMapper.hasOwnProperty(extUid)) {
|
|
66
|
+
return client
|
|
67
|
+
.stack({ api_key: config.target_stack, management_token: config.management_token })
|
|
68
|
+
.extension()
|
|
69
|
+
.create({ extension: ext })
|
|
70
|
+
.then((response) => {
|
|
71
|
+
self.success.push(response);
|
|
72
|
+
self.extUidMapper[extUid] = response.uid;
|
|
73
|
+
helper.writeFile(extUidMapperPath, self.extUidMapper);
|
|
74
|
+
})
|
|
75
|
+
.catch(function (err) {
|
|
76
|
+
let error = JSON.parse(err.message);
|
|
77
|
+
self.fails.push(ext);
|
|
78
|
+
if (error.errors.title) {
|
|
79
|
+
addlogs(config, chalk.white("Extension: '" + ext.title + "' already exists"), 'success');
|
|
80
|
+
} else {
|
|
81
|
+
addlogs(
|
|
82
|
+
config,
|
|
83
|
+
chalk.white("Extension: '" + ext.title + "' failed to be import\n " + JSON.stringify(error.errors)),
|
|
84
|
+
'error',
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
// the extensions has already been created
|
|
90
|
+
addlogs(
|
|
91
|
+
config,
|
|
92
|
+
chalk.white("The extension: '" + ext.title + "' already exists. Skipping it to avoid duplicates!"),
|
|
93
|
+
'success',
|
|
94
|
+
);
|
|
95
|
+
// import 2 extensions at a time
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
concurrency: reqConcurrency,
|
|
99
|
+
},
|
|
100
|
+
)
|
|
101
|
+
.then(function () {
|
|
102
|
+
// extensions have imported successfully
|
|
103
|
+
helper.writeFile(extSuccessPath, self.success);
|
|
104
|
+
addlogs(config, chalk.green('Extensions have been imported successfully!'), 'success');
|
|
105
|
+
return resolve();
|
|
106
|
+
})
|
|
107
|
+
.catch(function (error) {
|
|
108
|
+
// error while importing extensions
|
|
109
|
+
helper.writeFile(extFailsPath, self.fails);
|
|
110
|
+
addlogs(config, chalk.red('Extension import failed'), 'error');
|
|
111
|
+
return reject(error);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
96
114
|
},
|
|
97
|
-
}
|
|
115
|
+
};
|
|
98
116
|
|
|
99
|
-
module.exports = new importExtensions()
|
|
117
|
+
module.exports = new importExtensions();
|
|
@@ -4,130 +4,141 @@
|
|
|
4
4
|
* MIT Licensed
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
let mkdirp = require('mkdirp')
|
|
8
|
-
let fs = require('fs')
|
|
9
|
-
let path = require('path')
|
|
10
|
-
let Promise = require('bluebird')
|
|
11
|
-
let chalk = require('chalk')
|
|
7
|
+
let mkdirp = require('mkdirp');
|
|
8
|
+
let fs = require('fs');
|
|
9
|
+
let path = require('path');
|
|
10
|
+
let Promise = require('bluebird');
|
|
11
|
+
let chalk = require('chalk');
|
|
12
|
+
const {isEmpty} = require('lodash');
|
|
12
13
|
|
|
13
|
-
let helper = require('../util/fs')
|
|
14
|
-
let {addlogs} = require('../util/log')
|
|
15
|
-
let
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
const stack = require('../util/contentstack-management-sdk')
|
|
14
|
+
let helper = require('../util/fs');
|
|
15
|
+
let { addlogs } = require('../util/log');
|
|
16
|
+
let extension_supress = require('../util/extensionsUidReplace');
|
|
17
|
+
let removeReferenceFields = require('../util/removeReferenceFields');
|
|
18
|
+
const stack = require('../util/contentstack-management-sdk');
|
|
19
19
|
|
|
20
|
-
let config = require('../../config/default')
|
|
21
|
-
let reqConcurrency = config.concurrency
|
|
22
|
-
let globalfieldsConfig = config.modules.globalfields
|
|
23
|
-
let globalfieldsFolderPath
|
|
24
|
-
let globalfieldsMapperPath
|
|
25
|
-
let globalfieldsUidMapperPath
|
|
26
|
-
let globalfieldsSuccessPath
|
|
27
|
-
let globalfieldsFailsPath
|
|
28
|
-
let client
|
|
20
|
+
let config = require('../../config/default');
|
|
21
|
+
let reqConcurrency = config.concurrency;
|
|
22
|
+
let globalfieldsConfig = config.modules.globalfields;
|
|
23
|
+
let globalfieldsFolderPath;
|
|
24
|
+
let globalfieldsMapperPath;
|
|
25
|
+
let globalfieldsUidMapperPath;
|
|
26
|
+
let globalfieldsSuccessPath;
|
|
27
|
+
let globalfieldsFailsPath;
|
|
28
|
+
let client;
|
|
29
|
+
let globalFieldsPending;
|
|
29
30
|
|
|
31
|
+
global._globalField_pending = [];
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
this.success = []
|
|
36
|
-
this.snipUidMapper = {}
|
|
33
|
+
function importGlobalFields() {
|
|
34
|
+
this.fails = [];
|
|
35
|
+
this.success = [];
|
|
36
|
+
this.snipUidMapper = {};
|
|
37
37
|
this.requestOptions = {
|
|
38
38
|
uri: config.host + config.apis.globalfields,
|
|
39
39
|
headers: config.headers,
|
|
40
40
|
method: 'POST',
|
|
41
|
-
}
|
|
41
|
+
};
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
importGlobalFields.prototype = {
|
|
45
45
|
start: function (credential) {
|
|
46
|
-
addlogs(config, chalk.white('Migrating global-fields'), 'success')
|
|
47
|
-
let self = this
|
|
48
|
-
config = credential
|
|
49
|
-
globalfieldsFolderPath = path.resolve(config.data, globalfieldsConfig.dirName)
|
|
50
|
-
globalfieldsMapperPath = path.resolve(config.data, 'mapper', 'global_fields')
|
|
51
|
-
globalfieldsUidMapperPath = path.resolve(config.data, 'mapper', 'global_fields', 'uid-mapping.json')
|
|
52
|
-
globalfieldsSuccessPath = path.resolve(config.data, 'mapper', 'global_fields', 'success.json')
|
|
53
|
-
globalFieldsPending = path.resolve(config.data, 'mapper', 'global_fields', 'pending_global_fields.js')
|
|
54
|
-
globalfieldsFailsPath = path.resolve(config.data, 'mapper', 'global_fields', 'fails.json')
|
|
55
|
-
self.globalfields = helper.readFile(path.resolve(globalfieldsFolderPath, globalfieldsConfig.fileName))
|
|
46
|
+
addlogs(config, chalk.white('Migrating global-fields'), 'success');
|
|
47
|
+
let self = this;
|
|
48
|
+
config = credential;
|
|
49
|
+
globalfieldsFolderPath = path.resolve(config.data, globalfieldsConfig.dirName);
|
|
50
|
+
globalfieldsMapperPath = path.resolve(config.data, 'mapper', 'global_fields');
|
|
51
|
+
globalfieldsUidMapperPath = path.resolve(config.data, 'mapper', 'global_fields', 'uid-mapping.json');
|
|
52
|
+
globalfieldsSuccessPath = path.resolve(config.data, 'mapper', 'global_fields', 'success.json');
|
|
53
|
+
globalFieldsPending = path.resolve(config.data, 'mapper', 'global_fields', 'pending_global_fields.js');
|
|
54
|
+
globalfieldsFailsPath = path.resolve(config.data, 'mapper', 'global_fields', 'fails.json');
|
|
55
|
+
self.globalfields = helper.readFile(path.resolve(globalfieldsFolderPath, globalfieldsConfig.fileName));
|
|
56
56
|
if (fs.existsSync(globalfieldsUidMapperPath)) {
|
|
57
|
-
self.snipUidMapper = helper.readFile(globalfieldsUidMapperPath)
|
|
58
|
-
self.snipUidMapper = this.snipUidMapper || {}
|
|
57
|
+
self.snipUidMapper = helper.readFile(globalfieldsUidMapperPath);
|
|
58
|
+
self.snipUidMapper = this.snipUidMapper || {};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
if (!fs.existsSync(globalfieldsMapperPath)) {
|
|
62
|
-
mkdirp.sync(globalfieldsMapperPath)
|
|
62
|
+
mkdirp.sync(globalfieldsMapperPath);
|
|
63
63
|
}
|
|
64
|
-
client = stack.Client(config)
|
|
64
|
+
client = stack.Client(config);
|
|
65
65
|
return new Promise(function (resolve, reject) {
|
|
66
|
-
if (self.globalfields === undefined) {
|
|
67
|
-
addlogs(config, chalk.white('No globalfields Found'), 'success')
|
|
68
|
-
helper.writeFile(globalFieldsPending, _globalField_pending)
|
|
69
|
-
return resolve()
|
|
66
|
+
if (self.globalfields === undefined || isEmpty(self.globalfields)) {
|
|
67
|
+
addlogs(config, chalk.white('No globalfields Found'), 'success');
|
|
68
|
+
helper.writeFile(globalFieldsPending, _globalField_pending);
|
|
69
|
+
return resolve({ empty: true });
|
|
70
70
|
}
|
|
71
|
-
let snipUids = Object.keys(self.globalfields)
|
|
72
|
-
return Promise.map(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
let snipUids = Object.keys(self.globalfields);
|
|
72
|
+
return Promise.map(
|
|
73
|
+
snipUids,
|
|
74
|
+
function (snipUid) {
|
|
75
|
+
let flag = {
|
|
76
|
+
supressed: false,
|
|
77
|
+
};
|
|
78
|
+
let snip = self.globalfields[snipUid];
|
|
79
|
+
extension_supress(snip.schema);
|
|
80
|
+
removeReferenceFields(snip.schema, flag);
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
if (flag.supressed) {
|
|
83
|
+
// eslint-disable-next-line no-undef
|
|
84
|
+
_globalField_pending.push(snip.uid);
|
|
85
|
+
}
|
|
84
86
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
if (!self.snipUidMapper.hasOwnProperty(snipUid)) {
|
|
88
|
+
let requestOption = {
|
|
89
|
+
global_field: snip,
|
|
90
|
+
};
|
|
91
|
+
return client
|
|
92
|
+
.stack({ api_key: config.target_stack, management_token: config.management_token })
|
|
93
|
+
.globalField()
|
|
94
|
+
.create(requestOption)
|
|
95
|
+
.then((globalField) => {
|
|
96
|
+
self.success.push(globalField.items);
|
|
97
|
+
let global_field_uid = globalField.uid;
|
|
98
|
+
self.snipUidMapper[snipUid] = globalField.items;
|
|
99
|
+
helper.writeFile(globalfieldsUidMapperPath, self.snipUidMapper);
|
|
100
|
+
addlogs(config, chalk.green('Global field ' + global_field_uid + ' created successfully'), 'success');
|
|
101
|
+
})
|
|
102
|
+
.catch(function (err) {
|
|
103
|
+
let error = JSON.parse(err.message);
|
|
104
|
+
if (error.errors.title) {
|
|
105
|
+
// eslint-disable-next-line no-undef
|
|
106
|
+
addlogs(config, chalk.white(snip.uid + ' globalfield already exists'), 'error');
|
|
107
|
+
} else {
|
|
108
|
+
addlogs(config, chalk.red('Globalfield failed to import ' + JSON.stringify(error.errors)), 'error');
|
|
109
|
+
}
|
|
110
|
+
self.fails.push(snip);
|
|
111
|
+
});
|
|
112
|
+
} else {
|
|
113
|
+
// globalfields has already been created
|
|
114
|
+
addlogs(
|
|
115
|
+
config,
|
|
116
|
+
chalk.white('The globalfields already exists. Skipping it to avoid duplicates!'),
|
|
117
|
+
'success',
|
|
118
|
+
);
|
|
88
119
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
addlogs(config, chalk.white('The globalfields already exists. Skipping it to avoid duplicates!'), 'success')
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
// import 2 globalfields at a time
|
|
114
|
-
}, {
|
|
115
|
-
concurrency: reqConcurrency,
|
|
116
|
-
}).then(function () {
|
|
117
|
-
// globalfields have imported successfully
|
|
118
|
-
helper.writeFile(globalfieldsSuccessPath, self.success)
|
|
119
|
-
helper.writeFile(globalFieldsPending, _globalField_pending)
|
|
120
|
-
addlogs(config, chalk.green('globalfields have been imported successfully!'), 'success')
|
|
121
|
-
return resolve()
|
|
122
|
-
}).catch(function (err) {
|
|
123
|
-
let error = JSON.parse(err)
|
|
124
|
-
// error while importing globalfields
|
|
125
|
-
helper.writeFile(globalfieldsFailsPath, self.fails)
|
|
126
|
-
addlogs(config, chalk.red('globalfields import failed'), 'error')
|
|
127
|
-
return reject(error)
|
|
128
|
-
})
|
|
129
|
-
})
|
|
120
|
+
// import 2 globalfields at a time
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
concurrency: reqConcurrency,
|
|
124
|
+
},
|
|
125
|
+
)
|
|
126
|
+
.then(function () {
|
|
127
|
+
// globalfields have imported successfully
|
|
128
|
+
helper.writeFile(globalfieldsSuccessPath, self.success);
|
|
129
|
+
helper.writeFile(globalFieldsPending, _globalField_pending);
|
|
130
|
+
addlogs(config, chalk.green('globalfields have been imported successfully!'), 'success');
|
|
131
|
+
return resolve();
|
|
132
|
+
})
|
|
133
|
+
.catch(function (err) {
|
|
134
|
+
let error = JSON.parse(err);
|
|
135
|
+
// error while importing globalfields
|
|
136
|
+
helper.writeFile(globalfieldsFailsPath, self.fails);
|
|
137
|
+
addlogs(config, chalk.red('globalfields import failed'), 'error');
|
|
138
|
+
return reject(error);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
130
141
|
},
|
|
131
|
-
}
|
|
142
|
+
};
|
|
132
143
|
|
|
133
|
-
module.exports = new importGlobalFields()
|
|
144
|
+
module.exports = new importGlobalFields();
|