Created
May 22, 2024 11:22
-
-
Save mohshbool/523009c66b021c4926dc7d7538d677bd to your computer and use it in GitHub Desktop.
A Node.js script to compare current installed version in your project with the latest version on the npm registry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const util = require('util'); | |
const exec = util.promisify(require('child_process').exec); | |
// ANSI escape codes for colors | |
const colors = { | |
reset: '\x1b[0m', | |
red: '\x1b[31m', | |
green: '\x1b[32m', | |
yellow: '\x1b[33m', | |
cyan: '\x1b[36m', | |
}; | |
// Function to read the package.json file | |
async function readPackageJson() { | |
const data = fs.readFileSync('package.json'); | |
return JSON.parse(data); | |
} | |
// Function to fetch the latest package version from npm | |
async function fetchLatestVersion(packageName) { | |
try { | |
const {stdout} = await exec(`npm view ${packageName} version`); | |
return stdout.trim(); | |
} catch (error) { | |
console.error( | |
`${colors.red}Failed to fetch version for ${packageName}: ${error}${colors.reset}`, | |
); | |
return null; | |
} | |
} | |
// Function to extract the major version from a version string | |
function getMajorVersion(version) { | |
return version.replace(/^[~^]/, ''); | |
} | |
// Main function to check for updates | |
async function checkForUpdates() { | |
const packageJson = await readPackageJson(); | |
const packages = { | |
...packageJson.dependencies, | |
...packageJson.devDependencies, | |
}; | |
let currentIndex = 0; | |
const packagesToUpgrade = []; | |
console.log(`${colors.cyan}Checking packages for updates...${colors.reset}`); | |
for (const [packageName, currentVersionSpec] of Object.entries(packages)) { | |
currentIndex++; | |
console.log( | |
`${colors.yellow}Checking [${currentIndex}/${ | |
Object.keys(packages).length | |
}]: ${packageName}${colors.reset}`, | |
); | |
const latestVersion = await fetchLatestVersion(packageName); | |
if ( | |
latestVersion && | |
getMajorVersion(latestVersion) !== getMajorVersion(currentVersionSpec) | |
) { | |
console.log( | |
`${colors.green}[${packageName}]: Current version spec ${currentVersionSpec}, Latest version ${latestVersion}${colors.reset}`, | |
); | |
packagesToUpgrade.push( | |
`${colors.green}${packageName}@${latestVersion}${colors.reset}`, | |
); | |
} | |
} | |
console.log( | |
'------------------------------------------------------------------------------', | |
); | |
for (const index in packagesToUpgrade) { | |
console.log(`yarn add ${packagesToUpgrade[index]}`); | |
} | |
console.log(`${colors.cyan}Check complete!${colors.reset}`); | |
} | |
// Run the check | |
checkForUpdates(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment