Skip to content

Instantly share code, notes, and snippets.

@4lessandrodev
Last active February 19, 2023 23:07
Show Gist options
  • Save 4lessandrodev/734a82a2d5bbe9ddf8490bd8bde405e0 to your computer and use it in GitHub Desktop.
Save 4lessandrodev/734a82a2d5bbe9ddf8490bd8bde405e0 to your computer and use it in GitHub Desktop.
const { execSync } = require('child_process');
const { readFileSync } = require('fs');
const { resolve } = require('path');
function compareVersions(a, b) {
return a.replace(/\^|\~|\=/g, '') === b.version;
}
try {
const npmDeps = execSync('npm ls --json');
const { dependencies: npmDepsObject = {} } = JSON.parse(npmDeps.toString());
const packageFile = readFileSync(resolve('package.json'), 'utf8');
const { dependencies: prdDeps = {}, devDependencies: devDeps = {} } = JSON.parse(packageFile);
const keys = Object.keys(npmDepsObject);
const prdResult = keys.reduce((acc, key) => {
if (prdDeps[key] && !compareVersions(prdDeps[key], npmDepsObject[key])) {
acc.push({
dependencies: key,
'package.json': prdDeps[key],
'node_modules': npmDepsObject[key].version,
equal: false
});
}
return acc;
}, []);
if (prdResult.length > 0) {
console.table(prdResult);
}
const devResult = keys.reduce((acc, key) => {
if (devDeps[key] && !compareVersions(devDeps[key], npmDepsObject[key])) {
acc.push({
devDependencies: key,
'package.json': devDeps[key],
'node_modules': npmDepsObject[key].version,
equal: false
});
}
return acc;
}, []);
if (devResult.length > 0) {
console.table(devResult);
}
} catch (error) {
console.error(error.message);
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment