Skip to content

Instantly share code, notes, and snippets.

@neemzy neemzy/outdated-deps.php
Last active Dec 9, 2016

Embed
What would you like to do?
PHP script to make "(composer|npm) outdated" smarter
#!/usr/bin/php
<?php
// We will filter original outputs by ignoring packages for which we aren't using the latest major version,
// as well as branch references and packages installed straight from git repos.
$isUpToDateComposer = true;
$isUpToDateNpm = true;
// Composer
if (file_exists('composer.json')) {
shell_exec('composer install -q'); // we must install dependencies first
$packages = array_filter(
explode(PHP_EOL, shell_exec('composer outdated --direct --no-ansi')),
function($package) {
// Get rid of empty elements
if (empty($package)) {
return false;
}
preg_match_all('/\s+v?([0-9]+\.[0-9]+\.[0-9]+)/', $package, $versions);
$versions = $versions[1]; // preg_match_all is a creep
// Get rid of branch references
if (empty($versions)) {
return false;
}
// Only keep what we need and explode version numbers as arrays for easier comparison
list($current, $wanted) = array_map(
function($item) {
return explode('.', $item);
},
$versions
);
// Ignore not-backwards-compatible suggested upgrades, like a major version bump
// or a minor version bump under 0th major
return (($current[0] === $wanted[0]) && (($current[0] !== '0') || ($current[1] === $wanted[1])));
}
);
echo(implode(PHP_EOL, $packages).PHP_EOL);
$isUpToDateComposer = count($packages) === 0;
}
// NPM
if (file_exists('package.json')) {
shell_exec('npm install --quiet'); // we must install dependencies first
$packages = array_filter(
explode(PHP_EOL, shell_exec('npm outdated --parseable')),
function($package) {
// Get rid of empty elements, and git-based dependencies
if (empty($package) || strpos($package, '@git') !== false) {
return false;
}
preg_match_all('/@([0-9]+\.[0-9]+\.[0-9]+)/', $package, $versions);
$versions = $versions[1]; // preg_match_all is a weirdo
// First version is the currently installed one, second is the latest according to our constraint,
// and third is the latest overall.
// We therefore must compare first and second to determine whether we are out-of-date.
return $versions[0] !== $versions[1];
}
);
echo(implode(PHP_EOL, $packages).PHP_EOL);
$isUpToDateNpm = count($packages) === 0;
}
// The boolean value will be casted to an integer automatically.
// Our status code will thus be 0 if everything is up-to-date, 1 otherwise.
exit(!($isUpToDateComposer && $isUpToDateNpm));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.