Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 58 additions & 32 deletions scripts/lib/release-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -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;
Expand All @@ -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};
59 changes: 33 additions & 26 deletions scripts/requirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();