forked from TBD54566975/developer.tbd.website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagateWeb5.js
43 lines (37 loc) · 1.41 KB
/
propagateWeb5.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
const fs = require("fs");
const path = require("path");
const rootPackageJsonPath = path.resolve(__dirname, "package.json");
const tutorialsDir = path.resolve(__dirname, "./examples/tutorials");
const getApiVersion = () => {
const rootPackageJsonContent = fs.readFileSync(rootPackageJsonPath, "utf8");
const rootPackageJson = JSON.parse(rootPackageJsonContent);
return rootPackageJson.dependencies["@web5/api"];
};
const updateTutorialsDependencies = (apiVersion) => {
const updatePackageJson = (dirPath) => {
const packageJsonPath = path.join(dirPath, "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
packageJson.dependencies = packageJson.dependencies || {};
packageJson.dependencies["@web5/api"] = apiVersion;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
};
fs.readdirSync(tutorialsDir, { withFileTypes: true }).forEach((dirent) => {
if (dirent.isDirectory()) {
updatePackageJson(path.join(tutorialsDir, dirent.name));
}
});
};
const propagateDependency = () => {
const apiVersion = getApiVersion();
if (apiVersion) {
updateTutorialsDependencies(apiVersion);
} else {
console.error(
"Failed to get the version of @web5/api from the root package.json"
);
}
};
propagateDependency();