forked from wormhole-foundation/native-token-transfers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setSdkVersion.ts
69 lines (56 loc) · 1.95 KB
/
setSdkVersion.ts
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
import fs from "fs";
import path from "path";
function updateVersionInPackageJson(
dir: string,
version: string,
packagesInWorkspace: string[]
) {
const packageJsonPath = path.join(dir, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
packageJson.version = version;
if (packageJson.dependencies) {
packageJson.dependencies = Object.fromEntries(
Object.entries(packageJson.dependencies).map((entry) => {
const [k, v] = entry as [string, string];
return [k, packagesInWorkspace.includes(k) ? `${version}` : v];
})
);
}
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
function getPackageName(dir: string): string {
const packageJsonPath = path.join(dir, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
return packageJson.name;
}
type Workspace = {
// workspace name
name: string;
// path to workspace
path: string;
// package name
package: string;
};
function updateVersionsInWorkspaces(dir: string, version: string) {
const packageJsonPath = path.join(dir, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const workspaces = packageJson.workspaces.map((workspaceName: string) => {
const workspaceDir = path.join(dir, workspaceName);
return {
name: workspaceName,
path: workspaceDir,
package: getPackageName(workspaceDir),
} as Workspace;
});
const workspacePackages = workspaces.map((ws: Workspace) => ws.package);
// Root update
updateVersionInPackageJson(dir, version, workspacePackages);
// Workspaces update
workspaces.forEach((ws: Workspace) => {
updateVersionInPackageJson(ws.path, version, workspacePackages);
});
}
const args = process.argv.slice(2);
const version = args[0];
if (!version) throw new Error("A version string must be provided");
updateVersionsInWorkspaces(path.resolve("."), version);