@contentstack/cli-cm-import 0.1.1-beta.9 → 1.0.1

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.
@@ -5,68 +5,95 @@
5
5
  */
6
6
 
7
7
  // eslint-disable-next-line unicorn/filename-case
8
- var path = require('path')
9
- var _ = require('lodash')
8
+ var path = require('path');
9
+ var _ = require('lodash');
10
10
 
11
- var util = require('.')
12
- var helper = require('./fs')
13
- var config = util.getConfig()
11
+ var util = require('.');
12
+ var helper = require('./fs');
13
+ var config = util.getConfig();
14
14
  // update references in entry object
15
15
  module.exports = function (data, mappedUids, uidMapperPath) {
16
- var parent = []
17
- var uids = []
18
- var unmapped = []
19
- var mapped = []
16
+ var parent = [];
17
+ var uids = [];
18
+ var unmapped = [];
19
+ var mapped = [];
20
20
 
21
- var isNewRefFields = false
22
- var preserveStackVersion = config.preserveStackVersion
21
+ var isNewRefFields = false;
22
+ var preserveStackVersion = config.preserveStackVersion;
23
23
 
24
- var update = function (parent, form_id, entry) {
25
- var _entry = entry
26
- var len = parent.length
24
+ function gatherJsonRteEntryIds(jsonRteData) {
25
+ jsonRteData.children.forEach(element => {
26
+ if (element.type) {
27
+ switch (element.type) {
28
+ case 'a':
29
+ case 'p': {
30
+ if (element.children && element.children.length > 0) {
31
+ gatherJsonRteEntryIds(element)
32
+ }
33
+ break;
34
+ }
35
+ case 'reference': {
36
+ if (Object.keys(element.attrs).length > 0 && element.attrs.type === "entry") {
37
+ if (uids.indexOf(element.attrs['entry-uid']) === -1) {
38
+ uids.push(element.attrs['entry-uid'])
39
+ }
40
+ }
41
+ if (element.children && element.children.length > 0) {
42
+ gatherJsonRteEntryIds(element)
43
+ }
44
+ break;
45
+ }
46
+ }
47
+ }
48
+ })
49
+ }
50
+
51
+ var update = function (_parent, form_id, updateEntry) {
52
+ var _entry = updateEntry
53
+ var len = _parent.length
27
54
  for (var j = 0; j < len; j++) {
28
- if (_entry && parent[j]) {
29
- if (j === (len - 1) && _entry[parent[j]]) {
55
+ if (_entry && _parent[j]) {
56
+ if (j === len - 1 && _entry[_parent[j]]) {
30
57
  if (form_id !== '_assets') {
31
- if (_entry[parent[j]].length) {
32
- _entry[parent[j]].forEach((item, idx) => {
58
+ if (_entry[_parent[j]].length) {
59
+ _entry[_parent[j]].forEach((item, idx) => {
33
60
  if (typeof item.uid === 'string' && item._content_type_uid) {
34
- uids.push(item.uid)
35
- } else if (typeof item === 'string' && (preserveStackVersion === true)) {
36
- uids.push(item)
61
+ uids.push(item.uid);
62
+ } else if (typeof item === 'string' && preserveStackVersion === true) {
63
+ uids.push(item);
37
64
  } else {
38
- uids.push(item)
39
- _entry[parent[j]][idx] = {
65
+ uids.push(item);
66
+ _entry[_parent[j]][idx] = {
40
67
  uid: item,
41
68
  _content_type_uid: form_id,
42
- }
69
+ };
43
70
  }
44
- })
71
+ });
45
72
  }
46
- } else if (Array.isArray(_entry[parent[j]])) {
47
- for (var k = 0; k < _entry[parent[j]].length; k++) {
48
- if (_entry[parent[j]][k].uid.length) {
49
- uids.push(_entry[parent[j]][k].uid)
73
+ } else if (Array.isArray(_entry[_parent[j]])) {
74
+ for (var k = 0; k < _entry[_parent[j]].length; k++) {
75
+ if (_entry[_parent[j]][k].uid.length) {
76
+ uids.push(_entry[_parent[j]][k].uid);
50
77
  }
51
78
  }
52
- } else if (_entry[parent[j]].uid.length) {
53
- uids.push(_entry[parent[j]].uid)
79
+ } else if (_entry[_parent[j]].uid.length) {
80
+ uids.push(_entry[_parent[j]].uid);
54
81
  }
55
82
  } else {
56
- _entry = _entry[parent[j]]
57
- var _keys = _.clone(parent).splice(eval(j + 1), len)
83
+ _entry = _entry[_parent[j]];
84
+ var _keys = _.clone(_parent).splice((j+1), len);
58
85
  if (Array.isArray(_entry)) {
59
86
  for (var i = 0, _i = _entry.length; i < _i; i++) {
60
- update(_keys, form_id, _entry[i])
87
+ update(_keys, form_id, _entry[i]);
61
88
  }
62
89
  } else if (!(_entry instanceof Object)) {
63
- break
90
+ break;
64
91
  }
65
92
  }
66
93
  }
67
94
  }
68
- }
69
- var find = function (schema, entry) {
95
+ };
96
+ var find = function (schema, _entry) {
70
97
  for (var i = 0, _i = schema.length; i < _i; i++) {
71
98
  switch (schema[i].data_type) {
72
99
  case 'reference':
@@ -74,96 +101,147 @@ module.exports = function (data, mappedUids, uidMapperPath) {
74
101
  isNewRefFields = true
75
102
  schema[i].reference_to.forEach(reference => {
76
103
  parent.push(schema[i].uid)
77
- update(parent, reference, entry)
104
+ update(parent, reference, _entry)
78
105
  parent.pop()
79
106
  })
80
107
  } else {
81
108
  parent.push(schema[i].uid)
82
- update(parent, schema[i].reference_to, entry)
109
+ update(parent, schema[i].reference_to, _entry)
83
110
  parent.pop()
84
111
  }
85
112
  break
113
+ case 'global_field':
86
114
  case 'group':
87
115
  parent.push(schema[i].uid)
88
- find(schema[i].schema, entry)
116
+ find(schema[i].schema, _entry)
89
117
  parent.pop()
90
118
  break
91
119
  case 'blocks':
92
120
  for (var j = 0, _j = schema[i].blocks.length; j < _j; j++) {
93
121
  parent.push(schema[i].uid)
94
122
  parent.push(schema[i].blocks[j].uid)
95
- find(schema[i].blocks[j].schema, entry)
123
+ find(schema[i].blocks[j].schema, _entry)
96
124
  parent.pop()
97
125
  parent.pop()
98
126
  }
99
127
  break
128
+ case 'json':
129
+ if (schema[i].field_metadata.rich_text_type) {
130
+ findEntryIdsFromJsonRte(data.entry, data.content_type.schema)
131
+ }
132
+ break
133
+ }
134
+ }
135
+ }
136
+
137
+ function findEntryIdsFromJsonRte(entry, ctSchema) {
138
+ for (let i = 0; i < ctSchema.length; i++) {
139
+ switch (ctSchema[i].data_type) {
140
+ case 'blocks': {
141
+ if (entry[ctSchema[i].uid]) {
142
+ if (ctSchema[i].multiple) {
143
+ entry[ctSchema[i].uid].forEach(e => {
144
+ let key = Object.keys(e).pop()
145
+ let subBlock = ctSchema[i].blocks.filter(e => e.uid === key).pop()
146
+ findEntryIdsFromJsonRte(e[key], subBlock.schema)
147
+ })
148
+ }
149
+ }
150
+ break;
151
+ }
152
+ case 'global_field':
153
+ case 'group': {
154
+ if (entry[ctSchema[i].uid]) {
155
+ if (ctSchema[i].multiple) {
156
+ entry[ctSchema[i].uid].forEach(e => {
157
+ findEntryIdsFromJsonRte(e, ctSchema[i].schema)
158
+ })
159
+ } else {
160
+ findEntryIdsFromJsonRte(entry[ctSchema[i].uid], ctSchema[i].schema)
161
+ }
162
+ }
163
+ break;
164
+ }
165
+ case 'json': {
166
+ if (entry[ctSchema[i].uid] && ctSchema[i].field_metadata.rich_text_type) {
167
+ if (ctSchema[i].multiple) {
168
+ entry[ctSchema[i].uid].forEach(jsonRteData => {
169
+ gatherJsonRteEntryIds(jsonRteData)
170
+ })
171
+ } else {
172
+ gatherJsonRteEntryIds(entry[ctSchema[i].uid])
173
+ }
174
+ }
175
+ break;
176
+ }
100
177
  }
101
178
  }
102
179
  }
180
+
103
181
  find(data.content_type.schema, data.entry)
104
182
  if (isNewRefFields) {
105
- findUidsInNewRefFields(data.entry, uids)
183
+ findUidsInNewRefFields(data.entry, uids);
106
184
  }
107
- uids = _.flattenDeep(uids)
185
+ uids = _.flattenDeep(uids);
108
186
  // if no references are found, return
109
187
  if (uids.length === 0) {
110
- return data.entry
188
+ return data.entry;
111
189
  }
112
190
 
113
- uids = _.uniq(uids)
114
- var entry = JSON.stringify(data.entry)
191
+ uids = _.uniq(uids);
192
+ var entry = JSON.stringify(data.entry);
115
193
  uids.forEach(function (uid) {
116
194
  if (mappedUids.hasOwnProperty(uid)) {
117
- entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid])
118
- mapped.push(uid)
195
+ entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid]);
196
+ mapped.push(uid);
119
197
  } else {
120
- unmapped.push(uid)
198
+ unmapped.push(uid);
121
199
  }
122
- })
200
+ });
123
201
 
124
202
  if (unmapped.length > 0) {
125
- var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'))
126
- unmappedUids = unmappedUids || {}
203
+ var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'));
204
+ unmappedUids = unmappedUids || {};
127
205
  if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
128
- unmappedUids[data.content_type.uid][data.entry.uid] = unmapped
206
+ unmappedUids[data.content_type.uid][data.entry.uid] = unmapped;
129
207
  } else {
130
208
  unmappedUids[data.content_type.uid] = {
131
209
  [data.entry.uid]: unmapped,
132
- }
210
+ };
133
211
  }
