Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook to check for iOS updates #168

Merged
merged 1 commit into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@


<platform name="ios">
<hook type="before_build" src="scripts/checkForUpdate.js" />

<header-file src="src/ios/IntercomBridge.h" />
<source-file src="src/ios/IntercomBridge.m" />

Expand Down
107 changes: 107 additions & 0 deletions scripts/checkForUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
function fetchUpdateInfo(context, callback) {
var fs = context.requireCordovaModule('fs');

var updateInfo = {
releaseDate: 0,
podUpdateDate: 0,
lastCheckDate: 0
};

fs.readFile('platforms/ios/.intercom_update', 'utf8', function (err, data) {
if (!err) {
obj = JSON.parse(data);
updateInfo.podUpdateDate = obj.podUpdateDate;
updateInfo.releaseDate = obj.releaseDate;
updateInfo.lastCheckDate = obj.lastCheckDate;
}
callback(updateInfo);
});
}

function writeUpdateInfo(context, updateInfo, callback) {
var fs = context.requireCordovaModule('fs');
fs.writeFile('platforms/ios/.intercom_update', JSON.stringify(updateInfo), 'utf8', function (err,data) {
callback();
});
}

function updateIntercomIfNeeded(context, updateInfo, callback) {
var exec = context.requireCordovaModule('child_process').exec;

var completion = function() {
writeUpdateInfo(context, updateInfo, function() {
callback();
});
};

if (updateInfo.releaseDate > updateInfo.podUpdateDate) {
console.log("Updating Intercom");
exec('cd platforms/ios && pod update Intercom', function(error, stdout, stderr) {
if (!error) {
console.log(stdout);
updateInfo.podUpdateDate = Date.now();
}
completion();
});
} else {
completion();
}
}

function fetchLatestRelease(context, callback) {
var https = context.requireCordovaModule('https');

var req = https.get({
headers: {
accept: 'application/json',
'User-Agent': 'Intercom-Cordova-Plugin'
},
host: 'api.github.com',
path: '/repos/intercom/intercom-ios/releases/latest'
}, function(response) {
if (response.statusCode != 200) {
callback(null);
return;
}

var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
var parsedResponse = JSON.parse(body);
callback(parsedResponse);
});
});
req.on('error', function(e) {
callback(null);
});
}

module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();

fetchUpdateInfo(context, function(updateInfo) {
// Check at most once every 48 hours
if (Date.now() - updateInfo.lastCheckDate > 1000 * 60 * 60 * 48) {
fetchLatestRelease(context, function(releaseData) {
updateInfo.lastCheckDate = Date.now();

if (releaseData != null) {
updateInfo.releaseDate = Date.parse(releaseData['published_at']);
updateIntercomIfNeeded(context, updateInfo, function() {
deferral.resolve();
});
} else {
writeUpdateInfo(context, updateInfo, function() {
deferral.resolve();
});
}
});
} else {
deferral.resolve();
}
});
return deferral.promise;
}