Skip to content

Commit 367ec21

Browse files
author
Aamod Pisat
committed
Remove 'publish_details' deprecated query for entry from 1.0.x
remove whitespaces from api_key and access_token added user-agent added note in commands and README.md
1 parent 60ed958 commit 367ec21

File tree

1 file changed

+86
-50
lines changed

1 file changed

+86
-50
lines changed

lib/sync.js

Lines changed: 86 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ util.inherits(Sync, EventEmitter);
7272
module.exports = Sync;
7373

7474
/*
75-
* Get the user inputs
76-
* */
77-
Sync.prototype.initialise = function(cmdInputs) {
75+
* Get the user inputs
76+
* */
77+
Sync.prototype.initialise = function (cmdInputs) {
7878
try {
7979
var self = this,
8080
inputs = [],
8181
fn;
8282

8383
self.inputs = {};
8484

85-
backup = utility.matchConfirm(cmdInputs['backup']);
85+
backup = utility.matchConfirm(cmdInputs['backup']);
8686
delete cmdInputs['backup'];
8787

88-
for(var key in cmdInputs) {
89-
if(key && typeof self._options[key] === 'undefined') {
88+
for (var key in cmdInputs) {
89+
if (key && typeof self._options[key] === 'undefined') {
9090
switch (key) {
9191
case 'environment':
9292
fn = utility.inputEnvironment(utility.inputs.environment());
@@ -98,23 +98,42 @@ Sync.prototype.initialise = function(cmdInputs) {
9898
fn = utility.inputCustom(utility.inputs.content_types());
9999
break;
100100
case 'skip_content_types':
101-
fn = utility.inputCustom(utility.inputs.content_types({name: "skip_content_types", description: "Enter the content types to be skipped(hit return/enter for none or type \",\" comma seperated content type uids): "}));
101+
fn = utility.inputCustom(utility.inputs.content_types({
102+
name: "skip_content_types",
103+
description: "Enter the content types to be skipped(hit return/enter for none or type \",\" comma seperated content type uids): "
104+
}));
102105
break;
103106
case 'datetime':
104-
fn = utility.inputCustom(utility.inputs.custom({name: "datetime", format: "date-time", description: "Enter the date-time(ISO String Format) from where you want to synchronize your data(hit return/enter for all data): ", conform: function(input) { if(Date.parse(input) != "NaN") { return true;} return false; }, before: function(input) { if(input && Date.parse(input) != "NaN") { return new Date(input).toISOString(); } return input; }}));
107+
fn = utility.inputCustom(utility.inputs.custom({
108+
name: "datetime",
109+
format: "date-time",
110+
description: "Enter the date-time(ISO String Format) from where you want to synchronize your data(hit return/enter for all data): ",
111+
conform: function (input) {
112+
if (Date.parse(input) != "NaN") {
113+
return true;
114+
}
115+
return false;
116+
},
117+
before: function (input) {
118+
if (input && Date.parse(input) != "NaN") {
119+
return new Date(input).toISOString();
120+
}
121+
return input;
122+
}
123+
}));
105124
break;
106125
}
107-
if(fn) inputs.push(fn);
126+
if (fn) inputs.push(fn);
108127
} else {
109-
if(key === 'environment' && self._options[key]) {
110-
inputs.push((function(environment){
128+
if (key === 'environment' && self._options[key]) {
129+
inputs.push((function (environment) {
111130
return function (callback) {
112131
utility
113132
.getEnvironment(environment)
114-
.then(function(result) {
133+
.then(function (result) {
115134
callback(null, result);
116135
})
117-
.fail(function(err) {
136+
.fail(function (err) {
118137
callback(err);
119138
});
120139
}
@@ -125,51 +144,58 @@ Sync.prototype.initialise = function(cmdInputs) {
125144
}
126145
}
127146
async
128-
.series(inputs, function(err, result) {
147+
.series(inputs, function (err, result) {
129148
try {
130-
if(err) throw err;
131-
for(var i = 0, total = result.length; i < total; i++) {
132-
if(result[i] && typeof result[i] == "object") {
133-
for(var key in result[i]) {
149+
if (err) throw err;
150+
for (var i = 0, total = result.length; i < total; i++) {
151+
if (result[i] && typeof result[i] == "object") {
152+
for (var key in result[i]) {
134153
self.inputs[key] = result[i][key];
135154
}
136155
}
137156
}
138-
helper.confirm(config, self.inputs.language, backup, function(err) {
139-
if(err) throw err;
157+
helper.confirm(config, self.inputs.language, backup, function (err) {
158+
if (err) throw err;
140159
self.loadData();
141160
});
142161
} catch (err) {
143162
console.error("Init Error : ", err);
144163
}
145164
});
146-
} catch(err) {
165+
} catch (err) {
147166
console.error("Init Error : ", err);
148167
}
149168
};
150169

151170
/*
152-
* Load all the entries from the content_types
153-
* */
154-
Sync.prototype.loadData = function() {
171+
* Load all the entries from the content_types
172+
* */
173+
Sync.prototype.loadData = function () {
155174
var self = this,
156175
_calls = [];
157176

158-
var _loadEntries = function(_content_types) {
159-
for(var i = 0, total = _content_types.length; i < total; i++) {
160-
_calls.push(function(content_type) {
177+
var _loadEntries = function (_content_types) {
178+
for (var i = 0, total = _content_types.length; i < total; i++) {
179+
_calls.push(function (content_type) {
161180
return utility.getEntries(content_type, self.inputs.language, {}, ['_metadata'], self.inputs.environment.name)
162-
.then(function(entries) {
181+
.then(function (entries) {
163182
var _entries = [];
164183
if (self.inputs.datetime) {
165184
for (var i = 0; i < entries.length; i++) {
166185
if (entries[i] && entries[i]['_metadata']['publish_details']) {
167-
var idx = _.findIndex(entries[i]['_metadata']['publish_details'], function (object) {
168-
if (object.locale === self.inputs.language && object.environment === self.inputs.environment.uid && object.time && object.time >= self.inputs.datetime) {
186+
if (entries[i]['_metadata']['publish_details'] instanceof Array) {
187+
var idx = _.findIndex(entries[i]['_metadata']['publish_details'], function (object) {
188+
if (object.locale === self.inputs.language && object.environment === self.inputs.environment.uid && object.time && object.time >= self.inputs.datetime) {
189+
_entries.push(entries[i]);
190+
return true;
191+
}
192+
});
193+
} else {
194+
var _publishDetailsObj = entries[i]['_metadata']['publish_details'];
195+
if (_publishDetailsObj['locale'] === self.inputs.language && _publishDetailsObj['environment'] === self.inputs.environment.uid && _publishDetailsObj['time'] && _publishDetailsObj['time'] >= self.inputs.datetime) {
169196
_entries.push(entries[i]);
170-
return true;
171197
}
172-
});
198+
}
173199
}
174200
}
175201
} else {
@@ -178,41 +204,42 @@ Sync.prototype.loadData = function() {
178204
console.log("Total %d entries of ContentType %s retrieved.", _entries.length, content_type);
179205
return self.parseEntries(_entries, content_type);
180206
})
181-
.fail(function(err) {
207+
.fail(function (err) {
182208
console.log("Error in retriveing entries: ", err, content_type);
183209
});
184210
}(_content_types[i]));
185-
};
211+
}
212+
;
186213
Q.all(_calls)
187-
.then(function(entries) {
188-
entries = entries.reduce(function(prev, crnt) {
189-
if(Array.isArray(crnt)) {
214+
.then(function (entries) {
215+
entries = entries.reduce(function (prev, crnt) {
216+
if (Array.isArray(crnt)) {
190217
prev = prev.concat(crnt);
191218
}
192219
return prev;
193220
}, []);
194221
console.log("Total entries %d are synchronized.", entries.length);
195222
})
196-
.fail(function(error) {
223+
.fail(function (error) {
197224
console.log("Synchronization error: ", error.message);
198225
});
199226
};
200227

201228
// calculating the content_types to be used
202-
if(self.inputs.content_types.length) {
229+
if (self.inputs.content_types.length) {
203230
self.inputs.content_types = _.difference(self.inputs.content_types, self.inputs.skip_content_types);
204231
_loadEntries(self.inputs.content_types);
205232
} else {
206233
utility
207234
.getContentTypes()
208-
.then(function(content_types) {
209-
if(content_types && content_types.content_types && content_types.content_types.length) {
235+
.then(function (content_types) {
236+
if (content_types && content_types.content_types && content_types.content_types.length) {
210237
content_types = _.difference(_.pluck(content_types.content_types, "uid"), self.inputs.skip_content_types);
211238
_loadEntries(content_types);
212239
} else {
213240
console.log("No ContentTypes found.");
214241
}
215-
}).fail(function(error) {
242+
}).fail(function (error) {
216243
console.log("ContentTypes retrieval error: ", error.message);
217244
});
218245
}
@@ -221,22 +248,31 @@ Sync.prototype.loadData = function() {
221248
/*
222249
* Load all the parse Entries
223250
* */
224-
Sync.prototype.parseEntries = function(entries, content_type) {
251+
Sync.prototype.parseEntries = function (entries, content_type) {
225252
try {
226253
var self = this;
227254
var deferred = Q.defer();
228255
console.log("Restoring %d entries of %s ContentType.", entries.length, content_type);
229256
for (var i = 0, total = entries.length; i < total; i++) {
230-
if(entries[i] && entries[i]['_metadata'] && entries[i]['_metadata']['publish_details']) {
231-
var idx = _.findIndex(entries[i]['_metadata']['publish_details'], {
232-
environment: self.inputs.environment.uid,
233-
locale: self.inputs.language
234-
});
257+
if (entries[i] && entries[i]['_metadata'] && entries[i]['_metadata']['publish_details']) {
258+
var idx;
259+
if (entries[i]['_metadata']['publish_details'] instanceof Array) {
260+
idx = _.findIndex(entries[i]['_metadata']['publish_details'], {
261+
environment: self.inputs.environment.uid,
262+
locale: self.inputs.language
263+
});
264+
} else {
265+
if (entries[i]['_metadata']['publish_details']['environment'] === self.inputs.environment.uid && entries[i]['_metadata']['publish_details']['locale'] === self.inputs.language) {
266+
idx = 1;
267+
} else {
268+
idx = -1;
269+
}
270+
}
235271
var _lang = _.findIndex(languages, {'code': self.inputs.language});
236-
if(~idx && ~_lang) {
272+
if (~idx && ~_lang) {
237273
q.push({
238274
message: {
239-
body:{
275+
body: {
240276
object: {
241277
form: {
242278
title: content_type,

0 commit comments

Comments
 (0)