Skip to content

Commit 0840b18

Browse files
authored
Merge pull request #77 from scrapbox-jp/copilot/fix-23021ea6-5d9d-44b9-9088-d6c00ca8bc47
Add npm publishing support for dual JSR and npm distribution
2 parents 869c807 + ed33b8c commit 0840b18

File tree

7 files changed

+225
-8
lines changed

7 files changed

+225
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on: [push, pull_request]
44

55
env:
66
DENO_VERSION: 2.x
7+
DENO_TLS_CA_STORE: system
78

89
jobs:
910
test:

.github/workflows/publish.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: publish
33

44
env:
55
DENO_VERSION: 2.x
6+
NODE_VERSION: 20.x
7+
DENO_TLS_CA_STORE: system
68

79
on:
810
push:
@@ -14,12 +16,34 @@ permissions:
1416
id-token: write
1517

1618
jobs:
17-
publish:
19+
publish-jsr:
1820
runs-on: ubuntu-latest
1921
steps:
2022
- uses: actions/checkout@v4
2123
- uses: denoland/setup-deno@v2
2224
with:
2325
deno-version: ${{ env.DENO_VERSION }}
24-
- name: Publish on tag
26+
- name: Publish to JSR
2527
run: deno run --allow-env --allow-run=deno --allow-read --allow-write=deno.jsonc jsr:@david/publish-on-tag@0.1.4
28+
29+
publish-npm:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v4
33+
- uses: denoland/setup-deno@v2
34+
with:
35+
deno-version: ${{ env.DENO_VERSION }}
36+
cache: true
37+
- uses: actions/setup-node@v4
38+
with:
39+
node-version: ${{ env.NODE_VERSION }}
40+
registry-url: "https://registry.npmjs.org"
41+
- name: Build npm package
42+
run: deno task npm:build
43+
env:
44+
VERSION: ${{ github.ref_name }}
45+
- name: Publish to npm
46+
run: npm publish --provenance --access=public
47+
working-directory: ./npm
48+
env:
49+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
docs/
2-
coverage/
2+
coverage/
3+
npm/

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
## @cosense/types
22

