Skip to content

Commit e6463eb

Browse files
committed
refactor checks
1 parent 05ee66d commit e6463eb

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

.ado/scripts/prepublish-check.mjs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import * as fs from "node:fs";
44
import * as util from "node:util";
55

66
const ADO_PUBLISH_PIPELINE = ".ado/templates/npm-publish.yml";
7-
const NPM_TAG_NEXT = "next";
87
const NX_CONFIG_FILE = "nx.json";
98

9+
const NPM_TAG_NEXT = "next";
10+
const NPM_TAG_NIGHTLY = "nightly";
11+
1012
/**
1113
* @typedef {typeof import("../../nx.json")} NxConfig
1214
* @typedef {{ tag?: string; update?: boolean; }} Options
@@ -23,6 +25,14 @@ function enablePublishingOnAzurePipelines() {
2325
console.log(`##vso[task.setvariable variable=publish_react_native_macos]1`);
2426
}
2527

28+
/**
29+
* Logs an error message to the console.
30+
* @param {string} message
31+
*/
32+
function error(message) {
33+
console.error("❌", message);
34+
}
35+
2636
/**
2737
* Returns whether the given branch is considered main branch.
2838
* @param {string} branch
@@ -168,9 +178,7 @@ function enablePublishing(config, currentBranch, tag, prerelease) {
168178
}
169179

170180
if (errors.length > 0) {
171-
for (const e of errors) {
172-
console.error("❌", e);
173-
}
181+
errors.forEach(error);
174182
throw new Error("Nx Release is not correctly configured for the current branch");
175183
}
176184

@@ -180,40 +188,48 @@ function enablePublishing(config, currentBranch, tag, prerelease) {
180188
/**
181189
* @param {string} file
182190
* @param {string} tag
191+
* @returns {boolean}
183192
*/
184193
function verifyPublishPipeline(file, tag) {
185194
const data = fs.readFileSync(file, { encoding: "utf-8" });
186195
const m = data.match(/publishTag: '(\w*?)'/);
187196
if (!m) {
188-
throw new Error(`Could not find npm publish tag in '${file}'`);
197+
error(`${file}: Could not find npm publish tag`);
198+
return false;
189199
}
190200

191201
if (m[1] !== tag) {
192-
throw new Error(`${file}: 'publishTag' needs to be set to '${tag}'`);
202+
error(`${file}: 'publishTag' needs to be set to '${tag}'`);
203+
return false;
193204
}
205+
206+
return true;
194207
}
195208

196209
/**
197210
* @param {Options} options
211+
* @returns {number}
198212
*/
199213
function main(options) {
200214
const branch = getCurrentBranch();
201215
if (!branch) {
202-
throw new Error("Could not get current branch");
216+
error("Could not get current branch");
217+
return 1;
203218
}
204219

205-
verifyPublishPipeline(ADO_PUBLISH_PIPELINE, options.tag || NPM_TAG_NEXT);
220+
if (!verifyPublishPipeline(ADO_PUBLISH_PIPELINE, options.tag || NPM_TAG_NEXT)) {
221+
return 1;
222+
}
206223

207224
const config = loadNxConfig(NX_CONFIG_FILE);
208225
try {
209226
if (isMainBranch(branch)) {
210-
enablePublishing(config, branch, "nightly", "nightly");
227+
enablePublishing(config, branch, NPM_TAG_NIGHTLY, NPM_TAG_NIGHTLY);
211228
} else if (isStableBranch(branch)) {
212229
const { npmTag, prerelease } = getTagForStableBranch(branch, options);
213230
enablePublishing(config, branch, npmTag, prerelease);
214231
}
215232
} catch (e) {
216-
process.exitCode = 1;
217233
if (options.update) {
218234
const fd = fs.openSync(NX_CONFIG_FILE, "w");
219235
fs.writeSync(fd, JSON.stringify(config, undefined, 2));
@@ -222,7 +238,10 @@ function main(options) {
222238
} else {
223239
console.error(`${e}`);
224240
}
241+
return 1;
225242
}
243+
244+
return 0;
226245
}
227246

228247
const { values } = util.parseArgs({
@@ -240,4 +259,4 @@ const { values } = util.parseArgs({
240259
strict: true,
241260
});
242261

243-
main(values);
262+
process.exitCode = main(values);

0 commit comments

Comments
 (0)