Skip to content

Commit 1fcb989

Browse files
authored
Merge pull request #237 from takker99:copilot/fix-6ec0a2ff-f2ad-42c5-b65e-98e76f6953de
Add npm publishing support for dual JSR and npm distribution
2 parents e1fda30 + 31401b8 commit 1fcb989

File tree

10 files changed

+360
-125
lines changed

10 files changed

+360
-125
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: ci
22

33
env:
44
DENO_VERSION: 2.x
5+
DENO_TLS_CA_STORE: system
56

67
on: [push, pull_request]
78

.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,13 +16,35 @@ 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 }}
2426
cache: true
25-
- name: Publish on tag
27+
- name: Publish to JSR
2628
run: deno run --allow-env --allow-run=deno --allow-read --allow-write=deno.jsonc jsr:@david/[email protected]
29+
30+
publish-npm:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- uses: denoland/setup-deno@v2
35+
with:
36+
deno-version: ${{ env.DENO_VERSION }}
37+
cache: true
38+
- uses: actions/setup-node@v4
39+
with:
40+
node-version: ${{ env.NODE_VERSION }}
41+
registry-url: "https://registry.npmjs.org"
42+
- name: Build npm package
43+
run: deno task npm:build
44+
env:
45+
VERSION: ${{ github.ref_name }}
46+
- name: Publish to npm
47+
run: npm publish --provenance --access=public
48+
working-directory: ./npm
49+
env:
50+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/udd.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: update
22

33
env:
44
DENO_VERSION: 2.x
5+
DENO_TLS_CA_STORE: system
56

67
on:
78
schedule:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
local_test/
22
coverage/
33
docs/
4+
npm/

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
# scrapbox-userscript-std
22

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

67
UNOFFICIAL standard module for Scrapbox UserScript
78

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

1027
This library serves as an unofficial standard library for developing Scrapbox
@@ -14,6 +31,11 @@ common utilities.
1431

1532
### Installation
1633

34+
This library supports both JSR (JavaScript Registry) and npm installation
35+
methods.
36+
37+
#### Option 1: JSR (Recommended for Deno projects)
38+
1739
1. Bundler Configuration This library is distributed through JSR (JavaScript
1840
Registry) and requires a bundler configuration. Follow these steps:
1941

@@ -35,6 +57,24 @@ import { press } from "jsr:@cosense/std/browser/dom";
3557
import { getLines } from "jsr:@cosense/std/browser/dom";
3658
```
3759

60+
#### Option 2: npm (For Node.js projects)
61+
62+
1. Install via npm:
63+
64+
```bash
65+
npm install @cosense/std
66+
```
67+
68+
2. Import the library:
69+
70+
**Only ESM syntax is supported.**
71+
72+
```typescript
73+
// ESM syntax
74+
import { getPage } from "@cosense/std/rest";
75+
import { parseAbsoluteLink } from "@cosense/std";
76+
```
77+
3878
2. Module Organization The library is organized into the following main modules:
3979

4080
- `rest/`: API operations for Scrapbox REST endpoints
@@ -58,7 +98,10 @@ import { getLines } from "jsr:@cosense/std/browser/dom";
5898

5999
```typescript
60100
// Get page content and metadata
101+
// JSR import
61102
import { getPage } from "jsr:@cosense/std/rest";
103+
// npm import
104+
// import { getPage } from "@cosense/std/rest";
62105

