Skip to content

Commit

Permalink
build: combine publish workflows into single workflow, check packages…
Browse files Browse the repository at this point in the history
… in order
  • Loading branch information
paradoxuum committed Jul 20, 2024
1 parent 94163a0 commit f303c0b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 211 deletions.
39 changes: 0 additions & 39 deletions .github/scripts/check-changed.mjs

This file was deleted.

18 changes: 0 additions & 18 deletions .github/scripts/get-version.mjs

This file was deleted.

26 changes: 26 additions & 0 deletions .github/scripts/publish.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { execSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";
import { getChangedPackages, getDevVersion } from "./util.mjs";

const nightly = process.argv.length > 2 && process.argv[2] === "true";
const tag = nightly ? "next" : "latest";
for (const pkg of getChangedPackages(nightly)) {
const packagePath = resolve(pkg, "package.json");
if (!existsSync(packagePath)) {
console.error(`Could not find package.json at ${packagePath}`);
process.exit(1);
}

const packageJson = JSON.parse(readFileSync(packagePath, "utf8"));
const pkgName = packageJson.name;

if (nightly) {
const version = getDevVersion(packageJson.version);
execSync(`yarn workspace ${pkgName} version ${version}`);
console.log(`Set ${pkgName} version to ${version}`);
}

execSync(`yarn workspace ${pkgName} npm publish --tag ${tag}`);
console.log(`Published ${pkgName}@${tag} to npm`);
}
48 changes: 48 additions & 0 deletions .github/scripts/util.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
import { execSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { resolve } from "node:path";

export function getChangedPackages(nightly) {
const packages = [];

if (isPackageChanged("packages/core", nightly)) {
packages.push("packages/core");
}

if (isPackageChanged("packages/ui", nightly)) {
packages.push("packages/ui");
}

return packages;
}

function isPackageChanged(directory, nightly) {
const packagePath = resolve(directory, "package.json");
if (!existsSync(packagePath)) {
throw `Could not find package.json at ${packagePath}`;
}

const packageJson = JSON.parse(readFileSync(packagePath, "utf8"));
const tag = nightly ? "next" : "latest";
const latest = execSync(`npm view ${packageJson.name} dist-tags.${tag}`)
.toString("utf8")
.trim();

if (nightly) {
const hash = latest.split("-").pop();
const result = execSync(
`git diff --quiet ${hash} HEAD -- ${directory} || echo changed`,
)
.toString("utf8")
.trim();

if (result === "changed") return true;
} else if (
packageJson.version.localeCompare(latest, undefined, {
numeric: true,
}) === 1
) {
return true;
}

return false;
}

export function getDevVersion(version) {
const [major, minor, patch] = version
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Publish packages

on:
workflow_dispatch:
schedule:
- cron: "0 7 * * *"
push:
branches:
- main
paths:
- packages/core/package.json

jobs:
publish:
runs-on: ubuntu-latest
outputs:
nightly: ${{steps.nightly.outputs.nightly }}
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Enable Corepack
run: corepack enable

- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: "https://registry.npmjs.org"
cache: "yarn"

- name: Install dependencies
run: yarn install

- name: Check nightly status
id: nightly
run: echo "nightly=${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}" >> "$GITHUB_OUTPUT"

- name: Publish changed packages
run: node .github/scripts/publish.mjs ${{ steps.nightly.outputs.nightly }}
77 changes: 0 additions & 77 deletions .github/workflows/release-core.yml

This file was deleted.

77 changes: 0 additions & 77 deletions .github/workflows/release-ui.yml

This file was deleted.

0 comments on commit f303c0b

Please sign in to comment.