Skip to content

Instantly share code, notes, and snippets.

@Tuizi
Last active September 28, 2023 12:21
Show Gist options
  • Save Tuizi/a0314364045b0f0a1882a10f53bb4466 to your computer and use it in GitHub Desktop.
Save Tuizi/a0314364045b0f0a1882a10f53bb4466 to your computer and use it in GitHub Desktop.
Automated Dependency Updater with Tests: A Node.js script that updates npm packages in your project, runs tests, and reverts updates if tests fail. Generated with the help of ChatGPT by OpenAI.

Automated Dependency Updater with Tests

This Node.js script automates the process of updating your project's dependencies while also running tests to ensure that each update doesn't break your code.

Generated with the help of ChatGPT by OpenAI.

Features

  • Checks for outdated npm packages in your project.
  • Sorts the packages by update type (patch, minor, major).
  • Updates each package one by one.
  • Runs your test suite after each update.
  • If a test fails, reverts the package to its previous version and continues with the next package.

How to Use

  1. Place the updateAndTestDependencies.js script in your project folder.

  2. Make sure you have yarn and Node.js installed.

  3. Run the script with:

    node updateAndTestDependencies.js
  4. Optionally, you can customize the test command by modifying the TEST_COMMAND constant in the script.

Enjoy automated, worry-free dependency updates!

/* eslint-disable */
const TEST_COMMAND = 'yarn test'; // Place your custom test command here
const { execSync } = require('child_process');
const semver = require('semver');
function getOutdatedPackages() {
let rawOutput;
try {
rawOutput = execSync('yarn outdated --json', { stdio: 'pipe' }).toString();
} catch (error) {
rawOutput = error.stdout.toString();
}
const lines = rawOutput.trim().split('\n');
let outdatedPackages = [];
for (let line of lines) {
let parsed;
try {
parsed = JSON.parse(line);
} catch (error) {
continue;
}
if (parsed.type === 'table') {
outdatedPackages = parsed.data.body;
break;
}
}
return outdatedPackages;
}
const order = ['patch', 'minor', 'major'];
function sortPackages(outdatedPackages) {
return outdatedPackages
.map(([name, current, wanted, latest]) => ({ name, current, latest }))
.sort((a, b) => {
const typeA = semver.diff(a.current, a.latest);
const typeB = semver.diff(b.current, b.latest);
return order.indexOf(typeA) - order.indexOf(typeB);
});
}
function updateAndTestPackages(sortedPackages) {
for (const { name, current, latest } of sortedPackages) {
console.log(`Updating ${name} to ${latest}`);
try {
execSync(`yarn upgrade ${name}@${latest}`, { stdio: 'ignore' });
console.log(`Running tests for ${name}`);
execSync(TEST_COMMAND, { stdio: 'ignore' });
} catch (error) {
console.error(
`Test or update failed for ${name}. Reverting to ${current}.`,
);
execSync(`yarn add ${name}@${current}`, { stdio: 'ignore' });
}
}
}
function main() {
console.log("Running 'yarn outdated'...");
const outdatedPackages = getOutdatedPackages();
const sortedPackages = sortPackages(outdatedPackages);
updateAndTestPackages(sortedPackages);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment