From 12c9d0a1a7f9524a19199c7313dbd4d683dd46f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Welte=CC=88?= Date: Wed, 21 Feb 2024 15:09:59 +1100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=94=A8=20better=20help=20text=20f?= =?UTF-8?q?or=20checkForNewRelease()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What: in scripts/requirements.js: 1) code put inside an async function so we can await another function 2) checkForRelease() now awaited and moved to end (so its output falls at the end of the execution, just like presently in scripts/release-check.js: 1) we make checkForNewRelease an async function and switch over from promise syntax (see "releaseInfo.then()..." change) 2) introduce helper to fetch from github based on a subpath 3) add new function to get the commit of the main branch in github (this seems to fix a bug where previously "target_commitish" was "main" rather than a SHA) 4) update the logging in checkForNewRelease to use colors / icons similar to how the requirements.js works (in fact we pass in checkmark icons etc). --- scripts/lib/release-check.js | 90 +++++++++++++++++++++++------------- scripts/requirements.js | 59 ++++++++++++----------- 2 files changed, 91 insertions(+), 58 deletions(-) diff --git a/scripts/lib/release-check.js b/scripts/lib/release-check.js index 31e6e797..f53bc134 100644 --- a/scripts/lib/release-check.js +++ b/scripts/lib/release-check.js @@ -18,15 +18,8 @@ function getLocalBranch() { return execSync('git branch --show-current', {encoding: 'utf8'}).trim(); } -function fetchLatestReleaseInfo() { +function fetchHelper(options) { return new Promise((resolve, reject) => { - const options = { - hostname: 'api.github.com', - path: `/repos/${user}/${repo}/releases/latest`, - method: 'GET', - headers: {'User-Agent': 'Planet 4 dev env'}, - }; - https .get(options, res => { let data = ''; @@ -45,6 +38,24 @@ function fetchLatestReleaseInfo() { }); } +function fetchGithub(subpath) { + const options = { + hostname: 'api.github.com', + path: `/repos/${user}/${repo}${subpath}`, + method: 'GET', + headers: {'User-Agent': 'Planet 4 dev env'}, + }; + return fetchHelper(options); +} + +function fetchLatestReleaseInfo() { + return fetchGithub('/releases/latest'); +} + +function fetchLatestUpstreamMainCommit() { + return fetchGithub('/branches/main'); +} + function compareVersions(versionA, versionB) { const normalizeVersion = version => version.startsWith('v') ? version.slice(1) : version; @@ -67,34 +78,49 @@ function compareVersions(versionA, versionB) { return 0; } -function checkForNewRelease() { - const releaseInfo = fetchLatestReleaseInfo().catch(err => { - console.error('Error fetching release information:', err.message); - }); +async function checkForNewRelease({ + greenCheck, orangeCross, redCross, +}) { + console.log('Inspecting your checked-out code:'); - releaseInfo.then(info => { - const latestVersion = info.tag_name; - const localVersion = getLocalVersion(); + const releaseInfo = await fetchLatestReleaseInfo().catch(err => { + console.error(`${redCross} Error fetching release information: ${err.message}`); + }); + const upstreamCommitInfo = await fetchLatestUpstreamMainCommit().catch(err => { + console.error(`${redCross} Error fetching remote commit information: ${err.message}`); + }); - if (compareVersions(localVersion, latestVersion) === -1) { - console.log( - `\nNew Planet 4 developer environment release available: ${latestVersion}` - ); - console.log('You should update !'); - } + const info = releaseInfo; + const latestVersion = info.tag_name; + const localVersion = getLocalVersion(); - const latestCommitHash = info.target_commitish; - const localCommitHash = getLocalCommitHash(); + if (compareVersions(localVersion, latestVersion) === -1) { + console.log( + `\nNew Planet 4 developer environment release available: ${latestVersion}` + ); + console.log(`${orangeCross} You should update !`); + } - if ( - getLocalBranch() === 'main' && - localCommitHash !== latestCommitHash - ) { - console.log( - `\nYour local commit (${localCommitHash}) does not match the latest release commit (${latestCommitHash})` - ); - } - }); + const latestCommitHash = upstreamCommitInfo.commit.sha; + const localCommitHash = getLocalCommitHash(); + + if ( + getLocalBranch() === 'main' && + localCommitHash !== latestCommitHash + ) { + console.log( + `\n${orangeCross} Your local commit (${localCommitHash}) does not match the latest "main" commit (${latestCommitHash})` + ); + const infoColor = '\u001b[34m'; // blue color code + const resetColor = '\u001b[0m'; // reset color code + const startBold = '\x1b[1m'; + const endBold = '\x1b[0m'; + const infoIcon = '\u2139'; + const helpText = 'You may want to update your local commit by running "git pull"'; + console.log(`➞ ${infoColor}${startBold} ${infoIcon} ${helpText} ${endBold}${resetColor}`); + } else { + console.log(`${greenCheck} Your checked out code is up to date!`); + } } module.exports = {checkForNewRelease}; diff --git a/scripts/requirements.js b/scripts/requirements.js index 68f61694..4cbeae36 100644 --- a/scripts/requirements.js +++ b/scripts/requirements.js @@ -19,29 +19,36 @@ function check(cmd, title, fail) { } } -checkForNewRelease(); - -console.log('Shell: '); -console.log(process.env.SHELL); - -console.log('Node version: ' + process.version); -nodeCheck(); - -check( - 'docker --version', - 'Docker version:', - `${redCross} Not found. Please install Docker.` -); -check( - 'docker compose version', - 'Docker compose version:', - `${redCross} Not found. Please install or activate Docker compose v2.` -); - -check('npx wp-env --version', 'wp-env version:', `${redCross} Not found. Please install wp-env.`); -wpenvCheck(); - -const nvmPath = process.env.NVM_DIR ? `${process.env.NVM_DIR}/nvm.sh` : '~/.nvm/nvm.sh'; -check(`. ${nvmPath} && nvm --version`, 'nvm version:', `${redCross} Not found (nvm path used: ${nvmPath}). Please install NVM.`); -check('curl --version', 'curl version:', `${redCross} Not found. Please install Curl.`); -check('gcloud version', 'gcloud version:', `${orangeCross} Not found. Install gcloud only if you want to import NRO database.`); +async function main() { + console.log('Shell: '); + console.log(process.env.SHELL); + + console.log('Node version: ' + process.version); + nodeCheck(); + + check( + 'docker --version', + 'Docker version:', + `${redCross} Not found. Please install Docker.` + ); + check( + 'docker compose version', + 'Docker compose version:', + `${redCross} Not found. Please install or activate Docker compose v2.` + ); + + check('npx wp-env --version', 'wp-env version:', `${redCross} Not found. Please install wp-env.`); + wpenvCheck(); + + const nvmPath = process.env.NVM_DIR ? `${process.env.NVM_DIR}/nvm.sh` : '~/.nvm/nvm.sh'; + check(`. ${nvmPath} && nvm --version`, 'nvm version:', `${redCross} Not found (nvm path used: ${nvmPath}). Please install NVM.`); + check('curl --version', 'curl version:', `${redCross} Not found. Please install Curl.`); + check('gcloud version', 'gcloud version:', `${orangeCross} Not found. Install gcloud only if you want to import NRO database.`); + console.log(); + + await checkForNewRelease({ + greenCheck, orangeCross, redCross, + }); +} + +main();