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();