Repository navigation
Expand file tree
/
Copy pathbeachball.config.js
More file actions
169 lines (155 loc) · 6.3 KB
/
Copy pathbeachball.config.js
File metadata and controls
169 lines (155 loc) · 6.3 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
164
165
166
167
168
169
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). FAST Build's native package manifests,
* exact optional pins and npm lock entries also stay in lock-step.
*
* 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) {
if (name === "@microsoft/fast-build") {
const parentPath = join(__dirname, "packages/fast-build/package.json");
const parent = JSON.parse(readFileSync(parentPath, "utf8"));
for (const nativeName of Object.keys(parent.optionalDependencies ?? {})) {
if (!nativeName.startsWith("@microsoft/fast-build-")) continue;
parent.optionalDependencies[nativeName] = version;
const target = nativeName.replace("@microsoft/fast-build-", "");
const nativePath = join(
__dirname,
"packages/fast-build/native",
target,
"package.json",
);
const native = JSON.parse(readFileSync(nativePath, "utf8"));
native.version = version;
writeFileSync(nativePath, `${JSON.stringify(native, null, 2)}\n`);
}
writeFileSync(parentPath, `${JSON.stringify(parent, null, 2)}\n`);
// Beachball updates the npm lock before postbump. Preserve exact pins there too.
const lockPath = join(__dirname, "package-lock.json");
const lock = JSON.parse(readFileSync(lockPath, "utf8"));
const entry = lock.packages?.["packages/fast-build"];
if (entry) {
entry.optionalDependencies = { ...parent.optionalDependencies };
for (const target of Object.keys(parent.optionalDependencies)) {
const location = `packages/fast-build/native/${target.replace("@microsoft/fast-build-", "")}`;
if (lock.packages[location]) lock.packages[location].version = version;
}
writeFileSync(lockPath, `${JSON.stringify(lock, null, 2)}\n`);
}
}
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/**",
"**/test/declarative/fixtures/**",
// This one is especially important (otherwise dependabot would be blocked by change file requirements)
"package-lock.json",
],
hooks: {
postbump: syncPairedCrateVersion,
},
};