Skip to content

Commit f4ea289

Browse files
ci: add npm publish workflow
Now on each push, a new version will be publish on GitHub' NPM registry.
1 parent 1077d56 commit f4ea289

File tree

4 files changed

+106
-22
lines changed

4 files changed

+106
-22
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Publish package to GitHub Packages
2+
3+
on:
4+
push:
5+
tags: [v*.*.*]
6+
branches: [main, master, release, development]
7+
paths:
8+
- test/**
9+
- lib/**
10+
- src/**
11+
- '*package*'
12+
- '*tsconfig*'
13+
- '*vitest*'
14+
- '*eslint*'
15+
- '*prettier*'
16+
17+
permissions:
18+
contents: read
19+
packages: write
20+
21+
jobs:
22+
build:
23+
name: Publish new NPM package
24+
runs-on: ubuntu-latest
25+
env:
26+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/[email protected]
30+
- name: Set up Node.js
31+
uses: actions/[email protected]
32+
with:
33+
node-version: 24
34+
cache-dependency-path: package-lock.json
35+
registry-url: 'https://npm.pkg.github.com'
36+
cache: 'npm'
37+
- name: Read `package.json` file and output contents
38+
id: package
39+
run: |
40+
version=$(node --print "require('#package').version")
41+
echo "version=$version" >> $GITHUB_OUTPUT
42+
echo "PACKAGE_VERSION=$version" >> $GITHUB_ENV
43+
name=$(node --print "require('#package').name")
44+
echo "name=$name" >> $GITHUB_OUTPUT
45+
echo "PACKAGE_NAME=$name" >> $GITHUB_ENV
46+
- name: Determine version and tag
47+
id: version
48+
run: |
49+
if [[ "${GITHUB_REF}" == refs/tags/* ]]; then
50+
echo "TAG_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
51+
echo "npm_tag=latest" >> $GITHUB_OUTPUT
52+
echo "is_release=true" >> $GITHUB_OUTPUT
53+
else
54+
echo "npm_tag=alpha" >> $GITHUB_OUTPUT
55+
echo "is_release=false" >> $GITHUB_OUTPUT
56+
fi
57+
- name: Verify tag matches package.json
58+
if: steps.version.outputs.is_release == 'true'
59+
run: |
60+
if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
61+
echo -n "Error: package.json version ($PACKAGE_VERSION)"
62+
echo " does not match tag ($TAG_VERSION)"
63+
exit 1
64+
fi
65+
- name: Install all dependencies
66+
run: npm ci
67+
- name: Build the project
68+
run: npm run build
69+
- name: Ensure we are not pushing a broken version
70+
run: npm run test
71+
- name: Turn links into permalinks on the README
72+
run: |
73+
file="README.md"
74+
domain="github.com"
75+
repo="${{ github.repository }}"
76+
commit="${{ github.sha }}"
77+
link="https://$domain/$repo/blob/$commit"
78+
sed --in-place "s#\(\[.*\]\)(\./\([^)]*\))#\1($link/\2)#g" "$file"
79+
sed --in-place "s#\(src\|srcset\)=\"\./#\1=\"$link/#g" "$file"
80+
- name: Publish NPM package to registry (tags)
81+
if: steps.version.outputs.is_release == 'true'
82+
run: |
83+
npm publish --tag ${{ steps.version.outputs.npm_tag }}
84+
IFS='.' read -r MAJOR MINOR PATCH <<< "$PACKAGE_VERSION"
85+
npm dist-tag add "$PACKAGE_NAME@$PACKAGE_VERSION" "$MAJOR.$MINOR"
86+
npm dist-tag add "$PACKAGE_NAME@$PACKAGE_VERSION" "$MAJOR"
87+
- name: Publish NPM package to registry (push)
88+
if: steps.version.outputs.is_release != 'true'
89+
run: |
90+
SHORT_SHA=${GITHUB_SHA:0:7}
91+
npm version prerelease --preid=alpha-$SHORT_SHA --no-git-tag-version
92+
npm publish --tag ${{ steps.version.outputs.npm_tag }}
93+

package-lock.json

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
2-
"name": "slyde",
2+
"name": "@tygo-van-den-hurk/slyde",
33
"description": "Make beautifully animated Slydes and presentations from XML with ease!",
44
"homepage": "https://Tygo-van-den-Hurk.github.io/Slyde",
55
"author": "Tygo van den Hurk",
66
"version": "0.0.1",
7-
"readme": "README.md",
8-
"private": true,
7+
"private": false,
98
"repository": {
109
"type": "git",
1110
"url": "git://github.com/Tygo-van-den-Hurk/Slyde.git"
@@ -16,13 +15,19 @@
1615
"maintainers": [
1716
"Tygo van den Hurk"
1817
],
19-
"bin": "./dist/src/cli.js",
18+
"bin": {
19+
"slyde": "./dist/src/cli.js"
20+
},
2021
"main": "./dist/lib/index.js",
2122
"exports": "./dist/lib/index.js",
2223
"types": "./dist/lib/index.d.ts",
2324
"files": [
2425
"./dist"
2526
],
27+
"publishConfig": {
28+
"registry": "https://npm.pkg.github.com/",
29+
"access": "public"
30+
},
2631
"type": "module",
2732
"imports": {
2833
"#src/*": "./dist/src/*.js",
@@ -83,4 +88,4 @@
8388
"vite-tsconfig-paths": "^5.1.4",
8489
"vitest": "^3.2.4"
8590
}
86-
}
91+
}

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import yargs from 'yargs';
1414

1515
// Helpers
1616

17-
const NAME = pkg.name;
17+
const NAME = 'slyde';
1818
const VERSION = pkg.version;
1919
const EPILOGUE = `
2020
For the documentation go to ${chalk.underline.cyan(pkg.homepage)}. You can report bugs or create request features by opening an

0 commit comments

Comments
 (0)