Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dx(npm): Attempt Auto-Publish #3

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {}
}
}
Loading