From 3221677bd31f41fe952e6eb76fa4702de42745a6 Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Sun, 22 Sep 2024 16:23:55 +0200 Subject: [PATCH] Use version checker to inform customers that they may be running an outdated version of Makerkit --- tooling/version/package.json | 8 +++++ tooling/version/src/index.mjs | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tooling/version/package.json create mode 100644 tooling/version/src/index.mjs diff --git a/tooling/version/package.json b/tooling/version/package.json new file mode 100644 index 000000000..7c4fa24ee --- /dev/null +++ b/tooling/version/package.json @@ -0,0 +1,8 @@ +{ + "name": "version", + "private": true, + "version": "0.1.0", + "scripts": { + "dev": "node src/index.mjs" + } +} diff --git a/tooling/version/src/index.mjs b/tooling/version/src/index.mjs new file mode 100644 index 000000000..dee32bbfc --- /dev/null +++ b/tooling/version/src/index.mjs @@ -0,0 +1,55 @@ +import { execSync } from 'node:child_process'; + +function runGitCommand(command) { + try { + return execSync(command, { encoding: 'utf8', stdio: 'pipe' }).trim(); + } catch (error) { + return null; + } +} + +function checkMakerkitVersion() { + // Fetch the latest changes from upstream without merging + const fetchResult = runGitCommand('git fetch upstream'); + + if (fetchResult === null) { + console.info( + '\x1b[33m%s\x1b[0m', "⚠️ You have not setup 'upstream'. Please set up the upstream remote so you can update your Makerkit version.", + ); + + return; + } + + // Get the number of commits the local branch is behind upstream + const behindCount = runGitCommand('git rev-list --count HEAD..upstream/main'); + + if (behindCount === null) { + console.warn( + "Failed to get commit count. Ensure you're on a branch that tracks upstream/main.", + ); + + return; + } + + const count = parseInt(behindCount, 10); + + if (count > 0) { + console.log('\x1b[33m%s\x1b[0m', '⚠️ Your Makerkit version is outdated!'); + + console.log( + '\x1b[33m%s\x1b[0m', + `You are ${count} commit(s) behind the latest version.`, + ); + + console.log( + '\x1b[33m%s\x1b[0m', + 'Please consider updating to the latest version for the best experience.', + ); + + console.log('\x1b[36m%s\x1b[0m', 'To update, run: git pull upstream main'); + } else { + console.log('\x1b[32m%s\x1b[0m', '✅ Your Makerkit version is up to date!'); + } +} + +checkMakerkitVersion(); \ No newline at end of file