@contentstack/cli-cm-import 0.1.1-beta.1 → 0.1.1-beta.12

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.
@@ -4,170 +4,168 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- var path = require('path');
8
- var _ = require('lodash');
7
+ // eslint-disable-next-line unicorn/filename-case
8
+ var path = require('path')
9
+ var _ = require('lodash')
9
10
 
10
- var util = require('./index');
11
- var helper = require('./fs');
12
- var config = util.getConfig();
11
+ var util = require('.')
12
+ var helper = require('./fs')
13
+ var config = util.getConfig()
13
14
  // update references in entry object
14
15
  module.exports = function (data, mappedUids, uidMapperPath) {
15
- var parent = [];
16
- var uids = [];
17
- var unmapped = [];
18
- var mapped = [];
16
+ var parent = []
17
+ var uids = []
18
+ var unmapped = []
19
+ var mapped = []
19
20
 
20
- var isNewRefFields = false;
21
- var preserveStackVersion = config.preserveStackVersion;
21
+ var isNewRefFields = false
22
+ var preserveStackVersion = config.preserveStackVersion
22
23
 
23
24
  var update = function (parent, form_id, entry) {
24
- var _entry = entry,
25
- len = parent.length;
25
+ var _entry = entry
26
+ var len = parent.length
26
27
  for (var j = 0; j < len; j++) {
27
28
  if (_entry && parent[j]) {
28
- if (j == (len - 1) && _entry[parent[j]]) {
29
+ if (j === (len - 1) && _entry[parent[j]]) {
29
30
  if (form_id !== '_assets') {
30
31
  if (_entry[parent[j]].length) {
31
32
  _entry[parent[j]].forEach((item, idx) => {
32
33
  if (typeof item.uid === 'string' && item._content_type_uid) {
33
- uids.push(item.uid);
34
+ uids.push(item.uid)
34
35
  } else if (typeof item === 'string' && (preserveStackVersion === true)) {
35
- uids.push(item);
36
+ uids.push(item)
36
37
  } else {
37
- uids.push(item);
38
+ uids.push(item)
38
39
  _entry[parent[j]][idx] = {
39
40
  uid: item,
40
- _content_type_uid: form_id
41
- };
41
+ _content_type_uid: form_id,
42
+ }
42
43
  }
43
- });
44
+ })
44
45
  }
45
- } else {
46
- if (_entry[parent[j]] instanceof Array) {
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']);
50
- }
51
- }
52
- } else {
53
- if (_entry[parent[j]]['uid'].length) {
54
- uids.push(_entry[parent[j]]['uid']);
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)
55
50
  }
56
51
  }
52
+ } else if (_entry[parent[j]].uid.length) {
53
+ uids.push(_entry[parent[j]].uid)
57
54
  }
58
55
  } else {
59
- _entry = _entry[parent[j]];
60
- var _keys = _.clone(parent).splice(eval(j + 1), len);
61
- if (_entry instanceof Array) {
56
+ _entry = _entry[parent[j]]
57
+ var _keys = _.clone(parent).splice(eval(j + 1), len)
58
+ if (Array.isArray(_entry)) {
62
59
  for (var i = 0, _i = _entry.length; i < _i; i++) {
63
- update(_keys, form_id, _entry[i]);
60
+ update(_keys, form_id, _entry[i])
64
61
  }
65
62
  } else if (!(_entry instanceof Object)) {
66
- break;
63
+ break
67
64
  }
68
65
  }
69
66
  }
70
67
  }
71
- };
68
+ }
72
69
  var find = function (schema, entry) {
73
70
  for (var i = 0, _i = schema.length; i < _i; i++) {
74
71
  switch (schema[i].data_type) {
75
72
  case 'reference':
76
- if (schema[i].reference_to instanceof Array) {
77
- isNewRefFields = true;
78
- schema[i].reference_to.forEach((reference) => {
79
- parent.push(schema[i].uid);
80
- update(parent, reference, entry);
81
- parent.pop();
82
- });
73
+ if (Array.isArray(schema[i].reference_to)) {
74
+ isNewRefFields = true
75
+ schema[i].reference_to.forEach(reference => {
76
+ parent.push(schema[i].uid)
77
+ update(parent, reference, entry)
78
+ parent.pop()
79
+ })
83
80
  } else {
84
- parent.push(schema[i].uid);
85
- update(parent, schema[i].reference_to, entry);
86
- parent.pop();
81
+ parent.push(schema[i].uid)
82
+ update(parent, schema[i].reference_to, entry)
83
+ parent.pop()
87
84
  }
88
- break;
85
+ break
89
86
  case 'group':
90
- parent.push(schema[i].uid);
91
- find(schema[i].schema, entry);
92
- parent.pop();
93
- break;
87
+ parent.push(schema[i].uid)
88
+ find(schema[i].schema, entry)
89
+ parent.pop()
90
+ break
94
91
  case 'blocks':
95
92
  for (var j = 0, _j = schema[i].blocks.length; j < _j; j++) {
96
- parent.push(schema[i].uid);
97
- parent.push(schema[i].blocks[j].uid);
98
- find(schema[i].blocks[j].schema, entry);
99
- parent.pop();
100
- parent.pop();
93
+ parent.push(schema[i].uid)
94
+ parent.push(schema[i].blocks[j].uid)
95
+ find(schema[i].blocks[j].schema, entry)
96
+ parent.pop()
97
+ parent.pop()
101
98
  }
102
- break;
99
+ break
103
100
  }
104
101
  }
105
- };
106
- find(data.content_type.schema, data.entry);
102
+ }
103
+ find(data.content_type.schema, data.entry)
107
104
  if (isNewRefFields) {
108
- findUidsInNewRefFields(data.entry, uids);
105
+ findUidsInNewRefFields(data.entry, uids)
109
106
  }
