Skip to content

Commit

Permalink
Add utilities to publish unstable versions every commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Feb 17, 2023
1 parent 565e9e5 commit 7846411
Show file tree
Hide file tree
Showing 8 changed files with 1,749 additions and 61 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/publish-unstable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# For every push to the master branch, this publishes an NPM package to the
# "unstable" NPM tag.

name: Publish Unstable

on:
push:
branches:
- main

concurrency:
group: ci-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
publish:
name: "NPM Publish"
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup
- name: Install Node
uses: actions/setup-node@v3
with:
node: 18
# This creates an .npmrc that reads the NODE_AUTH_TOKEN environment variable
registry-url: 'https://registry.npmjs.org'

- name: set versions
run: node ./test-packages/unstable-release/version.js

- name: npm publish
run: node ./test-packages/unstable-release/publish.js
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"registry": "https://registry.npmjs.org"
},
"volta": {
"node": "14.19.3",
"node": "18.14.1",
"yarn": "1.22.19"
},
"version": "1.8.3"
Expand Down
5 changes: 4 additions & 1 deletion test-packages/support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"qunit": "^2.16.0",
"webpack": "^5"
},
"engines": {
"node": ">= 18.0.0"
},
"volta": {
"extends": "../../package.json"
"node": ">= 18.0.0"
}
}
9 changes: 9 additions & 0 deletions test-packages/unstable-release/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @embroider/unstable-release

Tools for supporting unstable releases off `main`.

_The changes this package makes to the monorepo should not be committed_.

```bash
node ./test-packages/unstable-release/version.js
```
10 changes: 10 additions & 0 deletions test-packages/unstable-release/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@embroider/unstable-release",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"ember-apply": "^2.6.5",
"execa": "^7.0.0"
}
}
19 changes: 19 additions & 0 deletions test-packages/unstable-release/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { execaCommand } from 'execa';
import { project } from 'ember-apply';

async function publish() {
for await (let workspace of await project.eachWorkspace()) {
if (await isPrivate()) continue;

console.info(`Publishing ${workspace}`);
await execaCommand('npm publish --tag=unstable --verbose');
}
}

publish();

async function isPrivate() {
let pkg = await packageJson.read();

return pkg.private ?? false;
}
75 changes: 75 additions & 0 deletions test-packages/unstable-release/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import path from 'node:path';

import { execaCommand } from 'execa';
import { packageJson, project } from 'ember-apply';

/**
* This is an I/O heavy way to do this, but hopefully it reads easy
*
* these functions change the CWD as they go, returnning to he previous
* CWD via finally blocks upon finish.
*/
async function updateVersions() {
let sha = await currentSHA();
let root = await project.gitRoot();

// Pick new versions for each package
for await (let workspace of await project.eachWorkspace()) {
if (await isPrivate()) continue;

console.info(`Setting version of ${path.relative(root, workspace)}`);
await setVersion(sha);
}

// Update each dependency to use the new versions
for await (let workspace of await project.eachWorkspace()) {
if (await isPrivate()) continue;

console.info(`Updating dependencies of ${path.relative(root, workspace)}`);
await updateDependencies();
}
}

updateVersions();

////////////////////////////////////////////

async function isPrivate() {
let pkg = await packageJson.read();

return pkg.private ?? false;
}

const NEW_VERSIONS = {};

async function setVersion(sha) {
await packageJson.modify(json => {
json.version = `${json.version}-unstable.${sha}`;

NEW_VERSIONS[json.name] = json.version;
});
}

async function updateDependencies() {
await packageJson.modify(json => {
for (let [dep, version] of Object.entries(NEW_VERSIONS)) {
if ((json.dependencies || {})[dep]) {
json.dependencies[dep] = version;
}

if ((json.devDependencies || {})[dep]) {
json.devDependencies[dep] = version;
}

if ((json.peerDependencies || {})[dep]) {
json.peerDependencies[dep] = version;
}
}
});
}

async function currentSHA() {
let { stdout } = await execaCommand(`git rev-parse --short HEAD`);

return stdout.trim();
}
Loading

0 comments on commit 7846411

Please sign in to comment.