134
212
  // write the unmapped contents to ./mapper/language/unmapped-uids.json
135
- helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids)
213
+ helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids);
136
214
  }
137
215
 
138
216
  if (mapped.length > 0) {
139
- var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'))
140
- _mappedUids = _mappedUids || {}
217
+ var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'));
218
+ _mappedUids = _mappedUids || {};
141
219
  if (_mappedUids.hasOwnProperty(data.content_type.uid)) {
142
- _mappedUids[data.content_type.uid][data.entry.uid] = mapped
220
+ _mappedUids[data.content_type.uid][data.entry.uid] = mapped;
143
221
  } else {
144
222
  _mappedUids[data.content_type.uid] = {
145
223
  [data.entry.uid]: mapped,
146
- }
224
+ };
147
225
  }
148
226
  // write the mapped contents to ./mapper/language/mapped-uids.json
149
- helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids)
227
+ helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids);
150
228
  }
151
229
 
152
- return JSON.parse(entry)
153
- }
230
+ return JSON.parse(entry);
231
+ };
154
232
 
155
233
  function findUidsInNewRefFields(entry, uids) {
156
234
  if (entry && typeof entry === 'object') {
157
235
  if (entry.uid && entry._content_type_uid) {
158
- uids.push(entry.uid)
236
+ uids.push(entry.uid);
159
237
  } else if (Array.isArray(entry) && entry.length) {
160
238
  entry.forEach(function (elem) {
161
- findUidsInNewRefFields(elem, uids)
162
- })
239
+ findUidsInNewRefFields(elem, uids);
240
+ });
163
241
  } else if (Object.keys(entry).length) {
164
242
  for (var key in entry) {
165
243
  if (key) {
166
- findUidsInNewRefFields(entry[key], uids)
244
+ findUidsInNewRefFields(entry[key], uids);
167
245
  }
168
246
  }
169
247
  }
