Skip to content

Commit

Permalink
dx(npm): Attempt Auto-Publish
Browse files Browse the repository at this point in the history
Needs to be tested by being merged as this will run on GitHub Actions.
  • Loading branch information
FlorianWendelborn committed Apr 20, 2024
1 parent 39d403f commit 28ab75e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Packages to NPM

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

permissions:
id-token: write

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- uses: oven-sh/setup-bun@v1

- run: bun install

# update npm for provenance support
- run: npm --version && npm install -g npm && npm --version

# make extra sure this publish wouldn’t break anything
- run: bun run build check test

- name: publish
run: bun internals/scripts/source/publish.ts
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Binary file modified bun.lockb
Binary file not shown.
13 changes: 13 additions & 0 deletions internals/scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"dependencies": {
"zod": "^3.22.5"
},
"name": "@skyblock-finance/scripts",
"private": true,
"scripts": {
"check:eslint": "bun run eslint --max-warnings=0 .",
"check:prettier": "bun --bun run --cwd ../.. prettier --check internals/scripts",
"fix:eslint": "bun run check:eslint --fix",
"fix:prettier": "bun --bun run check:prettier --write"
}
}
46 changes: 46 additions & 0 deletions internals/scripts/source/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { $, semver } from 'bun'
import { z } from 'zod'

const packagesToConsider = ['packages/schemas']

const packageJsonSchema = z.object({ name: z.string(), version: z.string() })

const getRemoteVersion = async (name: string): Promise<string | null> => {
try {
return await $`npm view ${name} version`.text()
} catch {
return null
}
}

const toPublish: string[] = []

for (const path of packagesToConsider) {
const { name, version } = packageJsonSchema.parse(
await Bun.file(`./${path}/package.json`).json(),
)
const remoteVersion = await getRemoteVersion(name)

console.info(
`${path}: found package “${name}@${version}”, remote is ${remoteVersion ?? 'LIKELY UNPUBLISHED'}`,
)

const shouldPublish =
remoteVersion === null || semver.order(version, remoteVersion) === 1
if (!shouldPublish) {
console.info(`${path}: already up-to-date, skipping`)
continue
}

console.info(`${path}: scheduled for publishing`)
toPublish.push(name)
}

if (toPublish.length === 0) {
console.warn(`nothing to publish, exiting.`)
process.exit(0)
}

console.info(`attempting to publish ${toPublish.join(', ')}`)

await $`turbo run publish-package --continue ${toPublish.map((packageName) => `--filter=${packageName}`).join(' ')}`
4 changes: 4 additions & 0 deletions knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"entry": ["scripts/*.ts", "source/index.ts"],
"project": ["source/**/*.ts"]
},
"internals/scripts": {
"entry": ["source/*.ts"],
"project": ["source/**/*.ts"]
},
"packages/*": {
"entry": ["scripts/*.ts", "source/index.ts"],
"project": ["source/**/*.ts"]
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"download": "bun scripts/download-data.ts",
"fix:eslint": "bun run check:eslint --fix",
"fix:prettier": "bun --bun run check:prettier --write",
"publish-package": "npm publish --provenance",
"start": "bun scripts/check-all.ts",
"watch": "nodemon -e ts,json --watch source --watch scripts --exec \"bun run start\""
},
Expand Down
7 changes: 6 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"fix:prettier": {
"inputs": ["**/*.{css,js,json,md,scss,ts}"],
"outputMode": "new-only"
}
},
"publish-package": {
"cache": false,
"dependsOn": ["build", "check", "test"]
},
"test": {}
}
}

0 comments on commit 28ab75e

Please sign in to comment.