110
- uids = _.flattenDeep(uids);
111
- // if no references are found, return
107
+ uids = _.flattenDeep(uids)
108
+ // if no references are found, return
112
109
  if (uids.length === 0) {
113
- return data.entry;
110
+ return data.entry
114
111
  }
115
112
 
116
- uids = _.uniq(uids);
117
- var entry = JSON.stringify(data.entry);
118
- uids.forEach(function (uid) {
113
+ uids = _.uniq(uids)
114
+ var entry = JSON.stringify(data.entry)
115
+ uids.forEach(function (uid) {
119
116
  if (mappedUids.hasOwnProperty(uid)) {
120
- entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid]);
121
- mapped.push(uid);
117
+ entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid])
118
+ mapped.push(uid)
122
119
  } else {
123
- unmapped.push(uid);
120
+ unmapped.push(uid)
124
121
  }
125
- });
122
+ })
126
123
 
127
- if (unmapped.length) {
128
- var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'));
129
- unmappedUids = unmappedUids || {};
124
+ if (unmapped.length > 0) {
125
+ var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'))
126
+ unmappedUids = unmappedUids || {}
130
127
  if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
131
- unmappedUids[data.content_type.uid][data.entry.uid] = unmapped;
128
+ unmappedUids[data.content_type.uid][data.entry.uid] = unmapped
132
129
  } else {
133
130
  unmappedUids[data.content_type.uid] = {
134
- [data.entry.uid]: unmapped
135
- };
131
+ [data.entry.uid]: unmapped,
132
+ }
136
133
  }
137
134
  // write the unmapped contents to ./mapper/language/unmapped-uids.json
138
- helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids);
135
+ helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids)
139
136
  }
140
137
 
141
- if (mapped.length) {
142
- var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'));
143
- _mappedUids = _mappedUids || {};
138
+ if (mapped.length > 0) {
139
+ var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'))
140
+ _mappedUids = _mappedUids || {}
144
141
  if (_mappedUids.hasOwnProperty(data.content_type.uid)) {
145
- _mappedUids[data.content_type.uid][data.entry.uid] = mapped;
142
+ _mappedUids[data.content_type.uid][data.entry.uid] = mapped
146
143
  } else {
147
144
  _mappedUids[data.content_type.uid] = {
148
- [data.entry.uid]: mapped
149
- };
145
+ [data.entry.uid]: mapped,
146
+ }
150
147
  }
151
148
  // write the mapped contents to ./mapper/language/mapped-uids.json
152
- helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids);
149
+ helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids)
153
150
  }
154
151
 
155
- return JSON.parse(entry);
156
- };
152
+ return JSON.parse(entry)
153
+ }
157
154
 
158
155
  function findUidsInNewRefFields(entry, uids) {
159
156
  if (entry && typeof entry === 'object') {
160
157
  if (entry.uid && entry._content_type_uid) {
161
- uids.push(entry.uid);
162
- } else if (entry instanceof Array && entry.length) {
163
- entry.forEach(function(elem) {
164
- findUidsInNewRefFields(elem, uids);
165
- });
158
+ uids.push(entry.uid)
159
+ } else if (Array.isArray(entry) && entry.length) {
160
+ entry.forEach(function (elem) {
161
+ findUidsInNewRefFields(elem, uids)
162
+ })
166
163
  } else if (Object.keys(entry).length) {
167
164
  for (var key in entry) {
168
- findUidsInNewRefFields(entry[key], uids);
165
+ if (key) {
166
+ findUidsInNewRefFields(entry[key], uids)
167
+ }
169
168
  }
170
169
  }
171
170
  }
172
- return;
173
- }
171
+ }
@@ -4,7 +4,7 @@ var _ = require('lodash');
4
4
 
5
5
  /* eslint-disable no-empty */
6
6
  var removeReferenceFields = module.exports = function (schema, flag) {
7
- for (var i in schema) {
7
+ for (let i = 0; i < schema.length; i++) {
8
8
  if (schema[i].data_type === 'group') {
9
9
  removeReferenceFields(schema[i].schema, flag);
10
10
  } else if (schema[i].data_type === 'blocks') {
@@ -13,10 +13,8 @@ var removeReferenceFields = module.exports = function (schema, flag) {
13
13
  }
14
14
  } else if(schema[i].data_type === 'reference') {
15
15
  flag.supressed = true;
16
- _.remove(schema, function(c) {
17
- return c.data_type === 'reference';
18
- });
19
-
16
+ schema.splice(i, 1);
17
+ --i;
20
18
  if(schema.length < 1) {
21
19
  schema.push({
22
20
  'data_type': 'text',
@@ -21,7 +21,7 @@ var supress = module.exports = function (schema, flag) {
21
21
  if(schema[i].uid !== 'title') {
22
22
  schema[i].unique = false;
23
23
  schema[i].mandatory = false;
24
- flag.supressed = true;
24
+ flag.suppressed = true;
25
25
  }
26
26
  }
27
27
  }