forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_workspace.js
510 lines (463 loc) Β· 16 KB
/
release_workspace.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
const { execSync } = require("child_process");
const { Command } = require("commander");
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const readline = require("readline");
const semver = require("semver");
const RELEASE_BRANCH = "release";
const MAIN_BRANCH = "main";
/**
* Handles execSync errors and logs them in a readable format.
* @param {string} command
* @param {{ doNotExit?: boolean }} [options] - Optional configuration
* @param {boolean} [options.doNotExit] - Whether or not to exit the process on error
*/
function execSyncWithErrorHandling(command, options = {}) {
try {
execSync(
command,
{ stdio: "inherit" } // This will stream output in real-time
);
} catch (error) {
console.error(error.message);
if (!options.doNotExit) {
process.exit(1);
}
}
}
/**
* Get the version of a workspace inside a directory.
*
* @param {string} workspaceDirectory
* @returns {string} The version of the workspace in the input directory.
*/
function getWorkspaceVersion(workspaceDirectory) {
const pkgJsonFile = fs.readFileSync(
path.join(process.cwd(), workspaceDirectory, "package.json")
);
const parsedJSONFile = JSON.parse(pkgJsonFile);
return parsedJSONFile.version;
}
/**
* Finds all workspaces in the monorepo and returns an array of objects.
* Each object in the return value contains the relative path to the workspace
* directory, along with the full package.json file contents.
*
* @returns {Array<{ dir: string, packageJSON: Record<string, any>}>}
*/
function getAllWorkspaces() {
const possibleWorkspaceDirectories = [
"./libs/*",
"./langchain",
"./langchain-core",
];
const allWorkspaces = possibleWorkspaceDirectories.flatMap(
(workspaceDirectory) => {
if (workspaceDirectory.endsWith("*")) {
// List all folders inside directory, require, and return the package.json.
const allDirs = fs.readdirSync(
path.join(process.cwd(), workspaceDirectory.replace("*", ""))
);
const subDirs = allDirs.map((dir) => {
return {
dir: `${workspaceDirectory.replace("*", "")}${dir}`,
packageJSON: require(path.join(
process.cwd(),
`${workspaceDirectory.replace("*", "")}${dir}`,
"package.json"
)),
};
});
return subDirs;
}
const packageJSON = require(path.join(
process.cwd(),
workspaceDirectory,
"package.json"
));
return {
dir: workspaceDirectory,
packageJSON,
};
}
);
return allWorkspaces;
}
/**
* Writes the JSON file with the updated dependency version. Accounts
* for version prefixes, eg ~, ^, >, <, >=, <=, ||, *. Also skips
* versions which are "latest" or "workspace:*".
*
* @param {Array<string>} workspaces
* @param {"dependencies" | "devDependencies" | "peerDependencies"} dependencyType
* @param {string} workspaceName
* @param {string} newVersion
*/
function updateDependencies(
workspaces,
dependencyType,
workspaceName,
newVersion
) {
const versionPrefixes = ["~", "^", ">", "<", ">=", "<=", "||", "*"];
const skipVersions = ["latest", "workspace:*"];
workspaces.forEach((workspace) => {
const currentVersion =
workspace.packageJSON[dependencyType]?.[workspaceName];
if (currentVersion) {
const prefix = versionPrefixes.find((p) => currentVersion.startsWith(p));
const shouldSkip = skipVersions.some((v) => currentVersion === v);
if (!shouldSkip) {
const versionToUpdate = prefix ? `${prefix}${newVersion}` : newVersion;
workspace.packageJSON[dependencyType][workspaceName] = versionToUpdate;
fs.writeFileSync(
path.join(workspace.dir, "package.json"),
JSON.stringify(workspace.packageJSON, null, 2) + "\n"
);
}
}
});
}
/**
* Runs `release-it` with args in the input package directory,
* passing the new version as an argument, along with other
* release-it args.
*
* @param {string} packageDirectory The directory to run yarn release in.
* @param {string} npm2FACode The 2FA code for NPM.
* @param {string | undefined} tag An optional tag to publish to.
* @returns {Promise<void>}
*/
async function runYarnRelease(packageDirectory, npm2FACode, tag) {
return new Promise((resolve, reject) => {
const workingDirectory = path.join(process.cwd(), packageDirectory);
const tagArg = tag ? `--npm.tag=${tag}` : "";
const args = [
"release-it",
`--npm.otp=${npm2FACode}`,
tagArg,
"--config",
".release-it.json",
];
console.log(`Running command: "yarn ${args.join(" ")}"`);
// Use 'inherit' for stdio to allow direct CLI interaction
const yarnReleaseProcess = spawn("yarn", args, {
stdio: "inherit",
cwd: workingDirectory,
});
yarnReleaseProcess.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject(`Process exited with code ${code}`);
}
});
yarnReleaseProcess.on("error", (err) => {
reject(`Failed to start process: ${err.message}`);
});
});
}
/**
* Finds all `package.json`'s which contain the input workspace as a dependency.
* Then, updates the dependency to the new version, runs yarn install and
* commits the changes.
*
* @param {string} workspaceName The name of the workspace to bump dependencies for.
* @param {string} workspaceDirectory The path to the workspace directory.
* @param {Array<{ dir: string, packageJSON: Record<string, any>}>} allWorkspaces
* @param {string | undefined} tag An optional tag to publish to.
* @param {string} preReleaseVersion The version of the workspace before it was released.
* @returns {void}
*/
function bumpDeps(
workspaceName,
workspaceDirectory,
allWorkspaces,
tag,
preReleaseVersion
) {
// Read workspace file, get version (edited by release-it), and bump pkgs to that version.
let updatedWorkspaceVersion = getWorkspaceVersion(workspaceDirectory);
if (!semver.valid(updatedWorkspaceVersion)) {
console.error("Invalid workspace version: ", updatedWorkspaceVersion);
process.exit(1);
}
// If the updated version is not greater than the pre-release version,
// the branch is out of sync. Pull from github and check again.
if (!semver.gt(updatedWorkspaceVersion, preReleaseVersion)) {
console.log(
"Updated version is not greater than the pre-release version. Pulling from github and checking again."
);
execSyncWithErrorHandling(`git pull origin ${RELEASE_BRANCH}`);
updatedWorkspaceVersion = getWorkspaceVersion(workspaceDirectory);
if (!semver.gt(updatedWorkspaceVersion, preReleaseVersion)) {
console.warn(
`Workspace version has not changed in repo. Version in repo: ${updatedWorkspaceVersion}. Exiting.`
);
process.exit(0);
}
}
console.log(`Bumping other packages which depend on ${workspaceName}.`);
console.log(`Checking out ${MAIN_BRANCH} branch.`);
// Separate variable for the branch name, incase it includes a tag.
let versionString = updatedWorkspaceVersion;
if (tag) {
versionString = `${updatedWorkspaceVersion}-${tag}`;
}
execSyncWithErrorHandling(`git checkout ${MAIN_BRANCH}`);
const newBranchName = `bump-${workspaceName}-to-${versionString}`;
console.log(`Checking out new branch: ${newBranchName}`);
execSyncWithErrorHandling(`git checkout -b ${newBranchName}`);
const allWorkspacesWhichDependOn = allWorkspaces.filter(({ packageJSON }) =>
Object.keys(packageJSON.dependencies ?? {}).includes(workspaceName)
);
const allWorkspacesWhichDevDependOn = allWorkspaces.filter(
({ packageJSON }) =>
Object.keys(packageJSON.devDependencies ?? {}).includes(workspaceName)
);
const allWorkspacesWhichPeerDependOn = allWorkspaces.filter(
({ packageJSON }) =>
Object.keys(packageJSON.peerDependencies ?? {}).includes(workspaceName)
);
// For console log, get all workspaces which depend and filter out duplicates.
const allWhichDependOn = new Set(
[
...allWorkspacesWhichDependOn,
...allWorkspacesWhichDevDependOn,
...allWorkspacesWhichPeerDependOn,
].map(({ packageJSON }) => packageJSON.name)
);
if (allWhichDependOn.size !== 0) {
console.log(`Found ${
[...allWhichDependOn].length
} workspaces which depend on ${workspaceName}.
Workspaces:
- ${[...allWhichDependOn].map((name) => name).join("\n- ")}
`);
// Update packages which depend on the input workspace.
updateDependencies(
allWorkspacesWhichDependOn,
"dependencies",
workspaceName,
updatedWorkspaceVersion
);
updateDependencies(
allWorkspacesWhichDevDependOn,
"devDependencies",
workspaceName,
updatedWorkspaceVersion
);
updateDependencies(
allWorkspacesWhichPeerDependOn,
"peerDependencies",
workspaceName,
updatedWorkspaceVersion
);
console.log("Updated package.json's! Running yarn install.");
try {
execSyncWithErrorHandling(`yarn install`);
} catch (_) {
console.log(
"Yarn install failed. Likely because NPM has not finished publishing the new version. Continuing."
);
}
// Add all current changes, commit, push and log branch URL.
console.log("Adding and committing all changes.");
execSyncWithErrorHandling(`git add -A`);
execSyncWithErrorHandling(
`git commit -m "all[minor]: bump deps on ${workspaceName} to ${versionString}"`
);
console.log("Pushing changes.");
execSyncWithErrorHandling(`git push -u origin ${newBranchName}`);
console.log(
"π Open %s and merge the bump-deps PR.",
`\x1b[34mhttps://github.com/langchain-ai/langchainjs/compare/${newBranchName}?expand=1\x1b[0m`
);
} else {
console.log(`No workspaces depend on ${workspaceName}.`);
}
}
/**
* Create a commit message for the input workspace and version.
*
* @param {string} workspaceName
* @param {string} version
*/
function createCommitMessage(workspaceName, version) {
const cleanedWorkspaceName = workspaceName.replace("@langchain/", "");
return `release(${cleanedWorkspaceName}): ${version}`;
}
/**
* Commits all changes and pushes to the current branch.
*
* @param {string} workspaceName The name of the workspace being released
* @param {string} version The new version being released
* @param {boolean} onlyPush Whether or not to only push the changes, and not commit
* @returns {void}
*/
function commitAndPushChanges(workspaceName, version, onlyPush) {
if (!onlyPush) {
console.log("Committing changes...");
const commitMsg = createCommitMessage(workspaceName, version);
try {
execSyncWithErrorHandling("git add -A", { doNotExit: true });
execSyncWithErrorHandling(`git commit -m "${commitMsg}"`, {
doNotExit: true,
});
} catch (_) {
// No-op. Likely erroring because there are no unstaged changes.
}
}
console.log("Pushing changes...");
// Pushes to the current branch
execSyncWithErrorHandling(
"git push -u origin $(git rev-parse --abbrev-ref HEAD)"
);
console.log("Successfully committed and pushed changes.");
}
/**
* Verifies the current branch is main, then checks out a new release branch
* and pushes an empty commit.
*
* @returns {void}
* @throws {Error} If the current branch is not main.
*/
function checkoutReleaseBranch() {
const currentBranch = execSync("git branch --show-current").toString().trim();
if (currentBranch === MAIN_BRANCH || currentBranch === RELEASE_BRANCH) {
console.log(`Checking out '${RELEASE_BRANCH}' branch.`);
execSyncWithErrorHandling(`git checkout -B ${RELEASE_BRANCH}`);
execSyncWithErrorHandling(`git push -u origin ${RELEASE_BRANCH}`);
} else {
throw new Error(
`Current branch is not ${MAIN_BRANCH} or ${RELEASE_BRANCH}. Current branch: ${currentBranch}`
);
}
}
/**
* Prompts the user for input and returns the input. This is used
* for requesting an OTP from the user for NPM 2FA.
*
* @param {string} question The question to log to the users terminal.
* @returns {Promise<string>} The user input.
*/
async function getUserInput(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(`\x1b[30m\x1b[47m${question}\x1b[0m`, (input) => {
rl.close();
resolve(input);
});
});
}
/**
* Checks if there are any uncommitted changes in the git repository.
*
* @returns {boolean} True if there are uncommitted changes, false otherwise
*/
function hasUncommittedChanges() {
try {
// Check for uncommitted changes (both staged and unstaged)
const uncommittedOutput = execSync("git status --porcelain").toString();
return uncommittedOutput.length > 0;
} catch (error) {
console.error("Error checking git status:", error);
// If we can't check, better to assume there are changes
return true;
}
}
/**
* Checks if there are any staged commits in the git repository.
*
* @returns {boolean} True if there are staged changes, false otherwise
*/
function hasStagedChanges() {
try {
// Check for staged but unpushed changes
const unPushedOutput = execSync("git log '@{u}..'").toString();
return unPushedOutput.length > 0;
} catch (error) {
console.error("Error checking git status:", error);
// If we can't check, better to assume there are changes
return true;
}
}
async function main() {
const program = new Command();
program
.description("Release a new workspace version to NPM.")
.option("--workspace <workspace>", "Workspace name, eg @langchain/core")
.option(
"--bump-deps",
"Whether or not to bump other workspaces that depend on this one."
)
.option("--tag <tag>", "Optionally specify a tag to publish to.");
program.parse();
/**
* @type {{ workspace: string, bumpDeps?: boolean, tag?: string }}
*/
const options = program.opts();
if (!options.workspace) {
throw new Error("--workspace is a required flag.");
}
if (hasUncommittedChanges()) {
console.warn(
"[WARNING]: You have uncommitted changes. These will be included in the release commit."
);
}
// Find the workspace package.json's.
const allWorkspaces = getAllWorkspaces();
const matchingWorkspace = allWorkspaces.find(
({ packageJSON }) => packageJSON.name === options.workspace
);
if (!matchingWorkspace) {
throw new Error(`Could not find workspace ${options.workspace}`);
}
// Checkout new "release" branch & push
checkoutReleaseBranch();
// Run build, lint, tests
console.log("Running build, lint, and tests.");
execSyncWithErrorHandling(
`yarn turbo:command run --filter ${options.workspace} build lint test --concurrency 1`
);
console.log("Successfully ran build, lint, and tests.");
const npm2FACode = await getUserInput(
"Please enter your NPM 2FA authentication code:"
);
const preReleaseVersion = getWorkspaceVersion(matchingWorkspace.dir);
// Run `release-it` on workspace
await runYarnRelease(matchingWorkspace.dir, npm2FACode, options.tag);
const hasStaged = hasStagedChanges();
const hasUnCommitted = hasUncommittedChanges();
if (hasStaged || hasUnCommitted) {
const updatedVersion = getWorkspaceVersion(matchingWorkspace.dir);
// Only push and do not commit if there are staged changes and no uncommitted changes
const onlyPush = hasStaged && !hasUnCommitted;
commitAndPushChanges(options.workspace, updatedVersion, onlyPush);
}
// Log release branch URL
console.log(
"π Open %s and merge the release PR.",
`\x1b[34mhttps://github.com/langchain-ai/langchainjs/compare/release?expand=1\x1b[0m`
);
// If `bump-deps` flag is set, find all workspaces which depend on the input workspace.
// Then, update their package.json to use the new version of the input workspace.
// This will create a new branch, commit and push the changes and log the branch URL.
if (options.bumpDeps) {
bumpDeps(
options.workspace,
matchingWorkspace.dir,
allWorkspaces,
options.tag,
preReleaseVersion
);
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});