Skip to content

Commit 08264f3

Browse files
Saadnajmitido64
andauthored
chore(0.76): fix prepublish script inconsistencies (#2411)
Backport of #2410 to 0.76-stable. I'm also updating the nx config tag. --------- Co-authored-by: Tommy Nguyen <[email protected]>
1 parent 39ada29 commit 08264f3

File tree

3 files changed

+104
-51
lines changed

3 files changed

+104
-51
lines changed

.ado/scripts/prepublish-check.mjs

Lines changed: 100 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ const NX_CONFIG_FILE = "nx.json";
88

99
const NPM_TAG_NEXT = "next";
1010
const NPM_TAG_NIGHTLY = "nightly";
11+
const RNMACOS_LATEST = "react-native-macos@latest";
12+
const RNMACOS_NEXT = "react-native-macos@next";
1113

1214
/**
13-
* @typedef {typeof import("../../nx.json")} NxConfig
14-
* @typedef {{ tag?: string; update?: boolean; }} Options
15+
* @typedef {import("nx/src/command-line/release/version").ReleaseVersionGeneratorSchema} ReleaseVersionGeneratorSchema;
16+
* @typedef {{
17+
* defaultBase: string;
18+
* release: {
19+
* version: {
20+
* generatorOptions: ReleaseVersionGeneratorSchema;
21+
* };
22+
* };
23+
* }} NxConfig;
24+
* @typedef {{ tag?: string; update?: boolean; verbose?: boolean; }} Options;
25+
* @typedef {{ npmTag: string; prerelease?: string; isNewTag?: boolean; }} TagInfo;
1526
*/
1627

1728
/**
@@ -33,6 +44,14 @@ function error(message) {
3344
console.error("❌", message);
3445
}
3546

47+
/**
48+
* Logs an informational message to the console.
49+
* @param {string} message
50+
*/
51+
function info(message) {
52+
console.log("ℹ️", message);
53+
}
54+
3655
/**
3756
* Returns whether the given branch is considered main branch.
3857
* @param {string} branch
@@ -91,10 +110,11 @@ function getCurrentBranch() {
91110

92111
/**
93112
* Returns the latest published version of `react-native-macos` from npm.
113+
* @param {"latest" | "next"} tag
94114
* @returns {number}
95115
*/
96-
function getLatestVersion() {
97-
const { stdout } = spawnSync("npm", ["view", "react-native-macos@latest", "version"]);
116+
function getPublishedVersion(tag) {
117+
const { stdout } = spawnSync("npm", ["view", `react-native-macos@${tag}`, "version"]);
98118
return versionToNumber(stdout.toString().trim());
99119
}
100120

@@ -108,44 +128,80 @@ function getLatestVersion() {
108128
*
109129
* @param {string} branch
110130
* @param {Options} options
111-
* @returns {{ npmTag: string; prerelease?: string; }}
131+
* @param {typeof info} log
132+
* @returns {TagInfo}
112133
*/
113-
function getTagForStableBranch(branch, { tag }) {
134+
function getTagForStableBranch(branch, { tag }, log) {
114135
if (!isStableBranch(branch)) {
115136
throw new Error("Expected a stable branch");
116137
}
117138

118-
const latestVersion = getLatestVersion();
139+
const latestVersion = getPublishedVersion("latest");
119140
const currentVersion = versionToNumber(branch);
120141

142+
log(`${RNMACOS_LATEST}: ${latestVersion}`);
143+
log(`Current version: ${currentVersion}`);
144+
121145
// Patching latest version
122146
if (currentVersion === latestVersion) {
123-
return { npmTag: "latest" };
147+
const npmTag = "latest";
148+
log(`Expected npm tag: ${npmTag}`);
149+
return { npmTag };
124150
}
125151

126-
// Patching an older stable version
152+
// Demoting or patching an older stable version
127153
if (currentVersion < latestVersion) {
128-
return { npmTag: "v" + branch };
154+
const npmTag = "v" + branch;
155+
log(`Expected npm tag: ${npmTag}`);
156+
// If we're demoting a branch, we will need to create a new tag. This will
157+
// make Nx trip if we don't specify a fallback. In all other scenarios, the
158+
// tags should exist and therefore prefer it to fail.
159+
return { npmTag, isNewTag: true };
129160
}
130161

131162
// Publishing a new latest version
132163
if (tag === "latest") {
164+
log(`Expected npm tag: ${tag}`);
133165
return { npmTag: tag };
134166
}
135167

136168
// Publishing a release candidate
169+
const nextVersion = getPublishedVersion("next");
170+
log(`${RNMACOS_NEXT}: ${nextVersion}`);
171+
log(`Expected npm tag: ${NPM_TAG_NEXT}`);
172+
173+
if (currentVersion < nextVersion) {
174+
throw new Error(`Current version cannot be a release candidate because it is too old: ${currentVersion} < ${nextVersion}`);
175+
}
176+
137177
return { npmTag: NPM_TAG_NEXT, prerelease: "rc" };
138178
}
139179

180+
/**
181+
* @param {string} file
182+
* @param {string} tag
183+
* @returns {void}
184+
*/
185+
function verifyPublishPipeline(file, tag) {
186+
const data = fs.readFileSync(file, { encoding: "utf-8" });
187+
const m = data.match(/publishTag: '(latest|next|nightly|v\d+\.\d+-stable)'/);
188+
if (!m) {
189+
throw new Error(`${file}: Could not find npm publish tag`);
190+
}
191+
192+
if (m[1] !== tag) {
193+
throw new Error(`${file}: 'publishTag' must be set to '${tag}'`);
194+
}
195+
}
196+
140197
/**
141198
* Verifies the configuration and enables publishing on CI.
142199
* @param {NxConfig} config
143200
* @param {string} currentBranch
144-
* @param {string} tag
145-
* @param {string} [prerelease]
201+
* @param {TagInfo} tag
146202
* @returns {asserts config is NxConfig["release"]}
147203
*/
148-
function enablePublishing(config, currentBranch, tag, prerelease) {
204+
function enablePublishing(config, currentBranch, { npmTag: tag, prerelease, isNewTag }) {
149205
/** @type {string[]} */
150206
const errors = [];
151207

@@ -160,52 +216,46 @@ function enablePublishing(config, currentBranch, tag, prerelease) {
160216
}
161217

162218
// Determines whether we need to add "nightly" or "rc" to the version string.
163-
const { currentVersionResolverMetadata, preid } = release.version.generatorOptions;
164-
if (preid !== prerelease) {
219+
const { generatorOptions } = release.version;
220+
if (generatorOptions.preid !== prerelease) {
165221
errors.push(`'release.version.generatorOptions.preid' must be set to '${prerelease || ""}'`);
166222
if (prerelease) {
167-
release.version.generatorOptions.preid = prerelease;
223+
generatorOptions.preid = prerelease;
168224
} else {
169-
// @ts-expect-error `preid` is optional
170-
release.version.generatorOptions.preid = undefined;
225+
generatorOptions.preid = undefined;
171226
}
172227
}
173228

174229
// What the published version should be tagged as e.g., "latest" or "nightly".
175-
if (currentVersionResolverMetadata.tag !== tag) {
230+
const { currentVersionResolverMetadata } = generatorOptions;
231+
if (currentVersionResolverMetadata?.tag !== tag) {
176232
errors.push(`'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to '${tag}'`);
177-
release.version.generatorOptions.currentVersionResolverMetadata.tag = tag;
233+
generatorOptions.currentVersionResolverMetadata ??= {};
234+
generatorOptions.currentVersionResolverMetadata.tag = tag;
235+
}
236+
237+
// If we're demoting a branch, we will need to create a new tag. This will
238+
// make Nx trip if we don't specify a fallback. In all other scenarios, the
239+
// tags should exist and therefore prefer it to fail.
240+
if (isNewTag) {
241+
if (generatorOptions.fallbackCurrentVersionResolver !== "disk") {
242+
errors.push("'release.version.generatorOptions.fallbackCurrentVersionResolver' must be set to 'disk'");
243+
generatorOptions.fallbackCurrentVersionResolver = "disk";
244+
}
245+
} else if (typeof generatorOptions.fallbackCurrentVersionResolver === "string") {
246+
errors.push("'release.version.generatorOptions.fallbackCurrentVersionResolver' must be unset");
247+
generatorOptions.fallbackCurrentVersionResolver = undefined;
178248
}
179249

180250
if (errors.length > 0) {
181251
errors.forEach(error);
182252
throw new Error("Nx Release is not correctly configured for the current branch");
183253
}
184254

255+
verifyPublishPipeline(ADO_PUBLISH_PIPELINE, tag);
185256
enablePublishingOnAzurePipelines();
186257
}
187258

188-
/**
189-
* @param {string} file
190-
* @param {string} tag
191-
* @returns {boolean}
192-
*/
193-
function verifyPublishPipeline(file, tag) {
194-
const data = fs.readFileSync(file, { encoding: "utf-8" });
195-
const m = data.match(/publishTag: '(\w*?)'/);
196-
if (!m) {
197-
error(`${file}: Could not find npm publish tag`);
198-
return false;
199-
}
200-
201-
if (m[1] !== tag) {
202-
error(`${file}: 'publishTag' needs to be set to '${tag}'`);
203-
return false;
204-
}
205-
206-
return true;
207-
}
208-
209259
/**
210260
* @param {Options} options
211261
* @returns {number}
@@ -217,17 +267,15 @@ function main(options) {
217267
return 1;
218268
}
219269

220-
if (!verifyPublishPipeline(ADO_PUBLISH_PIPELINE, options.tag || NPM_TAG_NEXT)) {
221-
return 1;
222-
}
270+
const logger = options.verbose ? info : () => undefined;
223271

224272
const config = loadNxConfig(NX_CONFIG_FILE);
225273
try {
226274
if (isMainBranch(branch)) {
227-
enablePublishing(config, branch, NPM_TAG_NIGHTLY, NPM_TAG_NIGHTLY);
275+
enablePublishing(config, branch, { npmTag: NPM_TAG_NIGHTLY, prerelease: NPM_TAG_NIGHTLY });
228276
} else if (isStableBranch(branch)) {
229-
const { npmTag, prerelease } = getTagForStableBranch(branch, options);
230-
enablePublishing(config, branch, npmTag, prerelease);
277+
const tag = getTagForStableBranch(branch, options, logger);
278+
enablePublishing(config, branch, tag);
231279
}
232280
} catch (e) {
233281
if (options.update) {
@@ -236,7 +284,7 @@ function main(options) {
236284
fs.writeSync(fd, "\n");
237285
fs.closeSync(fd)
238286
} else {
239-
console.error(`${e}`);
287+
error(`${e.message}`);
240288
}
241289
return 1;
242290
}
@@ -255,6 +303,10 @@ const { values } = util.parseArgs({
255303
type: "boolean",
256304
default: false,
257305
},
306+
verbose: {
307+
type: "boolean",
308+
default: false,
309+
},
258310
},
259311
strict: true,
260312
});

.ado/templates/npm-publish-steps.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
parameters:
22
# If this is a new stable branch, change `publishTag` to `latest` when going stable
3-
publishTag: 'latest'
3+
publishTag: 'v0.76-stable'
44

55
steps:
66
- template: /.ado/templates/configure-git.yml@self
@@ -10,7 +10,7 @@ steps:
1010
displayName: Install npm dependencies
1111
1212
- script: |
13-
node .ado/scripts/prepublish-check.mjs --tag ${{ parameters['publishTag'] }}
13+
node .ado/scripts/prepublish-check.mjs --verbose --tag ${{ parameters['publishTag'] }}
1414
displayName: Verify release config
1515
1616
- script: |

nx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
"generatorOptions": {
2727
"currentVersionResolver": "registry",
2828
"currentVersionResolverMetadata": {
29-
"tag": "latest"
29+
"tag": "v0.76-stable"
3030
},
31+
"fallbackCurrentVersionResolver": "disk",
3132
"skipLockFileUpdate": true
3233
}
3334
}

0 commit comments

Comments
 (0)