Skip to content

Commit 5380bde

Browse files
authored
chore: fix prepublish script inconsistencies (#2410)
## Summary: Addresses inconsistencies in the prepublish script by moving the Azure Pipelines checks after the correct tag has been determined. ## Test Plan: Ensure tag is correctly set on `main`: ``` ~/S/react-native-macos (main) % node .ado/scripts/prepublish-check.mjs --verbose ❌ .ado/templates/npm-publish-steps.yml: 'publishTag' needs to be set to 'nightly' ``` Ensure tag is correctly set for `0.76-stable` (previous): ``` ~/S/react-native-macos (0.76-stable) % node .ado/scripts/prepublish-check.mjs --verbose ℹ️ react-native-macos@latest: 77 ℹ️ Current version: 76 ℹ️ Expected npm tag: v0.76-stable ❌ 'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to 'v0.76-stable' ❌ Nx Release is not correctly configured for the current branch ``` Ensure tag is correctly set for `0.77-stable` (current): ``` ~/S/react-native-macos (0.77-stable) % node .ado/scripts/prepublish-check.mjs --verbose ℹ️ react-native-macos@latest: 77 ℹ️ Current version: 77 ℹ️ Expected npm tag: latest ##vso[task.setvariable variable=publish_react_native_macos]1 ``` Ensure tag is correctly set for `0.78-stable` (release candidate): ``` ~/S/react-native-macos (0.78-stable) % node .ado/scripts/prepublish-check.mjs --verbose ℹ️ react-native-macos@latest: 77 ℹ️ Current version: 78 ℹ️ Expected npm tag: next ❌ 'defaultBase' must be set to '0.78-stable' ❌ 'release.version.generatorOptions.preid' must be set to 'rc' ❌ 'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to 'next' ❌ Nx Release is not correctly configured for the current branch ``` Update config for `0.78-stable` (release candidate): ``` ~/S/react-native-macos (0.78-stable) % node .ado/scripts/prepublish-check.mjs --update ❌ 'defaultBase' must be set to '0.78-stable' ❌ 'release.version.generatorOptions.preid' must be set to 'rc' ❌ 'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to 'next' ~/S/react-native-macos (0.78-stable) [1]% node .ado/scripts/prepublish-check.mjs --verbose ℹ️ react-native-macos@latest: 77 ℹ️ Current version: 78 ℹ️ Expected npm tag: next ❌ .ado/templates/npm-publish-steps.yml: 'publishTag' needs to be set to 'next' ``` Mark `0.78-stable` stable: ``` ~/S/react-native-macos (0.78-stable) % node .ado/scripts/prepublish-check.mjs --verbose --tag latest --update ℹ️ react-native-macos@latest: 77 ℹ️ Current version: 78 ℹ️ Expected npm tag: latest ❌ 'defaultBase' must be set to '0.78-stable' ~/S/react-native-macos (0.78-stable) [1]% node .ado/scripts/prepublish-check.mjs --verbose --tag latest ℹ️ react-native-macos@latest: 77 ℹ️ Current version: 78 ℹ️ Expected npm tag: latest ##vso[task.setvariable variable=publish_react_native_macos]1 ```
1 parent 9f58406 commit 5380bde

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

.ado/scripts/prepublish-check.mjs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ 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";
1112

1213
/**
1314
* @typedef {typeof import("../../nx.json")} NxConfig
14-
* @typedef {{ tag?: string; update?: boolean; }} Options
15+
* @typedef {{ tag?: string; update?: boolean; verbose?: boolean; }} Options
1516
*/
1617

1718
/**
@@ -33,6 +34,14 @@ function error(message) {
3334
console.error("❌", message);
3435
}
3536

37+
/**
38+
* Logs an informational message to the console.
39+
* @param {string} message
40+
*/
41+
function info(message) {
42+
console.log("ℹ️", message);
43+
}
44+
3645
/**
3746
* Returns whether the given branch is considered main branch.
3847
* @param {string} branch
@@ -94,7 +103,7 @@ function getCurrentBranch() {
94103
* @returns {number}
95104
*/
96105
function getLatestVersion() {
97-
const { stdout } = spawnSync("npm", ["view", "react-native-macos@latest", "version"]);
106+
const { stdout } = spawnSync("npm", ["view", RNMACOS_LATEST, "version"]);
98107
return versionToNumber(stdout.toString().trim());
99108
}
100109

@@ -108,35 +117,62 @@ function getLatestVersion() {
108117
*
109118
* @param {string} branch
110119
* @param {Options} options
120+
* @param {typeof info} log
111121
* @returns {{ npmTag: string; prerelease?: string; }}
112122
*/
113-
function getTagForStableBranch(branch, { tag }) {
123+
function getTagForStableBranch(branch, { tag }, log) {
114124
if (!isStableBranch(branch)) {
115125
throw new Error("Expected a stable branch");
116126
}
117127

118128
const latestVersion = getLatestVersion();
119129
const currentVersion = versionToNumber(branch);
120130

131+
log(`${RNMACOS_LATEST}: ${latestVersion}`);
132+
log(`Current version: ${currentVersion}`);
133+
121134
// Patching latest version
122135
if (currentVersion === latestVersion) {
123-
return { npmTag: "latest" };
136+
const npmTag = "latest";
137+
log(`Expected npm tag: ${npmTag}`);
138+
return { npmTag };
124139
}
125140

126141
// Patching an older stable version
127142
if (currentVersion < latestVersion) {
128-
return { npmTag: "v" + branch };
143+
const npmTag = "v" + branch;
144+
log(`Expected npm tag: ${npmTag}`);
145+
return { npmTag };
129146
}
130147

131148
// Publishing a new latest version
132149
if (tag === "latest") {
150+
log(`Expected npm tag: ${tag}`);
133151
return { npmTag: tag };
134152
}
135153

136154
// Publishing a release candidate
155+
log(`Expected npm tag: ${NPM_TAG_NEXT}`);
137156
return { npmTag: NPM_TAG_NEXT, prerelease: "rc" };
138157
}
139158

159+
/**
160+
* @param {string} file
161+
* @param {string} tag
162+
* @returns {void}
163+
*/
164+
function verifyPublishPipeline(file, tag) {
165+
const data = fs.readFileSync(file, { encoding: "utf-8" });
166+
const m = data.match(/publishTag: '(\w*?)'/);
167+
if (!m) {
168+
throw new Error(`${file}: Could not find npm publish tag`);
169+
}
170+
171+
if (m[1] !== tag) {
172+
throw new Error(`${file}: 'publishTag' needs to be set to '${tag}'`);
173+
}
174+
}
175+
140176
/**
141177
* Verifies the configuration and enables publishing on CI.
142178
* @param {NxConfig} config
@@ -182,30 +218,10 @@ function enablePublishing(config, currentBranch, tag, prerelease) {
182218
throw new Error("Nx Release is not correctly configured for the current branch");
183219
}
184220

221+
verifyPublishPipeline(ADO_PUBLISH_PIPELINE, tag);
185222
enablePublishingOnAzurePipelines();
186223
}
187224

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-
209225
/**
210226
* @param {Options} options
211227
* @returns {number}
@@ -217,16 +233,14 @@ function main(options) {
217233
return 1;
218234
}
219235

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

224238
const config = loadNxConfig(NX_CONFIG_FILE);
225239
try {
226240
if (isMainBranch(branch)) {
227241
enablePublishing(config, branch, NPM_TAG_NIGHTLY, NPM_TAG_NIGHTLY);
228242
} else if (isStableBranch(branch)) {
229-
const { npmTag, prerelease } = getTagForStableBranch(branch, options);
243+
const { npmTag, prerelease } = getTagForStableBranch(branch, options, logger);
230244
enablePublishing(config, branch, npmTag, prerelease);
231245
}
232246
} catch (e) {
@@ -236,7 +250,7 @@ function main(options) {
236250
fs.writeSync(fd, "\n");
237251
fs.closeSync(fd)
238252
} else {
239-
console.error(`${e}`);
253+
error(`${e.message}`);
240254
}
241255
return 1;
242256
}
@@ -255,6 +269,10 @@ const { values } = util.parseArgs({
255269
type: "boolean",
256270
default: false,
257271
},
272+
verbose: {
273+
type: "boolean",
274+
default: false,
275+
},
258276
},
259277
strict: true,
260278
});

.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: 'next'
3+
publishTag: 'nightly'
44

55
steps:
66
- checkout: self
@@ -15,7 +15,7 @@ steps:
1515
displayName: Install npm dependencies
1616
1717
- script: |
18-
node .ado/scripts/prepublish-check.mjs --tag ${{ parameters['publishTag'] }}
18+
node .ado/scripts/prepublish-check.mjs --verbose --tag ${{ parameters['publishTag'] }}
1919
displayName: Verify release config
2020
2121
- script: |

0 commit comments

Comments
 (0)