63106
const result = await getPage("projectName", "pageName");
64107
if (result.ok) {
@@ -73,7 +116,10 @@ if (result.ok) {
73116

74117
```typescript
75118
// Interact with the current page's content
119+
// JSR import
76120
import { getLines, press } from "jsr:@cosense/std/browser/dom";
121+
// npm import
122+
// import { getLines, press } from "@cosense/std/browser/dom";
77123

78124
// Get all lines from the current page
79125
const lines = getLines();
@@ -88,8 +134,12 @@ await press("Tab"); // Indent the line
88134

89135
```typescript
90136
// Parse external links (YouTube, Spotify, etc.)
137+
// JSR import
91138
import { parseAbsoluteLink } from "jsr:@cosense/std";
92139
import type { LinkNode } from "@progfay/scrapbox-parser";
140+
// npm import
141+
// import { parseAbsoluteLink } from "@cosense/std";
142+
// import type { LinkNode } from "@progfay/scrapbox-parser";
93143

94144
// Create a link node with absolute path type
95145
const link = {

api/pages/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {
2-
BasePage,
32
NotFoundError,
43
NotLoggedInError,
54
NotMemberError,
65
PageList,
6+
PageSummary,
77
} from "@cosense/types/rest";
88
import { type BaseOptions, setDefaults } from "../../util.ts";
99
import { cookie } from "../../rest/auth.ts";
@@ -150,7 +150,7 @@ export interface ListPagesStreamOption<R extends Response | undefined>
150150
export async function* listPagesStream(
151151
project: string,
152152
options?: ListPagesStreamOption<Response>,
153-
): AsyncGenerator<BasePage, void, unknown> {
153+
): AsyncGenerator<PageSummary, void, unknown> {
154154
const props = {
155155
...(options ?? {}),
156156
skip: options?.skip ?? 0,

deno.jsonc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"exclude": [
1111
"coverage/",
12-
"docs/"
12+
"docs/",
13+
"npm/"
1314
],
1415
"exports": {
1516
".": "./mod.ts",
@@ -20,7 +21,6 @@
2021
"./rest": "./rest/mod.ts",
2122
"./text": "./text.ts",
2223
"./title": "./title.ts",
23-
"./websocket": "./websocket/mod.ts",
2424
"./unstable-api": "./api.ts",
2525
"./unstable-api/pages": "./api/pages.ts",
2626
"./unstable-api/pages/project": "./api/pages/project.ts",
@@ -30,36 +30,35 @@
3030
"./unstable-api/pages/project/search/query": "./api/pages/project/search/query.ts",
3131
"./unstable-api/pages/project/search/titles": "./api/pages/project/search/titles.ts",
3232
"./unstable-api/pages/project/title": "./api/pages/project/title.ts",
33+
"./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts",
34+
"./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts",
3335
"./unstable-api/pages/projects": "./api/projects.ts",
3436
"./unstable-api/pages/projects/project": "./api/projects/project.ts",
35-
"./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts",
36-
"./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts",
3737
"./unstable-api/pages/smart-context": "./api/smart-context.ts",
3838
"./unstable-api/pages/smart-context/export-1hop-links": "./api/smart-context/export-1hop-links/project.ts",
3939
"./unstable-api/pages/smart-context/export-2hop-links": "./api/smart-context/export-2hop-links/project.ts",
4040
"./unstable-api/users": "./api/users.ts",
41-
"./unstable-api/users/me": "./api/users/me.ts"
41+
"./unstable-api/users/me": "./api/users/me.ts",
42+
"./websocket": "./websocket/mod.ts"
4243
},
4344
"imports": {
4445
"@core/iterutil": "jsr:@core/iterutil@^0.9.0",
4546
"@core/unknownutil": "jsr:@core/unknownutil@^4.0.0",
4647
"@cosense/std/browser/websocket": "./websocket/mod.ts",
4748
"@cosense/std/rest": "./rest/mod.ts",
4849
"@cosense/std/websocket": "./websocket/mod.ts",
49-
"@cosense/types": "jsr:@cosense/types@^0.10.7",
50-
"@cosense/types/rest": "jsr:@cosense/[email protected]/rest",
51-
"@cosense/types/userscript": "jsr:@cosense/[email protected]/userscript",
52-
"@progfay/scrapbox-parser": "jsr:@progfay/scrapbox-parser@9",
50+
"@cosense/types": "jsr:@cosense/types@^0.11.0",
51+
"@deno/dnt": "jsr:@deno/dnt@^0.42.3",
52+
"@progfay/scrapbox-parser": "jsr:@progfay/scrapbox-parser@^10.0.1",
5353
"@std/assert": "jsr:@std/assert@1",
54-
"@std/async": "jsr:@std/async@^1.0.11",
54+
"@std/async": "jsr:@std/async@^1.0.14",
5555
"@std/encoding": "jsr:@std/encoding@1",
5656
"@std/http": "jsr:@std/http@^1.0.13",
5757
"@std/json": "jsr:@std/json@^1.0.0",
5858
"@std/testing": "jsr:@std/testing@^1.0.9",
59-
"@std/testing/snapshot": "jsr:@std/testing@1/snapshot",
6059
"@takker/md5": "jsr:@takker/[email protected]",
6160
"@takker/onp": "./vendor/raw.githubusercontent.com/takker99/onp/0.0.1/mod.ts",
62-
"option-t": "npm:option-t@^51.0.0",
61+
"option-t": "npm:option-t@53",
6362
"socket.io-client": "npm:socket.io-client@^4.7.5"
6463
},
6564
"lint": {
@@ -76,7 +75,7 @@
7675
"test"
7776
]
7877
},
79-
"coverage": "deno test --allow-read=./ --parallel --shuffle --coverage --no-check && deno coverage --html",
78+
"coverage": "deno test --allow-read=./ --doc --parallel --shuffle --coverage --no-check && deno coverage --html",
8079
"doc": "deno doc --html mod.ts",
8180
"fix": {
8281
"command": "deno fmt && deno lint --fix && deno publish --dry-run --allow-dirty",
@@ -85,6 +84,13 @@
8584
"test"
8685
]
8786
},
87+
"npm:build": "deno run -A scripts/build_npm.ts",
88+
"npm:check": {
89+
"command": "cd npm && npm publish --provenance --access=public --dry-run",
90+
"dependencies": [
91+
"npm:build"
92+
]
93+
},
8894
"test": "deno test --allow-read=./ --doc --parallel --shuffle --no-check",
8995
"type-check": "deno check --remote **/*.ts",
9096
// from https://github.com/jsr-core/unknownutil/blob/v4.2.2/deno.jsonc#L84-L85

0 commit comments

Comments
 (0)