-
Notifications
You must be signed in to change notification settings - Fork 0
129 lines (113 loc) · 3.83 KB
/
Copy pathpublish-npm.yml
File metadata and controls
129 lines (113 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: Publish JS SDK
on:
push:
branches:
- main
paths:
- "**"
- ".github/workflows/publish-npm.yml"
workflow_dispatch: {}
jobs:
publish:
if: ${{ github.actor != 'github-actions[bot]' }}
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: Check out repo
uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: "24"
registry-url: "https://registry.npmjs.org"
- name: Upgrade npm for trusted publishing
run: npm install -g npm@11.5.1
- name: Install dependencies
run: npm ci
- name: Decide version
id: version
run: |
current=$(node -p "require('./package.json').version")
published=$(npm view diffio version 2>/dev/null || true)
echo "current=$current"
echo "published=$published"
CURRENT="$current" PUBLISHED="$published" node - <<'NODE'
const fs = require("fs");
const current = process.env.CURRENT;
const published = process.env.PUBLISHED;
const parse = (value) => {
const core = value.split("-")[0].split("+")[0];
const parts = core.split(".").map((item) => parseInt(item, 10));
while (parts.length < 3) {
parts.push(0);
}
return parts.slice(0, 3);
};
const compare = (left, right) => {
const a = parse(left);
const b = parse(right);
for (let i = 0; i < 3; i += 1) {
if (a[i] > b[i]) return 1;
if (a[i] < b[i]) return -1;
}
return 0;
};
const bumpPatch = (value) => {
const [major, minor, patch] = parse(value);
return `${major}.${minor}.${patch + 1}`;
};
let nextVersion = current;
let bumpNeeded = false;
if (published) {
const comparison = compare(current, published);
if (comparison <= 0) {
nextVersion = bumpPatch(comparison === 0 ? current : published);
bumpNeeded = true;
}
}
const publish =
!published || (published && compare(nextVersion, published) > 0);
const output = process.env.GITHUB_OUTPUT;
if (output) {
fs.appendFileSync(output, `next_version=${nextVersion}\n`);
fs.appendFileSync(
output,
`bump_needed=${bumpNeeded ? "true" : "false"}\n`
);
fs.appendFileSync(
output,
`publish=${publish ? "true" : "false"}\n`
);
}
NODE
- name: Bump package.json version
if: ${{ steps.version.outputs.bump_needed == 'true' }}
run: npm version "${{ steps.version.outputs.next_version }}" --no-git-tag-version
- name: Sync SDK version constant
env:
NEXT_VERSION: ${{ steps.version.outputs.next_version }}
run: |
node - <<'NODE'
const fs = require("fs");
const path = "src/version.ts";
const nextVersion = process.env.NEXT_VERSION;
if (!nextVersion) {
process.exit(0);
}
const contents = fs.readFileSync(path, "utf8");
const updated = contents.replace(
/DIFFIO_SDK_VERSION\s*=\s*["'][^"']+["']/,
`DIFFIO_SDK_VERSION = "${nextVersion}"`
);
if (updated !== contents) {
fs.writeFileSync(path, updated);
}
NODE
- name: Build
if: ${{ steps.version.outputs.publish == 'true' }}
run: npm run build
- name: Publish to npm
if: ${{ steps.version.outputs.publish == 'true' }}
run: npm publish --access public