Skip to content

Commit 54c63c5

Browse files
committed
feat(plugin): add publish to npm
1 parent 310580b commit 54c63c5

File tree

9 files changed

+2147
-2
lines changed

9 files changed

+2147
-2
lines changed

.github/workflows/main.yml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
name: CI & Release
3+
4+
# Workflow name based on selected inputs. Fallback to default Github naming when expression evaluates to empty string
5+
run-name: >-
6+
${{
7+
inputs.release && inputs.test && format('Build {0} ➤ Test ➤ Publish to NPM', github.ref_name) ||
8+
inputs.release && !inputs.test && format('Build {0} ➤ Skip Tests ➤ Publish to NPM', github.ref_name) ||
9+
github.event_name == 'workflow_dispatch' && inputs.test && format('Build {0} ➤ Test', github.ref_name) ||
10+
github.event_name == 'workflow_dispatch' && !inputs.test && format('Build {0} ➤ Skip Tests', github.ref_name) ||
11+
''
12+
}}
13+
14+
on:
15+
# Build on pushes branches that have a PR (including drafts)
16+
pull_request:
17+
# Build on commits pushed to branches without a PR if it's in the allowlist
18+
push:
19+
branches: [main]
20+
# https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow
21+
workflow_dispatch:
22+
inputs:
23+
test:
24+
description: Run tests
25+
required: true
26+
default: true
27+
type: boolean
28+
release:
29+
description: Release new version
30+
required: true
31+
default: false
32+
type: boolean
33+
34+
concurrency:
35+
# On PRs builds will cancel if new pushes happen before the CI completes, as it defines `github.head_ref` and gives it the name of the branch the PR wants to merge into
36+
# Otherwise `github.run_id` ensures that you can quickly merge a queue of PRs without causing tests to auto cancel on any of the commits pushed to main.
37+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
38+
cancel-in-progress: true
39+
40+
permissions:
41+
contents: read # for checkout
42+
43+
jobs:
44+
build:
45+
runs-on: ubuntu-latest
46+
name: Lint & Build
47+
steps:
48+
- uses: actions/checkout@v4
49+
- uses: actions/setup-node@v4
50+
with:
51+
cache: npm
52+
node-version: lts/*
53+
- run: npm clean-install
54+
# Linting can be skipped
55+
- run: npm run lint --if-present
56+
if: github.event.inputs.test != 'false'
57+
# But not the build script, as semantic-release will crash if this command fails so it makes sense to test it early
58+
- run: npm run prepublishOnly --if-present
59+
60+
test:
61+
needs: build
62+
# The test matrix can be skipped, in case a new release needs to be fast-tracked and tests are already passing on main
63+
if: github.event.inputs.test != 'false'
64+
runs-on: ${{ matrix.os }}
65+
name: Node.js ${{ matrix.node }} / ${{ matrix.os }}
66+
strategy:
67+
# A test failing on windows doesn't mean it'll fail on macos. It's useful to let all tests run to its completion to get the full picture
68+
fail-fast: false
69+
matrix:
70+
# Run the testing suite on each major OS with the latest LTS release of Node.js
71+
os: [macos-latest, ubuntu-latest, windows-latest]
72+
node: [lts/*]
73+
# It makes sense to also test the oldest, and latest, versions of Node.js, on ubuntu-only since it's the fastest CI runner
74+
include:
75+
- os: ubuntu-latest
76+
# Test the oldest LTS release of Node that's still receiving bugfixes and security patches, versions older than that have reached End-of-Life
77+
node: lts/-1
78+
- os: ubuntu-latest
79+
# Test the actively developed version that will become the latest LTS release next October
80+
node: current
81+
steps:
82+
# It's only necessary to do this for windows, as mac and ubuntu are sane OS's that already use LF
83+
- name: Set git to use LF
84+
if: matrix.os == 'windows-latest'
85+
run: |
86+
git config --global core.autocrlf false
87+
git config --global core.eol lf
88+
- uses: actions/checkout@v4
89+
- uses: actions/setup-node@v4
90+
with:
91+
cache: npm
92+
node-version: ${{ matrix.node }}
93+
- run: npm install
94+
- run: npm test --if-present
95+
96+
release:
97+
permissions:
98+
contents: write # to be able to publish a GitHub release
99+
issues: write # to be able to comment on released issues
100+
pull-requests: write # to be able to comment on released pull requests
101+
id-token: write # to enable use of OIDC for npm provenance
102+
needs: [build, test]
103+
# only run if opt-in during workflow_dispatch
104+
if: always() && github.event.inputs.release == 'true' && needs.build.result != 'failure' && needs.test.result != 'failure' && needs.test.result != 'cancelled'
105+
runs-on: ubuntu-latest
106+
name: Semantic release
107+
steps:
108+
- uses: actions/checkout@v4
109+
with:
110+
# Need to fetch entire commit history to
111+
# analyze every commit since last release
112+
fetch-depth: 0
113+
- uses: actions/setup-node@v4
114+
with:
115+
cache: npm
116+
node-version: lts/*
117+
- run: npm clean-install
118+
- run: npm audit signatures
119+
# Branches that will release new versions are defined in .releaserc.json
120+
# @TODO remove --dry-run after verifying everything is good to go
121+
- run: npx semantic-release --dry-run
122+
# Don't allow interrupting the release step if the job is cancelled, as it can lead to an inconsistent state
123+
# e.g. git tags were pushed but it exited before `npm publish`
124+
if: always()
125+
env:
126+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
127+
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no -- commitlint --edit ""

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx lint-staged

.releaserc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "@sanity/semantic-release-preset",
3+
"branches": ["main"]
4+
}

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ with default configuration for build & watch scripts.
5656

5757
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
5858
on how to run this plugin with hotreload in the studio.
59+
60+
61+
### Release new version
62+
63+
Run ["CI & Release" workflow](TODO/actions/workflows/main.yml).
64+
Make sure to select the main branch and check "Release new version".
65+
66+
Semantic release will only release on configured branches, so it is safe to run release on any branch.

commitlint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
}

lint-staged.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
'**/*.{js,jsx}': ['eslint'],
3+
'**/*.{ts,tsx}': ['eslint', () => 'tsc --build'],
4+
}

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"link-watch": "plugin-kit link-watch",
3636
"lint": "eslint .",
3737
"prepublishOnly": "npm run build",
38-
"watch": "pkg-utils watch --strict"
38+
"watch": "pkg-utils watch --strict",
39+
"prepare": "husky install"
3940
},
4041
"dependencies": {
4142
"@sanity/incompatible-plugin": "^1.0.4",
@@ -44,8 +45,11 @@
4445
"xml2js": "^0.6.2"
4546
},
4647
"devDependencies": {
48+
"@commitlint/cli": "^19.3.0",
49+
"@commitlint/config-conventional": "^19.2.2",
4750
"@sanity/pkg-utils": "^6.8.18",
4851
"@sanity/plugin-kit": "^4.0.16",
52+
"@sanity/semantic-release-preset": "^4.1.7",
4953
"@types/react": "^18.3.3",
5054
"@types/xml2js": "^0.4.14",
5155
"@typescript-eslint/eslint-plugin": "^7.11.0",
@@ -56,6 +60,8 @@
5660
"eslint-plugin-prettier": "^5.1.3",
5761
"eslint-plugin-react": "^7.34.2",
5862
"eslint-plugin-react-hooks": "^4.6.2",
63+
"husky": "^9.0.11",
64+
"lint-staged": "^15.2.5",
5965
"prettier": "^3.3.0",
6066
"prettier-plugin-packagejson": "^2.5.0",
6167
"react": "^18.3.1",
@@ -72,4 +78,4 @@
7278
"engines": {
7379
"node": ">=18"
7480
}
75-
}
81+
}

0 commit comments

Comments
 (0)