forked from TBD54566975/tbd-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagateSdkVersions.js
178 lines (160 loc) · 6.18 KB
/
propagateSdkVersions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
// Setup
const sdkVersionsPath = path.resolve(__dirname, "sdk-versions.json");
// Grab SDK versions from sdk-versions.json
const getSdkVersions = async () => {
try {
const content = await fs.promises.readFile(sdkVersionsPath, "utf8");
return JSON.parse(content);
} catch (error) {
console.error("Error reading sdk-versions.json:", error);
throw error; // Rethrow to handle it in propagateVersions
}
};
// Update package.json dependencies and install npm packages
const updatePackageJsonDependencies = async (dirPath, sdkVersions) => {
const packageJsonPath = path.join(dirPath, "package.json");
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(
await fs.promises.readFile(packageJsonPath, "utf8")
);
let updated = false;
for (let sdk in sdkVersions.js) {
if (packageJson.dependencies && packageJson.dependencies[sdk]) {
packageJson.dependencies[sdk] = sdkVersions.js[sdk];
updated = true;
}
if (packageJson.devDependencies && packageJson.devDependencies[sdk]) {
packageJson.devDependencies[sdk] = sdkVersions.js[sdk];
updated = true;
}
}
if (updated) {
await fs.promises.writeFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2)
);
console.log(`Updated ${packageJsonPath}`);
// Run npm install to update the dependencies
exec(`npm install --prefix ${dirPath}`, (err, stdout, stderr) => {
if (err) {
console.error(`Error running npm install in ${dirPath}:`, err);
return;
}
console.log(`npm install output in ${dirPath}: ${stdout}`);
if (stderr) console.error(`npm install stderr in ${dirPath}: ${stderr}`);
});
}
} catch (error) {
console.error(`Error updating ${packageJsonPath}:`, error);
}
}
// Continue with the original directory processing
if (fs.lstatSync(dirPath).isDirectory()) {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (let dirent of entries) {
if (dirent.name === "node_modules") continue; // Skip node_modules directory
const fullPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory()) {
await updatePackageJsonDependencies(fullPath, sdkVersions); // Recursive call for subdirectories
}
}
}
};
const updatePomXmlVersion = async (filePath, sdkVersions) => {
try {
const pomContent = await fs.promises.readFile(filePath, "utf8");
let updatedPomContent = pomContent;
Object.entries(sdkVersions.jvm).forEach(([artifactId, version]) => {
const versionTagRegex = new RegExp(
`(<version.xyz.block.${artifactId}>)(.*?)(<\/version.xyz.block.${artifactId}>)`,
"g"
);
if (versionTagRegex.test(pomContent)) {
console.log(
`Found matches for ${artifactId}, updating to version ${version}.`
);
} else {
console.log(`No matches found for ${artifactId}.`);
}
updatedPomContent = updatedPomContent.replace(
versionTagRegex,
`$1${version}$3`
);
});
if (updatedPomContent !== pomContent) {
await fs.promises.writeFile(filePath, updatedPomContent);
} else {
}
} catch (error) {
console.error(`Failed to update ${filePath}:`, error);
}
};
// Function to recursively find and update pom.xml files, excluding node_modules
async function findAndUpdatePomFiles(dirPath, sdkVersions) {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (let dirent of entries) {
if (dirent.name === "node_modules") continue; // Skip node_modules directory
const fullPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory()) {
await findAndUpdatePomFiles(fullPath, sdkVersions); // Recursive call for directories
} else if (dirent.name === "pom.xml") {
await updatePomXmlVersion(fullPath, sdkVersions); // Directly update pom.xml files
}
}
}
// Function to update Package.swift dependencies
const updatePackageSwiftDependencies = async (dirPath, sdkVersions) => {
const packageSwiftPath = path.join(dirPath, "Package.swift");
try {
const packageSwiftContent = await fs.promises.readFile(
packageSwiftPath,
"utf8"
);
let updatedPackageSwiftContent = packageSwiftContent;
Object.keys(sdkVersions.swift).forEach((dependency) => {
const { url, branch } = sdkVersions.swift[dependency];
const regex = new RegExp(
`\\.package\\(url: "${url}", branch: ".*?"\\)`,
"g"
);
updatedPackageSwiftContent = updatedPackageSwiftContent.replace(
regex,
`.package(url: "${url}", branch: "${branch}")`
);
});
await fs.promises.writeFile(packageSwiftPath, updatedPackageSwiftContent);
} catch (error) {
console.error(`Failed to update Package.swift in ${dirPath}:`, error);
}
};
// Function to recursively find and update Package.swift files
async function findAndUpdatePackageSwift(dirPath, sdkVersions) {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (let dirent of entries) {
if (dirent.name === "node_modules") continue; // Skip node_modules directory
const fullPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory()) {
await findAndUpdatePackageSwift(fullPath, sdkVersions); // Recursive call for directories
} else if (dirent.name === "Package.swift") {
await updatePackageSwiftDependencies(dirPath, sdkVersions); // Directly update dependencies in Package.swift files
}
}
}
// Main function to initiate the version propagation process
async function propagateVersions() {
try {
const sdkVersions = await getSdkVersions();
for (const dirPath of ['.']) {
await updatePackageJsonDependencies(dirPath, sdkVersions);
await findAndUpdatePomFiles(dirPath, sdkVersions);
await findAndUpdatePackageSwift(dirPath, sdkVersions); // Add this line to update Swift dependencies
}
} catch (error) {
console.error("Failed to propagate versions:", error);
}
}
propagateVersions();