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

Fix Cannot read property 'statusCode' of undefined when updating #40

Merged
merged 10 commits into from
May 13, 2020
Merged
Changes from 2 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
34 changes: 23 additions & 11 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ const performUpgrade = function(config, force, callback) {
callback = force;
force = false;
}
if (force || Date.now() - (config.updatecheck || 0) > config.UPDATE_CHECK_INTERVAL) {

const updateCheckIntervalPassed = Date.now() - (config.updatecheck || 0) > config.UPDATE_CHECK_INTERVAL;
const shouldUpdate = force || updateCheckIntervalPassed;

if (!shouldUpdate) {
return callback(); // no check necessary, run rest of program
} else {
if (os.platform() === 'darwin') {
config.VERSION_CHECK_URL = config.VERSION_CHECK_URL.replace('PLATFORM', 'mac');
config.UPDATE_CHECK_URL = config.UPDATE_CHECK_URL.replace('PLATFORM', 'mac');
Expand All @@ -166,14 +172,22 @@ const performUpgrade = function(config, force, callback) {
return callback(); // ignore for now until this works
}

if (force) log('Checking for updates...');
var force_timeout_ms = 30000;
var default_timeout_ms = 2500;
request.get(config.VERSION_CHECK_URL, {
timeout: (force ? force_timeout_ms : default_timeout_ms)
}, (error, response, body) => {
if (error) return log('Error ' + response.statusCode + ': ' + error);
if (body) body = body.replace(/\r/g, '').replace(/\n/g, '');
if (force) {
log('Checking for updates...');
}

const force_timeout_ms = 30000;
smusali marked this conversation as resolved.
Show resolved Hide resolved
const default_timeout_ms = 2500;
const timeout = force ? force_timeout_ms : default_timeout_ms;

request.get(config.VERSION_CHECK_URL, { timeout }, (error, response, body) => {
if (error) {
const statusCode = response ? response.statusCode : 'no response';
return log(`Error [${statusCode}]: ${error}`);
}
if (body) {
body = body.replace(/\r/g, '').replace(/\n/g, '');
}
if (!semver.valid(body)) {
config.updatecheck = Date.now() - config.UPDATE_CHECK_INTERVAL + 86400000;
return saveConfig(config, function(error, success) {
Expand Down Expand Up @@ -203,8 +217,6 @@ const performUpgrade = function(config, force, callback) {
}
});
}

return callback(); // no check necessary, run rest of program
};

exports.saveConfig = saveConfig;
Expand Down