33
[![JSR](https://jsr.io/badges/@cosense/types)](https://jsr.io/@cosense/types)
4+
[![npm](https://img.shields.io/npm/v/@cosense/types)](https://www.npmjs.com/package/@cosense/types)
45
[![test](https://github.com/scrapbox-jp/types/workflows/ci/badge.svg)](https://github.com/scrapbox-jp/types/actions?query=workflow%3Aci)
56

7+
> **Node.js & npm Notice (since 0.11.1)**
8+
>
9+
> Now also published on **[npm](https://www.npmjs.com/package/@cosense/types)**.
10+
>
11+
> Node.js support is **experimental**: I mainly target Deno and browsers, so I
12+
> don't actively maintain Node.js compatibility. Some tests run, but there may
13+
> still be runtime or type gaps remaining. Please use it with that
14+
> understanding.
15+
>
16+
> That said, **issues / PRs to improve Node support are very welcome!**
17+
>
18+
> If you don't need a public npm package, you can consume the JSR version
19+
> directly—`npm` via a custom registry in `.npmrc`; `yarn` or `pnpm` need no
20+
> extra config. See the
21+
> [JSR docs](https://jsr.io/docs/using-packages#adding-a-package).
22+
623
Type definitions for [cosense](https://cosen.se)
724

825
## Getting Started
926

27+
This library provides TypeScript type definitions for Scrapbox/Cosense and
28+
supports both JSR (JavaScript Registry) and npm installation methods.
29+
30+
### Installation
31+
32+
#### Option 1: JSR (Recommended for Deno projects)
33+
1034
If you want to use type definitions for REST API, run
1135

1236
```bash
@@ -18,3 +42,51 @@ If you want to use type definitions for UserScript, run
1842
```bash
1943
deno add jsr:@cosense/types/userscript
2044
```
45+
46+
#### Option 2: npm (For Node.js projects)
47+
48+
Install via npm:
49+
50+
```bash
51+
npm install @cosense/types
52+
```
53+
54+
Import the library:
55+
56+
```typescript ignore
57+
// ESM syntax
58+
import type { PageList } from "@cosense/types/rest";
59+
import type { Scrapbox } from "@cosense/types/userscript";
60+
```
61+
62+
### Usage Examples
63+
64+
```typescript ignore
65+
// Example for JSR (Deno)
66+
import type { PageList } from "jsr:@cosense/types/rest";
67+
68+
const pages: PageList = {
69+
projectName: "example-project",
70+
skip: 0,
71+
limit: 100,
72+
count: 42,
73+
pages: [],
74+
};
75+
```
76+
77+
For npm users:
78+
79+
```typescript ignore
80+
// Example for npm (Node.js)
81+
import type { PageList } from "@cosense/types/rest";
82+
import type { Scrapbox } from "@cosense/types/userscript";
83+
84+
// Use the types in your Node.js application
85+
const pageData: PageList = {
86+
projectName: "my-project",
87+
skip: 0,
88+
limit: 50,
89+
count: 10,
90+
pages: [],
91+
};
92+
```

deno.jsonc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
"fix": "deno fmt && deno lint --fix && deno test --allow-read --doc --parallel --shuffle && deno publish --dry-run --allow-dirty",
66
"check": "deno fmt --check && deno lint && deno test --allow-read --doc --parallel --shuffle && deno publish --dry-run",
77
"coverage": "deno test --allow-read=./ --parallel --shuffle --coverage && deno coverage --html",
8-
"doc": "deno doc --html rest.ts userscript.ts websocket.ts"
8+
"doc": "deno doc --html rest.ts userscript.ts websocket.ts",
9+
"npm:build": "deno run -A scripts/build_npm.ts",
10+
"npm:check": "cd npm && npm publish --provenance --access=public --dry-run"
911
},
1012
"imports": {
11-
"@std/testing/types": "jsr:@std/testing@0/types"
13+
"@std/testing/types": "jsr:@std/testing@0/types",
14+
"@deno/dnt": "jsr:@deno/dnt@^0.42.3"
1215
},
1316
"exports": {
1417
"./rest": "./rest.ts",
@@ -24,6 +27,7 @@
2427
},
2528
"exclude": [
2629
"coverage/",
27-
"docs/"
30+
"docs/",
31+
"npm/"
2832
]
2933
}

deno.lock

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

scripts/build_npm.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { build, emptyDir } from "@deno/dnt";
2+
3+
await emptyDir("./npm");
4+
5+
await build({
6+
entryPoints: [
7+
{
8+
name: "./rest",
9+
path: "./rest.ts",
10+
},
11+
{
12+
name: "./userscript",
13+
path: "./userscript.ts",
14+
},
15+
{
16+
name: "./websocket",
17+
path: "./websocket.ts",
18+
},
19+
],
20+
outDir: "./npm",
21+
shims: { deno: "dev" },
22+
package: {
23+
// package.json properties
24+
name: "@cosense/types",
25+
version: Deno.env.get("VERSION") ?? "0.0.0",
26+
description: "Type definitions for cosense",
27+
author: "scrapbox-jp",
28+
license: "MIT",
29+
repository: {
30+
type: "git",
31+
url: "git+https://github.com/scrapbox-jp/types.git",
32+
},
33+
homepage: "https://github.com/scrapbox-jp/types#readme",
34+
bugs: {
35+
url: "https://github.com/scrapbox-jp/types/issues",
36+
},
37+
keywords: [
38+
"scrapbox",
39+
"cosense",
40+
"types",
41+
"typescript",
42+
"deno",
43+
],
44+
engines: {
45+
node: ">=16.0.0",
46+
},
47+
},
48+
// Don't run type checking during build to avoid Node.js compatibility issues
49+
typeCheck: false,
50+
declaration: "separate",
51+
scriptModule: false,
52+
compilerOptions: {
53+
lib: ["ESNext", "DOM", "DOM.Iterable"],
54+
target: "ES2023",
55+
},
56+
postBuild: async () => {
57+
await Deno.copyFile("LICENSE", "npm/LICENSE");
58+
await Deno.copyFile("README.md", "npm/README.md");
59+
},
60+
});

0 commit comments

Comments
 (0)