Skip to content

Commit

Permalink
Automatically publish PRs from non-forks (#148)
Browse files Browse the repository at this point in the history
Collaborators can get a published npm package, as a treat.
  • Loading branch information
stevenpetryk authored Mar 25, 2024
1 parent 143bbca commit 2caa922
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ jobs:
cache: "pnpm"
- run: pnpm install
- run: pnpm tsc

publish-branch:
runs-on: ubuntu-latest
environment: publish
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
- uses: actions/setup-node@v4
with:
cache: "pnpm"
- run: pnpm install
- run: pnpm publish-branch
File renamed without changes.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"main": "build/index.js",
"module": "build/index.mjs",
"types": "build/index.d.ts",
"type": "module",
"packageManager": "[email protected]",
"engines": {
"node": ">=18.0.0"
Expand All @@ -31,7 +30,8 @@
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"format": "prettier --write .",
"format:check": "prettier --check .",
"prepublishOnly": "pnpm test && pnpm lint && pnpm build"
"prepublishOnly": "pnpm test && pnpm lint && pnpm build",
"publish-branch": "tsx scripts/publish-branch.ts"
},
"devDependencies": {
"@microsoft/api-extractor": "7.40.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "node:fs"
import { fileURLToPath } from "node:url"
import path from "node:path"

import docgen from "react-docgen-typescript"
import * as docgen from "react-docgen-typescript"

const projectRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..")
const tsConfigPath = path.join(projectRoot, "tsconfig.json")
Expand Down
22 changes: 22 additions & 0 deletions scripts/publish-branch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import childProcess from "node:child_process"
import fs from "node:fs"

const shortCommit = childProcess.execSync("git rev-parse --short HEAD", { encoding: "utf8" }).trim()

const version = `0.0.0-${shortCommit}`

console.log(`Publishing ${version} to npm...`)

console.log("NPM_TOKEN", process.env.NPM_TOKEN?.length)

const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8"))
packageJson.version = version
fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2))

fs.writeFileSync(".npmrc", "//registry.npmjs.org/:_authToken=${NPM_TOKEN}")

childProcess.execSync("pnpm build", { stdio: "inherit" })
childProcess.execSync("npm publish --ignore-scripts --access public --tag experimental", {
stdio: "inherit",
env: { ...process.env },
})

0 comments on commit 2caa922

Please sign in to comment.