forked from chaibuilder/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.js
More file actions
executable file
·163 lines (139 loc) · 5.27 KB
/
publish.js
File metadata and controls
executable file
·163 lines (139 loc) · 5.27 KB
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
#!/usr/bin/env node
import { execSync } from "child_process";
import { readFileSync, writeFileSync } from "fs";
import { dirname } from "path";
import readline from "readline";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Function to validate version number
function validateVersion(version) {
const regex = /^[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$/;
if (!regex.test(version)) {
throw new Error("Invalid version format. Please use format: x.x.x or x.x.x-beta.x");
}
return version;
}
// Function to execute shell commands
function execCommand(command) {
try {
return execSync(command, { stdio: "inherit" });
} catch (error) {
console.error(`Error executing command: ${command}`);
process.exit(1);
}
}
// Function to get current branch name
function getCurrentBranch() {
return execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
}
// Function to safely push to a branch
function safePushToBranch(branch, targetBranch = "main") {
try {
// First try to push with upstream tracking
execCommand(`git push -u origin "${branch}:${targetBranch}"`);
} catch (error) {
console.error(
"Failed to push to remote. Please ensure you have the right permissions and the branch name is valid.",
);
throw error;
}
}
// Function to ask yes/no question
function askYesNo(question) {
return new Promise((resolve) => {
rl.question(`${question} (y/n): `, (answer) => {
resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes");
});
});
}
async function main() {
try {
// Read current version from package.json
const packageJson = JSON.parse(readFileSync("./package.json", "utf8"));
const currentVersion = packageJson.version;
console.log(`Current SDK version: ${currentVersion}`);
// Check for @chaibuilder/runtime in dependencies and peerDependencies
const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
const peerDependencies = packageJson.peerDependencies || {};
const runtimeVersion = dependencies["@chaibuilder/runtime"] || peerDependencies["@chaibuilder/runtime"];
if (runtimeVersion) {
console.log(`Current Runtime version: ${runtimeVersion}`);
if (peerDependencies["@chaibuilder/runtime"]) {
console.log(`(specified in peerDependencies)`);
}
}
// Ask for new version
const newVersion = await new Promise((resolve) => {
rl.question("Enter new SDK version number: ", resolve);
});
// Validate new version
validateVersion(newVersion);
// Ask about runtime version update if it exists in dependencies
let updateRuntime = false;
let newRuntimeVersion;
if (runtimeVersion) {
updateRuntime = await askYesNo("Do you want to update @chaibuilder/runtime version as well?");
if (updateRuntime) {
newRuntimeVersion = await new Promise((resolve) => {
rl.question("Enter new Runtime version number: ", resolve);
});
validateVersion(newRuntimeVersion);
}
}
// Get current branch name
const currentBranch = getCurrentBranch();
// Build the project
execCommand("pnpm run build");
// Update SDK version in package.json (without git tag)
execCommand(`pnpm version ${newVersion} --no-git-tag-version`);
// Update runtime version if requested
if (updateRuntime && newRuntimeVersion) {
// Update in dependencies if it exists there
if (dependencies["@chaibuilder/runtime"]) {
execCommand(`pnpm add @chaibuilder/runtime@${newRuntimeVersion} --save-exact`);
}
// Update in peerDependencies if it exists there
if (peerDependencies["@chaibuilder/runtime"]) {
const updatedPackageJson = JSON.parse(readFileSync("./package.json", "utf8"));
if (!updatedPackageJson.peerDependencies) {
updatedPackageJson.peerDependencies = {};
}
updatedPackageJson.peerDependencies["@chaibuilder/runtime"] = newRuntimeVersion;
writeFileSync("./package.json", JSON.stringify(updatedPackageJson, null, 2) + "\n");
}
}
// Git operations
execCommand("git add package.json");
if (updateRuntime && dependencies["@chaibuilder/runtime"]) {
execCommand("git add pnpm-lock.yaml");
}
execCommand(
`git commit -m "chore: bump version to ${newVersion}${updateRuntime ? ` and runtime to ${newRuntimeVersion}` : ""}"`,
);
// Create and push tag
execCommand(`git tag -a v${newVersion} -m "Release version ${newVersion}"`);
// Safely push to main branch
safePushToBranch(currentBranch, "main");
// Push the tag
execCommand(`git push origin v${newVersion}`);
console.log(`✅ Version bumped to ${newVersion} and tag created successfully!`);
if (updateRuntime) {
console.log(`✅ Runtime version bumped to ${newRuntimeVersion}`);
}
console.log(`🔄 Created and pushed tag v${newVersion}`);
} catch (error) {
console.error("❌ Error:", error.message);
// Revert package.json changes
execCommand("git checkout package.json");
execCommand("git checkout pnpm-lock.yaml");
process.exit(1);
} finally {
rl.close();
}
}
main();