Skip to content

Commit

Permalink
Merge pull request #115 from UofGAnalytics/hotfix-use-authenticated-g…
Browse files Browse the repository at this point in the history
…it-to-check-for-new-version

use authenticated git to check for new version
  • Loading branch information
dmca-glasgow committed Jun 24, 2022
2 parents 4deef7e + 0a314ce commit 7eac889
Showing 1 changed file with 68 additions and 19 deletions.
87 changes: 68 additions & 19 deletions compiler/src/utils/check-for-latest-version.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,88 @@
import { spawn } from 'child_process';
import { EOL } from 'os';

import chalk from 'chalk';
import figureSet from 'figures';
import fetch from 'node-fetch';

const repo = 'UofGAnalytics/build-coursework';

type ReleaseInfo = {
tag_name?: string;
};

export async function checkForLatestVersion() {
if (process.env.NODE_ENV === 'test') {
return;
}
const response = await fetch(
`https://api.github.com/repos/${repo}/releases/latest`
);
const json = (await response.json()) as ReleaseInfo;

const currentVersion = process.env.VERSION;

if (json.tag_name === undefined) {
try {
const tags = await listRemoteGitTags();
const latestTag = parseLatestTag(tags);

if (latestTag !== currentVersion) {
console.log(chalk.yellow.bold('New version available'));
console.log(chalk.yellow(`Current version: ${currentVersion}`));
console.log(chalk.yellow(`Latest version: ${latestTag}`));
console.log(chalk.yellow(`Run the following command to update:`));
console.log(chalk.yellow(`npm install -g ${repo}`));
console.log('');
} else {
// console.log(chalk.yellow(`Up to date :)`));
}
} catch (err) {
const message = `Can't read latest version from Github`;
console.log(chalk.yellow.bold(`${figureSet.warning} ${message}`));
console.log(chalk.yellow(`Current version: ${currentVersion}`));
console.log('');
return;
}
}

const latestTag = json.tag_name.replace('v', '');
async function listRemoteGitTags() {
return new Promise<string>((resolve, reject) => {
// https://stackoverflow.com/questions/10649814#12704727
const lsRemote = spawn('git', [
'-c',
'versionsort.suffix=-',
'ls-remote',
'--tags',
'--sort=v:refname',
`https://github.com/${repo}`,
]);

if (latestTag !== currentVersion) {
console.log(chalk.yellow.bold('New version available'));
console.log(chalk.yellow(`Current version: ${currentVersion}`));
console.log(chalk.yellow(`Latest version: ${latestTag}`));
console.log(chalk.yellow(`Run the following command to update:`));
console.log(chalk.yellow(`npm install -g ${repo}`));
console.log('');
const result: string[] = [];

lsRemote.stdout.on('data', (data) => {
const line = data.toString();
// console.log('LINE', line);
result.push(line);
});

lsRemote.stdout.on('end', () => {
// console.log('STDOUT END');
const end = result.join('').trim();
// console.log('END', end);
resolve(end);
});

lsRemote.stdout.on('error', (err) => {
console.error('[get-latest-version]:', 'STDOUT error', err.message);
reject();
});

lsRemote.stderr.on('data', (data) => {
const str = data.toString();
console.error('[get-latest-version]:', 'STDERR', str);
reject();
});
});
}

function parseLatestTag(tags: string) {
const lines = tags.split(EOL);
const lastLine = lines[lines.length - 1];
const match = lastLine.match(/tags\/v(\d+.\d+.\d+)/);
if (match === null) {
const message = `can't extract version from line: "${lastLine}"`;
console.error('[get-latest-version]:', message);
throw new Error(message);
}
return match[1];
}

0 comments on commit 7eac889

Please sign in to comment.