mirror of
https://github.com/warkanum/monorepo-dep-checker.git
synced 2025-05-18 18:57:29 +00:00
Fixed better update
This commit is contained in:
parent
6aef0da6c4
commit
9851a91bb3
116
src/index.js
116
src/index.js
@ -474,67 +474,77 @@ class DependencyChecker {
|
|||||||
async updateDependencies(dryRun = false) {
|
async updateDependencies(dryRun = false) {
|
||||||
console.log(chalk.bold('\nUpdating dependencies...\n'));
|
console.log(chalk.bold('\nUpdating dependencies...\n'));
|
||||||
|
|
||||||
|
// Read the main app's package.json
|
||||||
|
const appPackageJson = JSON.parse(fs.readFileSync(this.appPackageJsonPath, 'utf8'));
|
||||||
|
const appDependencies = {
|
||||||
|
...appPackageJson.dependencies,
|
||||||
|
...appPackageJson.devDependencies,
|
||||||
|
...appPackageJson.peerDependencies
|
||||||
|
};
|
||||||
|
|
||||||
const updates = [];
|
const updates = [];
|
||||||
this.dependencyMap.forEach((depInfo, dep) => {
|
|
||||||
const allVersions = Array.from(depInfo.versions.keys()).filter(
|
|
||||||
(v) => !v.startsWith('workspace:')
|
|
||||||
); // Skip workspace dependencies
|
|
||||||
|
|
||||||
if (allVersions.length === 0) return; // Skip if only workspace versions exist
|
// Process each package.json except the main app
|
||||||
|
this.packageJsonFiles
|
||||||
|
.filter(filePath => filePath !== this.appPackageJsonPath)
|
||||||
|
.forEach(filePath => {
|
||||||
|
const packageJson = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||||
|
let hasUpdates = false;
|
||||||
|
|
||||||
const cleanVersions = allVersions.map((v) => this.getSemverVersion(v)).filter(Boolean);
|
// Helper function to update dependencies of a specific type
|
||||||
|
const updateDependencySection = (section) => {
|
||||||
|
if (!packageJson[section]) return;
|
||||||
|
|
||||||
if (cleanVersions.length === 0) return; // Skip if no valid versions
|
Object.entries(packageJson[section]).forEach(([dep, version]) => {
|
||||||
|
// Skip workspace dependencies
|
||||||
const highestVersion = semver.maxSatisfying(cleanVersions, '*');
|
if (version.startsWith('workspace:')) return;
|
||||||
|
|
||||||
if (highestVersion) {
|
// If the dependency exists in the main app, sync the version
|
||||||
this.packageJsonFiles.forEach((filePath) => {
|
if (appDependencies[dep]) {
|
||||||
const packageJson = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
const appVersion = appDependencies[dep];
|
||||||
let updated = false;
|
if (version !== appVersion) {
|
||||||
|
if (!dryRun) {
|
||||||
// Don't update workspace dependencies
|
packageJson[section][dep] = appVersion;
|
||||||
if (
|
}
|
||||||
packageJson.dependencies?.[dep] &&
|
updates.push({
|
||||||
!packageJson.dependencies[dep].startsWith('workspace:')
|
package: packageJson.name,
|
||||||
) {
|
dependency: dep,
|
||||||
if (!dryRun) {
|
from: version,
|
||||||
packageJson.dependencies[dep] = `^${highestVersion}`;
|
to: appVersion,
|
||||||
|
type: section
|
||||||
|
});
|
||||||
|
hasUpdates = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
updated = true;
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
if (
|
// Update all dependency sections
|
||||||
packageJson.peerDependencies?.[dep] &&
|
updateDependencySection('dependencies');
|
||||||
!packageJson.peerDependencies[dep].startsWith('workspace:')
|
updateDependencySection('devDependencies');
|
||||||
) {
|
updateDependencySection('peerDependencies');
|
||||||
if (!dryRun) {
|
|
||||||
packageJson.peerDependencies[dep] = `^${highestVersion}`;
|
|
||||||
}
|
|
||||||
updated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (updated) {
|
// Write updated package.json if there were changes
|
||||||
if (!dryRun) {
|
if (hasUpdates && !dryRun) {
|
||||||
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2));
|
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2) + '\n');
|
||||||
}
|
}
|
||||||
updates.push({
|
});
|
||||||
package: packageJson.name,
|
|
||||||
dependency: dep,
|
|
||||||
version: highestVersion,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
updates.forEach(({ package: pkgName, dependency, version }) => {
|
// Display updates
|
||||||
console.log(
|
if (updates.length === 0) {
|
||||||
chalk.green(
|
console.log(chalk.green('No updates needed - all versions match the main app'));
|
||||||
`${dryRun ? '[DRY RUN] Would update' : 'Updated'} ${dependency} to ^${version} in ${pkgName}`
|
} else {
|
||||||
)
|
updates.forEach(({ package: pkgName, dependency, from, to, type }) => {
|
||||||
);
|
console.log(
|
||||||
});
|
chalk.green(
|
||||||
|
`${dryRun ? '[DRY RUN] Would update' : 'Updated'} ${dependency} in ${pkgName} (${type})\n` +
|
||||||
|
` ${chalk.red(from)} → ${chalk.green(to)}`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return updates;
|
||||||
}
|
}
|
||||||
|
|
||||||
async run({
|
async run({
|
||||||
|
Loading…
Reference in New Issue
Block a user