-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-dependency.js
76 lines (63 loc) · 2.18 KB
/
update-dependency.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
const fs = require('fs');
const path = require('path');
const packagesDir = path.join(__dirname, 'packages');
const workspaceDependencyVersion = 'workspace:*';
/**
* Get all direct package.json files in packages
*/
function getTopLevelPackageJsonPaths() {
const subdirs = fs.readdirSync(packagesDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => path.join(packagesDir, dirent.name, 'package.json'))
.filter(pkgPath => fs.existsSync(pkgPath));
return subdirs;
}
/**
* Collect names of internal workspace packages from top-level package.json files
*/
function collectWorkspacePackageNames(packageJsonPaths) {
const names = [];
for (const filePath of packageJsonPaths) {
const content = fs.readFileSync(filePath, 'utf-8');
try {
const pkg = JSON.parse(content);
if (pkg.name) {
names.push(pkg.name);
}
} catch (err) {
console.error(`❌ Failed to parse ${filePath}: ${err.message}`);
}
}
return names;
}
/**
* Update only the internal dependency versions to "workspace:*"
*/
function updateDependenciesToWorkspace(pkgJsonPath, internalPackageNames) {
const content = fs.readFileSync(pkgJsonPath, 'utf-8');
const pkg = JSON.parse(content);
let updated = false;
['dependencies', 'devDependencies', 'peerDependencies'].forEach((depType) => {
if (pkg[depType]) {
internalPackageNames.forEach((name) => {
if (Object.prototype.hasOwnProperty.call(pkg[depType], name)) {
if (pkg[depType][name] !== workspaceDependencyVersion) {
pkg[depType][name] = workspaceDependencyVersion;
updated = true;
}
}
});
}
});
if (updated) {
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8');
console.log(`✅ Updated dependencies in ${pkgJsonPath}`);
}
}
// 🔄 Run the process
const topLevelPackageJsonPaths = getTopLevelPackageJsonPaths();
const internalPackageNames = collectWorkspacePackageNames(topLevelPackageJsonPaths);
console.log('📦 Found internal packages:', internalPackageNames);
for (const pkgPath of topLevelPackageJsonPaths) {
updateDependenciesToWorkspace(pkgPath, internalPackageNames);
}