Skip to content

Commit

Permalink
chore: set up proper publishing pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Nov 1, 2024
1 parent 2b33fbb commit a9fe079
Show file tree
Hide file tree
Showing 8 changed files with 1,097 additions and 1,202 deletions.
2 changes: 1 addition & 1 deletion .ado/jobs/npm-publish-dry-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
submodules: recursive # set to 'true' for a single level of submodules or 'recursive' to get submodules of submodules
persistCredentials: true # set to 'true' to leave the OAuth token in the Git config after the initial fetch

- template: /.ado/templates/apple-steps-publish.yml@self
- template: /.ado/templates/npm-publish.yml@self
parameters:
build_type: 'dry-run'
86 changes: 86 additions & 0 deletions .ado/scripts/prepublish-check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// @ts-check
import { spawnSync } from "node:child_process";
import * as fs from "node:fs";
import * as util from "node:util";

/** @typedef {typeof import("../../nx.json")} NxConfig */

/**
* @returns {NxConfig}
*/
function loadNxConfig(configFile = "nx.json") {
const nx = fs.readFileSync(configFile, { encoding: "utf-8" });
return JSON.parse(nx);
}

/**
* @returns {string}
*/
function getCurrentBranch() {
const { stdout } = spawnSync("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
return stdout.toString().trim();
}

/**
* @param {string} branch
*/
function isStableBranch(branch) {
return /^\d+\.\d+-stable$/.test(branch);
}

/**
* @param {NxConfig["release"]} config
* @param {string} tag
* @param {string} [prerelease]
* @returns {asserts config is NxConfig["release"]}
*/
function enablePublishing(config, tag, prerelease) {
/** @type {string[]} */
const errors = [];

const { currentVersionResolverMetadata, preid } = config.version.generatorOptions;
if (preid !== prerelease) {
errors.push(`'release.version.generatorOptions.preid' must be set to '${prerelease || ""}'`);
}

if (currentVersionResolverMetadata.tag !== tag) {
errors.push(`'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to '${tag}'`);
}

if (errors.length > 0) {
for (const e of errors) {
console.error("❌", e);
}
throw new Error("Nx Release is not correctly configured for the current branch");
}

console.log(`##vso[task.setvariable variable=publish_react_native_macos]1`);
}

function main({ "release-candidate": releaseCandidate }) {
const branch = getCurrentBranch();
if (!branch) {
throw new Error("Could not get current branch");
}

const { defaultBase, release } = loadNxConfig();
if (branch === defaultBase) {
enablePublishing(release, "nightly", "nightly");
} else if (isStableBranch(branch)) {
enablePublishing(release, "v" + branch, releaseCandidate ? "rc" : undefined);
}
}

const { values } = util.parseArgs({
args: process.argv.slice(2),
options: {
"release-candidate": {
type: "boolean",
default: true
}
},
strict: true,
allowNegative: true,
});

main(values);
23 changes: 23 additions & 0 deletions .ado/templates/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
parameters:
build_type: ''

steps:
- script: |
yarn install
displayName: Install npm dependencies
- script: |
# If this is a stable branch, add `--no-release-candidate` when going stable
node .ado/scripts/prepublish-check.mjs
displayName: Verify release config
- script: |
yarn nx release --dry-run
displayName: Version and publish packages (dry run)
condition: ne(variables['publish_react_native_macos'], '1')
- script: |
#yarn nx release --yes
yarn nx release --dry-run
displayName: Version and publish packages
condition: eq(variables['publish_react_native_macos'], '1')
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,8 @@ vendor/
.ado/Brewfile.lock.json
.ado/verdaccio/htpasswd
.ado/verdaccio/storage/.verdaccio-db.json

# Nx
.nx/cache
.nx/workspace-data
# macOS]
30 changes: 30 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"defaultBase": "main",
"targetDefaults": {
"build": {
"dependsOn": [
"^build"
]
}
},
"release": {
"projects": [
"packages/react-native",
"packages/react-native-macos-init",
"packages/virtualized-lists"
],
"projectsRelationship": "independent",
"versionPlans": true,
"version": {
"generatorOptions": {
"currentVersionResolver": "registry",
"currentVersionResolverMetadata": {
"tag": "nightly"
},
"preid": "nightly",
"skipLockFileUpdate": true
}
}
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@babel/preset-flow": "^7.24.7",
"@definitelytyped/dtslint": "^0.0.127",
"@jest/create-cache-key-function": "^29.6.3",
"@nx/js": "~20.0.0",
"@pkgjs/parseargs": "^0.11.0",
"@react-native/metro-babel-transformer": "0.76.0-main",
"@react-native/metro-config": "0.76.0-main",
Expand Down Expand Up @@ -94,6 +95,7 @@
"mkdirp": "^0.5.1",
"node-fetch": "^2.2.0",
"nullthrows": "^1.1.1",
"nx": "~20.0.0",
"prettier": "2.8.8",
"prettier-plugin-hermes-parser": "0.23.1",
"react": "19.0.0-rc-fb9a90fa48-20240614",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
},
"dependencies": {
"@jest/create-cache-key-function": "^29.6.3",
"@react-native-mac/virtualized-lists": "0.76.0-main",
"@react-native-mac/virtualized-lists": "workspace:*",
"@react-native/assets-registry": "0.76.0-main",
"@react-native/codegen": "0.76.0-main",
"@react-native/community-cli-plugin": "0.76.0-main",
Expand Down
Loading

0 comments on commit a9fe079

Please sign in to comment.