Skip to content

chore: fix prepublish script inconsistencies #2410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 49 additions & 31 deletions .ado/scripts/prepublish-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const NX_CONFIG_FILE = "nx.json";

const NPM_TAG_NEXT = "next";
const NPM_TAG_NIGHTLY = "nightly";
const RNMACOS_LATEST = "react-native-macos@latest";

/**
* @typedef {typeof import("../../nx.json")} NxConfig
* @typedef {{ tag?: string; update?: boolean; }} Options
* @typedef {{ tag?: string; update?: boolean; verbose?: boolean; }} Options
*/

/**
Expand All @@ -33,6 +34,14 @@ function error(message) {
console.error("❌", message);
}

/**
* Logs an informational message to the console.
* @param {string} message
*/
function info(message) {
console.log("ℹ️", message);
}

/**
* Returns whether the given branch is considered main branch.
* @param {string} branch
Expand Down Expand Up @@ -94,7 +103,7 @@ function getCurrentBranch() {
* @returns {number}
*/
function getLatestVersion() {
const { stdout } = spawnSync("npm", ["view", "react-native-macos@latest", "version"]);
const { stdout } = spawnSync("npm", ["view", RNMACOS_LATEST, "version"]);
return versionToNumber(stdout.toString().trim());
}

Expand All @@ -108,35 +117,62 @@ function getLatestVersion() {
*
* @param {string} branch
* @param {Options} options
* @param {typeof info} log
* @returns {{ npmTag: string; prerelease?: string; }}
*/
function getTagForStableBranch(branch, { tag }) {
function getTagForStableBranch(branch, { tag }, log) {
if (!isStableBranch(branch)) {
throw new Error("Expected a stable branch");
}

const latestVersion = getLatestVersion();
const currentVersion = versionToNumber(branch);

log(`${RNMACOS_LATEST}: ${latestVersion}`);
log(`Current version: ${currentVersion}`);

// Patching latest version
if (currentVersion === latestVersion) {
return { npmTag: "latest" };
const npmTag = "latest";
log(`Expected npm tag: ${npmTag}`);
return { npmTag };
}

// Patching an older stable version
if (currentVersion < latestVersion) {
return { npmTag: "v" + branch };
const npmTag = "v" + branch;
log(`Expected npm tag: ${npmTag}`);
return { npmTag };
}

// Publishing a new latest version
if (tag === "latest") {
log(`Expected npm tag: ${tag}`);
return { npmTag: tag };
}

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

/**
* @param {string} file
* @param {string} tag
* @returns {void}
*/
function verifyPublishPipeline(file, tag) {
const data = fs.readFileSync(file, { encoding: "utf-8" });
const m = data.match(/publishTag: '(\w*?)'/);
if (!m) {
throw new Error(`${file}: Could not find npm publish tag`);
}

if (m[1] !== tag) {
throw new Error(`${file}: 'publishTag' needs to be set to '${tag}'`);
}
}

/**
* Verifies the configuration and enables publishing on CI.
* @param {NxConfig} config
Expand Down Expand Up @@ -182,30 +218,10 @@ function enablePublishing(config, currentBranch, tag, prerelease) {
throw new Error("Nx Release is not correctly configured for the current branch");
}

verifyPublishPipeline(ADO_PUBLISH_PIPELINE, tag);
enablePublishingOnAzurePipelines();
}

/**
* @param {string} file
* @param {string} tag
* @returns {boolean}
*/
function verifyPublishPipeline(file, tag) {
const data = fs.readFileSync(file, { encoding: "utf-8" });
const m = data.match(/publishTag: '(\w*?)'/);
if (!m) {
error(`${file}: Could not find npm publish tag`);
return false;
}

if (m[1] !== tag) {
error(`${file}: 'publishTag' needs to be set to '${tag}'`);
return false;
}

return true;
}

/**
* @param {Options} options
* @returns {number}
Expand All @@ -217,16 +233,14 @@ function main(options) {
return 1;
}

if (!verifyPublishPipeline(ADO_PUBLISH_PIPELINE, options.tag || NPM_TAG_NEXT)) {
return 1;
}
const logger = options.verbose ? info : () => undefined;

const config = loadNxConfig(NX_CONFIG_FILE);
try {
if (isMainBranch(branch)) {
enablePublishing(config, branch, NPM_TAG_NIGHTLY, NPM_TAG_NIGHTLY);
} else if (isStableBranch(branch)) {
const { npmTag, prerelease } = getTagForStableBranch(branch, options);
const { npmTag, prerelease } = getTagForStableBranch(branch, options, logger);
enablePublishing(config, branch, npmTag, prerelease);
}
} catch (e) {
Expand All @@ -236,7 +250,7 @@ function main(options) {
fs.writeSync(fd, "\n");
fs.closeSync(fd)
} else {
console.error(`${e}`);
error(`${e.message}`);
}
return 1;
}
Expand All @@ -255,6 +269,10 @@ const { values } = util.parseArgs({
type: "boolean",
default: false,
},
verbose: {
type: "boolean",
default: false,
},
},
strict: true,
});
Expand Down
4 changes: 2 additions & 2 deletions .ado/templates/npm-publish-steps.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
# If this is a new stable branch, change `publishTag` to `latest` when going stable
publishTag: 'next'
publishTag: 'nightly'

steps:
- checkout: self
Expand All @@ -15,7 +15,7 @@ steps:
displayName: Install npm dependencies

- script: |
node .ado/scripts/prepublish-check.mjs --tag ${{ parameters['publishTag'] }}
node .ado/scripts/prepublish-check.mjs --verbose --tag ${{ parameters['publishTag'] }}
displayName: Verify release config

- script: |
Expand Down
Loading