@@ -1,42 +1,45 @@
1
- //var pending_reference_fields = [];
2
1
  var _ = require('lodash');
3
2
 
4
-
5
3
  /* eslint-disable no-empty */
6
- var removeReferenceFields = module.exports = function (schema, flag) {
7
- for (var i in schema) {
4
+ var removeReferenceFields = (module.exports = function (schema, flag) {
5
+ for (let i = 0; i < schema.length; i++) {
8
6
  if (schema[i].data_type === 'group') {
9
7
  removeReferenceFields(schema[i].schema, flag);
10
8
  } else if (schema[i].data_type === 'blocks') {
11
9
  for (var block in schema[i].blocks) {
12
10
  removeReferenceFields(schema[i].blocks[block].schema, flag);
13
11
  }
14
- } else if(schema[i].data_type === 'reference') {
12
+ } else if (schema[i].data_type === 'reference') {
15
13
  flag.supressed = true;
16
- _.remove(schema, function(c) {
17
- return c.data_type === 'reference';
18
- });
19
-
20
- if(schema.length < 1) {
14
+ schema.splice(i, 1);
15
+ --i;
16
+ if (schema.length < 1) {
21
17
  schema.push({
22
- 'data_type': 'text',
23
- 'display_name': 'dummyTest',
24
- 'uid': 'dummy_test',
25
- 'field_metadata': {
26
- 'description': '',
27
- 'default_value': '',
28
- 'version': 3
18
+ data_type: 'text',
19
+ display_name: 'dummyTest',
20
+ uid: 'dummy_test',
21
+ field_metadata: {
22
+ description: '',
23
+ default_value: '',
24
+ version: 3,
29
25
  },
30
- 'format': '',
31
- 'error_messages': {
32
- 'format': ''
26
+ format: '',
27
+ error_messages: {
28
+ format: '',
33
29
  },
34
- 'multiple': false,
35
- 'mandatory': false,
36
- 'unique': false,
37
- 'non_localizable': false
30
+ multiple: false,
31
+ mandatory: false,
32
+ unique: false,
33
+ non_localizable: false,
38
34
  });
39
35
  }
40
- }
36
+ } else if( // handling entry references in json rte
37
+ schema[i].data_type === 'json'
38
+ && schema[i].field_metadata.rich_text_type
39
+ && schema[i].field_metadata.embed_entry
40
+ && schema[i].reference_to.length > 1) {
41
+ flag.supressed = true;
42
+ schema[i].reference_to = ["sys_assets"]
43
+ }
41
44
  }
42
- };
45
+ });
@@ -1,40 +1,38 @@
1
1
  module.exports = {
2
- 'content_type': {
3
- 'title': 'Seed',
4
- 'uid': '',
5
- 'schema': [
2
+ content_type: {
3
+ title: 'Seed',
4
+ uid: '',
5
+ schema: [
6
6
  {
7
- 'display_name': 'Title',
8
- 'uid': 'title',
9
- 'data_type': 'text',
10
- 'field_metadata': {
11
- '_default': true
7
+ display_name: 'Title',
8
+ uid: 'title',
9
+ data_type: 'text',
10
+ field_metadata: {
11
+ _default: true,
12
12
  },
13
- 'unique': false,
14
- 'mandatory': true,
15
- 'multiple': false
13
+ unique: false,
14
+ mandatory: true,
15
+ multiple: false,
16
16
  },
17
17
  {
18
- 'display_name': 'URL',
19
- 'uid': 'url',
20
- 'data_type': 'text',
21
- 'field_metadata': {
22
- '_default': true
18
+ display_name: 'URL',
19
+ uid: 'url',
20
+ data_type: 'text',
21
+ field_metadata: {
22
+ _default: true,
23
23
  },
24
- 'unique': false,
25
- 'multiple': false
26
- }
24
+ unique: false,
25
+ multiple: false,
26
+ },
27
27
  ],
28
- 'options': {
29
- 'title': 'title',
30
- 'publishable': true,
31
- 'is_page': true,
32
- 'singleton': false,
33
- 'sub_title': [
34
- 'url'
35
- ],
36
- 'url_pattern': '/:title',
37
- 'url_prefix': '/'
38
- }
39
- }
40
- };
28
+ options: {
29
+ title: 'title',
30
+ publishable: true,
31
+ is_page: true,
32
+ singleton: false,
33
+ sub_title: ['url'],
34
+ url_pattern: '/:title',
35
+ url_prefix: '/',
36
+ },
37
+ },
38
+ };
@@ -4,9 +4,9 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- var supress = module.exports = function (schema, flag) {
7
+ var supress = (module.exports = function (schema, flag) {
8
8
  for (var i in schema) {
9
- if (schema[i].data_type === 'group') {
9
+ if (schema[i].data_type === 'group' || schema[i].data_type === 'global_field') {
10
10
  supress(schema[i].schema, flag);
11
11
  } else if (schema[i].data_type === 'blocks') {
12
12
  for (var block in schema[i].blocks) {
@@ -14,15 +14,21 @@ var supress = module.exports = function (schema, flag) {
14
14
  }
15
15
  } else if (schema[i].data_type === 'reference') {
16
16
  flag.references = true;
17
+ } else if (schema[i].data_type === 'json' && schema[i].field_metadata.rich_text_type) {
18
+ flag.jsonRte = true
19
+ if (schema[i].field_metadata.embed_entry === true)
20
+ flag.jsonRteEmbeddedEntries = true;
17
21
  }
18
22
 
19
- if ((schema[i].hasOwnProperty('mandatory') && schema[i].mandatory) || (schema[i].hasOwnProperty('unique') &&
20
- schema[i].unique)) {
21
- if(schema[i].uid !== 'title') {
23
+ if (
24
+ (schema[i].hasOwnProperty('mandatory') && schema[i].mandatory) ||
25
+ (schema[i].hasOwnProperty('unique') && schema[i].unique)
26
+ ) {
27
+ if (schema[i].uid !== 'title') {
22
28
  schema[i].unique = false;
23
29
  schema[i].mandatory = false;
24
- flag.supressed = true;
25
- }
30
+ flag.suppressed = true;
31
+ }
26
32
  }
27
33
  }
28
- };
34
+ });
@@ -4,45 +4,46 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- 'use strict'
7
+ 'use strict';
8
8
 
9
- var fs = require('fs')
10
- var Bluebird = require('bluebird')
11
- var request = Bluebird.promisify(require('request'))
12
- var debug = require('debug')('util:requests')
13
- var MAX_RETRY_LIMIT = 5
9
+ var debug = require('debug')('util:requests');
10
+ var MAX_RETRY_LIMIT = 5;
11
+ var Bluebird = require('bluebird');
14
12
 
15
- var util = require('./index')
16
- var config = util.getConfig()
17
- const client = require('../util/contentstack-management-sdk').Client(config)
13
+ var util = require('./index');
14
+ var config = util.getConfig();
15
+ const client = require('../util/contentstack-management-sdk').Client(config);
18
16
 
19
- function validate (req) {
17
+ function validate(req) {
20
18
  if (typeof req !== 'object') {
21
- throw new Error(`Invalid params passed for request\n${JSON.stringify(arguments)}`)
19
+ throw new Error(`Invalid params passed for request\n${JSON.stringify(arguments)}`);
22
20
  }
23
21
  }
24
22
 
25
- var upload = module.exports = function (req, fsPath, RETRY) {
23
+ var upload = (module.exports = function (req, fsPath, RETRY) {
26
24
  return new Bluebird(function (resolve, reject) {
27
25
  try {
28
- validate(req)
26
+ validate(req);
29
27
  if (typeof RETRY !== 'number') {
30
- RETRY = 1
28
+ RETRY = 1;
31
29
  } else if (RETRY > MAX_RETRY_LIMIT) {
32
- return reject(new Error('Max retry limit exceeded!'))
30
+ return reject(new Error('Max retry limit exceeded!'));
33
31
  }
34
- // var uploadStream = fs.createReadStream(fsPath)
35
-
36
- req.upload = fsPath
37
- client.stack({api_key: config.target_stack, management_token: config.management_token}).asset().create(req)
38
- .then(response => {
39
- return resolve(response)
40
- }).catch(error => {
41
- return reject(error)
42
- })
32
+
33
+ req.upload = fsPath;
34
+ client
35
+ .stack({ api_key: config.target_stack, management_token: config.management_token })
36
+ .asset()
37
+ .create(req)
38
+ .then((response) => {
39
+ return resolve(response);
40
+ })
41
+ .catch((error) => {
42
+ return reject(error);
43
+ });
43
44
  } catch (error) {
44
- debug(error)
45
- return reject(error)
45
+ debug(error);
46
+ return reject(error);
46
47
  }
47
- })
48
- };
48
+ });
49
+ });