-
Notifications
You must be signed in to change notification settings - Fork 629
Expand file tree
/
Copy pathbeachball.config.js
More file actions
137 lines (123 loc) · 4.55 KB
/
Copy pathbeachball.config.js
File metadata and controls
137 lines (123 loc) · 4.55 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
const { existsSync, readFileSync, writeFileSync } = require("node:fs");
const { join } = require("node:path");
/**
* Convert an npm package name into the paired Rust crate name by dropping
* the leading `@` and replacing `/` with `-`.
*
* Example: `@microsoft/fast-build` -> `microsoft-fast-build`.
*/
function npmNameToCrateName(npmName) {
return npmName.replace(/^@/, "").replace(/\//g, "-");
}
const bundledCratesByPackage = new Map([
["@microsoft/fast-build", ["microsoft-fast-build", "microsoft-fast-convert"]],
]);
function npmNameToCrateNames(npmName) {
return bundledCratesByPackage.get(npmName) ?? [npmNameToCrateName(npmName)];
}
/**
* Rewrite the `version = "..."` line for a specific `[package]` or
* `[[package]]` block (matched by the crate name) within Cargo TOML
* content. Returns the new content, or `null` if no change was made.
*
* Hand-rolled (instead of using a TOML library) so beachball.config.js
* does not pull in any new runtime dependencies.
*/
function rewriteCargoVersion(content, crateName, newVersion, { manifest }) {
const lines = content.split("\n");
let inTargetBlock = false;
let nameMatched = manifest;
let changed = false;
for (let i = 0; i < lines.length; i++) {
const trimmed = lines[i].trim();
if (trimmed.startsWith("[")) {
if (manifest) {
inTargetBlock = trimmed === "[package]";
} else {
inTargetBlock = trimmed === "[[package]]";
nameMatched = false;
}
continue;
}
if (!inTargetBlock) continue;
if (!manifest && !nameMatched) {
const nameMatch = /^name\s*=\s*"([^"]+)"/.exec(trimmed);
if (nameMatch && nameMatch[1] === crateName) {
nameMatched = true;
continue;
}
}
if (nameMatched) {
const versionMatch = /^(\s*version\s*=\s*")([^"]+)(")/.exec(lines[i]);
if (versionMatch) {
if (versionMatch[2] !== newVersion) {
lines[i] = `${versionMatch[1]}${newVersion}${versionMatch[3]}`;
changed = true;
}
if (manifest) break;
inTargetBlock = false;
nameMatched = false;
}
}
}
return changed ? lines.join("\n") : null;
}
/**
* Beachball `postbump` hook: when an npm package with paired Rust
* crates is bumped, rewrite each crate's `Cargo.toml` (and matching entry
* in `Cargo.lock`, if present) so the crate version stays in lock-step
* with the npm version.
*
* Beachball commits any modified files in the same bump commit, so the
* Cargo updates land in the same PR as the package.json bump.
*/
function syncPairedCrateVersion(packagePath, name, version) {
for (const crateName of npmNameToCrateNames(name)) {
const cargoTomlPath = join(__dirname, "crates", crateName, "Cargo.toml");
if (!existsSync(cargoTomlPath)) continue;
const updatedToml = rewriteCargoVersion(
readFileSync(cargoTomlPath, "utf8"),
crateName,
version,
{ manifest: true },
);
if (updatedToml !== null) {
writeFileSync(cargoTomlPath, updatedToml);
console.log(`[beachball] Synced ${cargoTomlPath} to ${version}`);
}
const cargoLockPath = join(__dirname, "crates", crateName, "Cargo.lock");
if (existsSync(cargoLockPath)) {
const updatedLock = rewriteCargoVersion(
readFileSync(cargoLockPath, "utf8"),
crateName,
version,
{ manifest: false },
);
if (updatedLock !== null) {
writeFileSync(cargoLockPath, updatedLock);
console.log(`[beachball] Synced ${cargoLockPath} to ${version}`);
}
}
}
}
// `npm run checkchange` is wrapped by `build/scripts/checkchange.mjs`,
// which can skip `beachball check` for maintainer-authored manual bump
// branches (see CONTRIBUTING.md > "Manual version bumps"). The
// `ignorePatterns` and `hooks` below still apply when beachball runs.
module.exports = {
ignorePatterns: [
".ignore",
".github/",
".prettierrc",
".vscode/",
"jest..js",
"src/e2e/",
"src/tests/",
"src/fixtures/**",
// This one is especially important (otherwise dependabot would be blocked by change file requirements)
"package-lock.json",
],
hooks: {
postbump: syncPairedCrateVersion,
},
};