Skip to content

Instantly share code, notes, and snippets.

@Anush-DP
Last active September 11, 2024 11:39
Show Gist options
  • Save Anush-DP/64f922440dfcdc451cc4787b823d647a to your computer and use it in GitHub Desktop.
Save Anush-DP/64f922440dfcdc451cc4787b823d647a to your computer and use it in GitHub Desktop.
chromium-updater.js
/*
TO RUN:
Install nodeJS: https://nodejs.org/en/download
then run:
node chromium-updater.js
*/
const readline = require('readline');
// Set to true for ASi builds
// Set to false for Intel builds
const APPLE_SILLICON = false;
// Ignore No-Sync builds?
const SYNC_ONLY = true;
// Show notifications
// brew install terminal-notifier
const NOTIFICATIONS = true;
const https = require('https');
const fs = require('fs');
// easier to do this with a node_module such as the excellent node-fetch
// but i want this to be one file
function makeRequest(options, cb) {
var req = https.request(options, (res) => {
var data = '';
res.on('data', (d) => {
data += d.toString();
});
res.on('end', () => {
res.data = data;
cb(undefined, res);
});
});
req.on('error', (e) => {
cb(e);
});
req.end();
}
const readLineAsync = () => {
const rl = readline.createInterface({
input: process.stdin
});
return new Promise((resolve) => {
rl.prompt();
rl.on('line', (line) => {
rl.close();
resolve(line);
});
});
};
function showNotif(description, subtitle, group) {
subtitle = subtitle || '';
console.error(subtitle, description);
if (fs.existsSync('/usr/local/bin/terminal-notifier') && NOTIFICATIONS) {
require('child_process').spawn('/usr/local/bin/terminal-notifier', [
'-title',
'Chromium Updater',
'-appIcon',
'/Applications/Chromium.app/Contents/Resources/app.icns',
'-subtitle',
JSON.stringify(subtitle),
'-message',
JSON.stringify(description),
'-sender',
"'org.chromium.Chromium'",
'-activate',
"'org.chromium.Chromium'",
'-ignoreDnD',
'-group',
JSON.stringify(group || 'chromiumUpdate'),
]);
}
}
showNotif('Checking for updates');
makeRequest(
{
//https://api.github.com/repos/macchrome/chromium/releases
hostname: 'api.github.com',
port: 443,
method: 'GET',
path: '/repos/macchrome/macstable/releases',
headers: {
'User-Agent':
'chromiumUpdater/1.0 pure-node-js-http-library/' +
process.version,
},
},
async (err, res) => {
if (res.statusCode > 299 || res.statusCode < 200)
return showNotif(
'Server returned a status of ' + res.statusCode,
'Error'
);
var j = JSON.parse(res.data);
var files = [];
for (var release of j) files.push(...release.assets);
// console.log('\nFound files', files.map((f) => f.name).join(', '));
// Mac builds only
files = files.filter((f) => f.name.endsWith('.tar.xz'));
// ASi builds
files = files.filter(
(f) =>
(f.name.includes('_arm64') && APPLE_SILLICON) ||
(!f.name.includes('_arm64') && !APPLE_SILLICON)
);
// Sync only
if (SYNC_ONLY) files = files.filter((f) => f.name.includes('.sync'));
if (!files[0]) return showNotif('No eligible builds found', 'Error');
// console.log(
// '\nEligible builds are',
// files.map((f) => f.name).join(', '),
// '.\nSince',
// files[0].name,
// "is the latest, we'll use that.\n"
// );
var latestSplit = files[0].name.split('.');
var latestMajor = parseInt(latestSplit[1]);
var latestMinor = parseInt(latestSplit[3]);
if (fs.existsSync('/Applications/Chromium.app/Contents/Info.plist')) {
var localplist = fs
.readFileSync('/Applications/Chromium.app/Contents/Info.plist')
.toString();
var localVersion = localplist
.split(`<key>CFBundleShortVersionString</key>\n\t<string>`)[1]
.split('</string>')[0];
var localSplit = localVersion.split('.');
var localMajor = parseInt(localSplit[0]);
var localMinor = parseInt(localSplit[2]);
console.log('The currently installed version is', localVersion);
console.log('The latest version is', files[0].name);
console.log('Do you want to update? [y/N]');
let answer = await readLineAsync();
if(answer.toLowerCase() !== 'y') {
console.log('Not updating. Bye.');
return;
}
}
showNotif(
'Updating!'
);
fs.rmSync('/Applications/Chromium.app', {
recursive: true,
force: true,
});
require('child_process').execSync(
'curl -L ' +
JSON.stringify(files[0].browser_download_url) +
' > /tmp/chromUpdate.tar.xz && tar -xvf /tmp/chromUpdate.tar.xz -C /Applications'
);
return showNotif(
localMajor +
'.' +
localMinor +
' -> ' +
latestMajor +
'.' +
latestMinor,
'Succesfully updated 😃👍!',
'chromium_update_' + latestMajor + latestMinor
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment