1111
1212jobs :
1313 publish :
14+ if : ${{ github.actor != 'github-actions[bot]' }}
1415 runs-on : ubuntu-latest
1516 permissions :
16- contents : read
17+ contents : write
1718 id-token : write
1819 steps :
1920 - name : Check out repo
@@ -31,23 +32,113 @@ jobs:
3132 - name : Install dependencies
3233 run : npm ci
3334
34- - name : Decide whether to publish
35- id : should_publish
35+ - name : Decide version
36+ id : version
3637 run : |
3738 current=$(node -p "require('./package.json').version")
3839 published=$(npm view diffio version 2>/dev/null || true)
3940 echo "current=$current"
4041 echo "published=$published"
41- if [ "$current" = "$published" ]; then
42- echo "publish=false" >> "$GITHUB_OUTPUT"
42+ CURRENT="$current" PUBLISHED="$published" node - <<'NODE'
43+ const fs = require("fs");
44+ const current = process.env.CURRENT;
45+ const published = process.env.PUBLISHED;
46+
47+ const parse = (value) => {
48+ const core = value.split("-")[0].split("+")[0];
49+ const parts = core.split(".").map((item) => parseInt(item, 10));
50+ while (parts.length < 3) {
51+ parts.push(0);
52+ }
53+ return parts.slice(0, 3);
54+ };
55+
56+ const compare = (left, right) => {
57+ const a = parse(left);
58+ const b = parse(right);
59+ for (let i = 0; i < 3; i += 1) {
60+ if (a[i] > b[i]) return 1;
61+ if (a[i] < b[i]) return -1;
62+ }
63+ return 0;
64+ };
65+
66+ const bumpPatch = (value) => {
67+ const [major, minor, patch] = parse(value);
68+ return `${major}.${minor}.${patch + 1}`;
69+ };
70+
71+ let nextVersion = current;
72+ let bumpNeeded = false;
73+ if (published) {
74+ const comparison = compare(current, published);
75+ if (comparison <= 0) {
76+ nextVersion = bumpPatch(comparison === 0 ? current : published);
77+ bumpNeeded = true;
78+ }
79+ }
80+
81+ const publish =
82+ !published || (published && compare(nextVersion, published) > 0);
83+
84+ const output = process.env.GITHUB_OUTPUT;
85+ if (output) {
86+ fs.appendFileSync(output, `next_version=${nextVersion}\n`);
87+ fs.appendFileSync(
88+ output,
89+ `bump_needed=${bumpNeeded ? "true" : "false"}\n`
90+ );
91+ fs.appendFileSync(
92+ output,
93+ `publish=${publish ? "true" : "false"}\n`
94+ );
95+ }
96+ NODE
97+
98+ - name : Bump package.json version
99+ if : ${{ steps.version.outputs.bump_needed == 'true' }}
100+ run : npm version "${{ steps.version.outputs.next_version }}" --no-git-tag-version
101+
102+ - name : Sync SDK version constant
103+ id : sync_version
104+ env :
105+ NEXT_VERSION : ${{ steps.version.outputs.next_version }}
106+ run : |
107+ node - <<'NODE'
108+ const fs = require("fs");
109+ const path = "src/version.ts";
110+ const nextVersion = process.env.NEXT_VERSION;
111+ if (!nextVersion) {
112+ process.exit(0);
113+ }
114+ const contents = fs.readFileSync(path, "utf8");
115+ const updated = contents.replace(
116+ /DIFFIO_SDK_VERSION\s*=\s*["'][^"']+["']/,
117+ `DIFFIO_SDK_VERSION = "${nextVersion}"`
118+ );
119+ if (updated !== contents) {
120+ fs.writeFileSync(path, updated);
121+ }
122+ NODE
123+ if git diff --quiet -- package.json package-lock.json src/version.ts; then
124+ echo "changed=false" >> "$GITHUB_OUTPUT"
43125 else
44- echo "publish =true" >> "$GITHUB_OUTPUT"
126+ echo "changed =true" >> "$GITHUB_OUTPUT"
45127 fi
46128
129+ - name : Commit version update
130+ if : ${{ steps.sync_version.outputs.changed == 'true' }}
131+ run : |
132+ git config user.name "github-actions[bot]"
133+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
134+ git add package.json package-lock.json src/version.ts
135+ git commit -m "chore(release): bump JS SDK to ${{ steps.version.outputs.next_version }}"
136+ git push origin "HEAD:${GITHUB_REF_NAME}"
137+
47138 - name : Build
48- if : ${{ steps.should_publish .outputs.publish == 'true' }}
139+ if : ${{ steps.version .outputs.publish == 'true' }}
49140 run : npm run build
50141
51142 - name : Publish to npm
52- if : ${{ steps.should_publish .outputs.publish == 'true' }}
143+ if : ${{ steps.version .outputs.publish == 'true' }}
53144 run : npm publish --access public
0 commit comments