forked from Dorota1997/react-frontend-dev-portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-diff.js
102 lines (88 loc) · 2.8 KB
/
package-diff.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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',
};
const blacklist = ['@types/react', 'react-test-renderer', 'eslint'];
// 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(/^[~^]/, '');
}
// Function to check if package is blacklisted from version checking
function isBlacklisted(_package) {
return blacklist.includes(_package);
}
// 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)
) {
if (!isBlacklisted(packageName)) {
console.log(
`${colors.green}[${packageName}]: Current version spec ${currentVersionSpec}, Latest version ${latestVersion}${colors.reset}`,
);
packagesToUpgrade.push(
`${colors.green}${packageName}@${latestVersion}${colors.reset}`,
);
}
}
}
console.log(
'------------------------------------------------------------------------------',
);
if (packagesToUpgrade.length === 0) {
console.log(
`${colors.green}No packages need to be upgraded${colors.reset}`,
);
} else {
for (const index in packagesToUpgrade) {
console.log(`yarn add ${packagesToUpgrade[index]}`);
}
}
console.log(
'------------------------------------------------------------------------------',
);
console.log(`${colors.cyan}Check complete!${colors.reset}`);
}
// Run the check
checkForUpdates();