Skip to content

Commit c5e83e8

Browse files
authored
SDK Improvements (#1)
1 parent 9c253c0 commit c5e83e8

31 files changed

+3433
-5685
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: Package Preview Publish
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
publish-preview:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Enable corepack
16+
run: corepack enable
17+
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: 20
22+
cache: "npm"
23+
24+
- name: Install dependencies
25+
run: npm install
26+
27+
- name: Build package
28+
run: npm run build
29+
30+
- name: Publish preview with pkg.pr.new
31+
run: npx pkg-pr-new publish --json output.json --comment=off
32+
33+
- name: Comment PR with install instructions
34+
uses: actions/github-script@v6
35+
with:
36+
script: |
37+
const fs = require('fs');
38+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
39+
if (!output.packages || output.packages.length === 0) {
40+
core.setFailed('No packages published by pkg.pr.new');
41+
return;
42+
}
43+
const pkg = output.packages[0];
44+
const installCmd = `npm i ${pkg.url}`;
45+
const badge = `[![pkg.pr.new](https://pkg.pr.new/badge/${context.repo.owner}/${context.repo.repo})](https://pkg.pr.new/~/${context.repo.owner}/${context.repo.repo})`;
46+
const body = `### 🚀 Package Preview Available!
47+
48+
${badge}
49+
50+
---
51+
52+
**Install this PR's preview build with npm:**
53+
54+
\`\`\`sh
55+
${installCmd}
56+
\`\`\`
57+
58+
- 📦 [Preview Package on pkg.pr.new](${pkg.url})
59+
- 🔗 [View this commit on GitHub](https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${pkg.commit})
60+
61+
---
62+
<sub>Preview powered by [pkg.pr.new](https://pkg.pr.new) — try new features instantly, no NPM release needed!</sub>
63+
`;
64+
65+
const botCommentIdentifier = '### 🚀 Package Preview Available!';
66+
67+
async function findBotComment(issueNumber) {
68+
if (!issueNumber) return null;
69+
const comments = await github.rest.issues.listComments({
70+
owner: context.repo.owner,
71+
repo: context.repo.repo,
72+
issue_number: issueNumber,
73+
});
74+
return comments.data.find((comment) =>
75+
comment.body.includes(botCommentIdentifier)
76+
);
77+
}
78+
79+
async function createOrUpdateComment(issueNumber) {
80+
if (!issueNumber) {
81+
console.log('No issue number provided. Cannot post or update comment.');
82+
return;
83+
}
84+
85+
const existingComment = await findBotComment(issueNumber);
86+
if (existingComment) {
87+
await github.rest.issues.updateComment({
88+
owner: context.repo.owner,
89+
repo: context.repo.repo,
90+
comment_id: existingComment.id,
91+
body: body,
92+
});
93+
} else {
94+
await github.rest.issues.createComment({
95+
issue_number: issueNumber,
96+
owner: context.repo.owner,
97+
repo: context.repo.repo,
98+
body: body,
99+
});
100+
}
101+
}
102+
103+
if (context.eventName === 'pull_request') {
104+
if (context.issue.number) {
105+
await createOrUpdateComment(context.issue.number);
106+
}
107+
}
108+
109+
permissions:
110+
contents: read
111+
pull-requests: write

.github/workflows/unit-tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Use Node.js 20.x
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: "20.x"
21+
cache: "npm"
22+
23+
- name: Install dependencies
24+
run: npm ci
25+
26+
- name: Run unit tests
27+
run: npm run test:unit

babel.config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

jest.config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)