Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 52 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,30 @@ ContentfulPull.prototype.sync = function(options) {
var _this = this;

console.log("ContentfulPull | Syncing...");
console.log(_this.settings);

var d = Promise.defer();
return new Promise(function(resolve, reject){
var client = contentful.createClient({
space: _this.settings.space,
accessToken: _this.settings.accessToken
})

var client = contentful.createClient({
space: this.settings.space,
accessToken: this.settings.accessToken
})
var isInitial = _this.currentSyncToken ? false : true;

var isInitial = this.currentSyncToken ? false : true;
var spacePromise = client.getSpace();
var contentTypesPromise = client.getContentTypes();
var syncPromise = client.sync({initial: isInitial, resolveLinks: false, nextSyncToken: _this.currentSyncToken});

var spacePromise = client.getSpace();
var contentTypesPromise = client.getContentTypes();
var syncPromise = client.sync({initial: isInitial, resolveLinks: false, nextSyncToken: this.currentSyncToken});
Promise.all([spacePromise, contentTypesPromise, syncPromise]).then(function(result) {
var handledData = _this.handleSyncResponse({
space: result[0],
contentTypes: result[1],
sync: result[2]
});

Promise.all([spacePromise, contentTypesPromise, syncPromise]).then(function(result) {
var handledData = _this.handleSyncResponse({
space: result[0],
contentTypes: result[1],
sync: result[2]
resolve(_this.transformData(handledData, options));
});

d.resolve(_this.transformData(handledData, options));
});

return d.promise;
};

/*
Expand Down Expand Up @@ -199,18 +198,16 @@ ContentfulPull.prototype.transformData = function(data, options) {
*/
ContentfulPull.prototype.getFromFile = function(options) {
var _this = this;
var d = Promise.defer();

fs.readFile(this.settings.path, "utf8", function(err, resp) {
if (err) d.reject(err);
if (!err) {
_this.data = JSON.parse(resp);
_this.currentSyncToken = _this.data.sync.currentSyncToken;
d.resolve(_this.transformData(_this.data, options));
}
})

return d.promise;
return new Promise(function(resolve, reject){
fs.readFile(_this.settings.path, "utf8", function(err, resp) {
if (err) reject(err);
if (!err) {
_this.data = JSON.parse(resp);
_this.currentSyncToken = _this.data.sync.currentSyncToken;
resolve(_this.transformData(_this.data, options));
}
})
});
}

/*
Expand All @@ -219,45 +216,43 @@ ContentfulPull.prototype.getFromFile = function(options) {
*/
ContentfulPull.prototype.get = function(options) {
var _this = this;
var d = Promise.defer();

// If no data is in memory, get from file, if that's not existing, sync from contentful
if (!this.data) {
this.getFromFile(options).then(d.resolve).catch(function() {
_this.sync(options).then(d.resolve);
})
}else{
return new Promise(function(resolve, reject) {
resolve(_this.transformData(_this.data, options));
})
}
return new Promise(function(resolve, reject){

return d.promise;
// If no data is in memory, get from file, if that's not existing, sync from contentful
if (!_this.data) {
_this.getFromFile(options).then(resolve).catch(function() {
_this.sync(options).then(resolve);
})
}else{
return new Promise(function(resolve, reject) {
resolve(_this.transformData(_this.data, options));
})
}
});
}

/*
ContentfulSynchronize.saveLocal
Saves the data to a local file
*/
ContentfulPull.prototype.saveLocal = function(data) {
var d = Promise.defer();

console.log("ContentfulPull | Saving to local file...");
var _this = this;

// Write to local storage
fs.writeFile(this.settings.path, JSON.stringify(data), "utf8", function(err) {
if (err) {
console.log("ContentfulPull | An error occurred while saving local file.");
console.log(err.stack);
d.reject();
}
if (!err) {
console.log("ContentfulPull | File saved successfully.");
d.resolve();
}
return new Promise(function(resolve, reject){
fs.writeFile(_this.settings.path, JSON.stringify(data), "utf8", function(err) {
if (err) {
console.log("ContentfulPull | An error occurred while saving local file.");
console.log(err.stack);
reject();
}
if (!err) {
console.log("ContentfulPull | File saved successfully.");
resolve();
}
});
});

return d.promise;
};

module.exports = ContentfulPull;