Skip to content

Commit 0d9d1e2

Browse files
Copilottakker99
andcommitted
Add npm publishing infrastructure: build script, workflows, and documentation
Co-authored-by: takker99 <[email protected]>
1 parent c0badb7 commit 0d9d1e2

File tree

7 files changed

+265
-2
lines changed

7 files changed

+265
-2
lines changed

.github/workflows/npm-build-test.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: npm-build-test
2+
3+
env:
4+
DENO_VERSION: 2.x
5+
NODE_VERSION: 20.x
6+
7+
on:
8+
pull_request:
9+
paths:
10+
- 'scripts/build_npm.ts'
11+
- 'package.json'
12+
- 'deno.jsonc'
13+
- '.github/workflows/publish.yml'
14+
- '.github/workflows/npm-build-test.yml'
15+
workflow_dispatch:
16+
17+
jobs:
18+
test-npm-build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
- uses: denoland/setup-deno@v2
23+
with:
24+
deno-version: ${{ env.DENO_VERSION }}
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: ${{ env.NODE_VERSION }}
28+
registry-url: 'https://registry.npmjs.org'
29+
- name: Build npm package
30+
run: deno task build:npm "0.0.0-test"
31+
- name: Verify npm package structure
32+
run: |
33+
cd npm
34+
echo "Package structure:"
35+
find . -type f -name "*.js" -o -name "*.d.ts" -o -name "package.json" | head -20
36+
echo "Package.json content:"
37+
cat package.json | head -30
38+
echo "Testing package installation..."
39+
npm pack --dry-run

.github/workflows/publish.yml

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

44
env:
55
DENO_VERSION: 2.x
6+
NODE_VERSION: 20.x
67

78
on:
89
push:
@@ -14,12 +15,35 @@ permissions:
1415
id-token: write
1516

1617
jobs:
17-
publish:
18+
publish-jsr:
1819
runs-on: ubuntu-latest
1920
steps:
2021
- uses: actions/checkout@v4
2122
- uses: denoland/setup-deno@v2
2223
with:
2324
deno-version: ${{ env.DENO_VERSION }}
24-
- name: Publish on tag
25+
- name: Publish to JSR
2526
run: deno run --allow-env --allow-run=deno --allow-read --allow-write=deno.jsonc jsr:@david/[email protected]
27+
28+
publish-npm:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
- uses: denoland/setup-deno@v2
33+
with:
34+
deno-version: ${{ env.DENO_VERSION }}
35+
- uses: actions/setup-node@v4
36+
with:
37+
node-version: ${{ env.NODE_VERSION }}
38+
registry-url: 'https://registry.npmjs.org'
39+
- name: Extract version from tag
40+
id: version
41+
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
42+
- name: Build npm package
43+
run: deno task build:npm ${{ steps.version.outputs.VERSION }}
44+
- name: Publish to npm
45+
run: |
46+
cd npm
47+
npm publish
48+
env:
49+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
@@ -14,6 +15,10 @@ common utilities.
1415

1516
### Installation
1617

18+
This library supports both JSR (JavaScript Registry) and npm installation methods.
19+
20+
#### Option 1: JSR (Recommended for Deno projects)
21+
1722
1. Bundler Configuration This library is distributed through JSR (JavaScript
1823
Registry) and requires a bundler configuration. Follow these steps:
1924

@@ -35,6 +40,26 @@ import { press } from "jsr:@cosense/std/browser/dom";
3540
import { getLines } from "jsr:@cosense/std/browser/dom";
3641
```
3742

43+
#### Option 2: npm (For Node.js projects)
44+
45+
1. Install via npm:
46+
47+
```bash
48+
npm install @cosense/std
49+
```
50+
51+
2. Import the library:
52+
53+
```typescript
54+
// ESM syntax (recommended)
55+
import { getPage } from "@cosense/std/rest";
56+
import { parseAbsoluteLink } from "@cosense/std";
57+
58+
// CommonJS syntax
59+
const { getPage } = require("@cosense/std/rest");
60+
const { parseAbsoluteLink } = require("@cosense/std");
61+
```
62+
3863
2. Module Organization The library is organized into the following main modules:
3964

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

5984
```typescript
6085
// Get page content and metadata
86+
// JSR import
6187
import { getPage } from "jsr:@cosense/std/rest";
88+
// npm import
89+
// import { getPage } from "@cosense/std/rest";
6290

6391
const result = await getPage("projectName", "pageName");
6492
if (result.ok) {
@@ -73,7 +101,10 @@ if (result.ok) {
73101

74102
```typescript
75103
// Interact with the current page's content
104+
// JSR import
76105
import { getLines, press } from "jsr:@cosense/std/browser/dom";
106+
// npm import
107+
// import { getLines, press } from "@cosense/std/browser/dom";
77108

78109
// Get all lines from the current page
79110
const lines = getLines();
@@ -88,8 +119,12 @@ await press("Tab"); // Indent the line
88119

89120
```typescript
90121
// Parse external links (YouTube, Spotify, etc.)
122+
// JSR import
91123
import { parseAbsoluteLink } from "jsr:@cosense/std";
92124
import type { LinkNode } from "@progfay/scrapbox-parser";
125+
// npm import
126+
// import { parseAbsoluteLink } from "@cosense/std";
127+
// import type { LinkNode } from "@progfay/scrapbox-parser";
93128

94129
// Create a link node with absolute path type
95130
const link = {

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
},
6767
"name": "@cosense/std",
6868
"tasks": {
69+
"build:npm": "deno run -A scripts/build_npm.ts",
6970
"check": {
7071
"command": "deno fmt --check && deno lint && deno publish --dry-run",
7172
"dependencies": [

scripts/build_npm.ts

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env -S deno run -A
2+
3+
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";
4+
5+
await emptyDir("./npm");
6+
7+
await build({
8+
entryPoints: [
9+
"./mod.ts",
10+
{
11+
name: "./browser",
12+
path: "./browser/mod.ts",
13+
},
14+
{
15+
name: "./browser/dom",
16+
path: "./browser/dom/mod.ts",
17+
},
18+
{
19+
name: "./browser/websocket",
20+
path: "./websocket/mod.ts",
21+
},
22+
{
23+
name: "./parseAbsoluteLink",
24+
path: "./parseAbsoluteLink.ts",
25+
},
26+
{
27+
name: "./rest",
28+
path: "./rest/mod.ts",
29+
},
30+
{
31+
name: "./text",
32+
path: "./text.ts",
33+
},
34+
{
35+
name: "./title",
36+
path: "./title.ts",
37+
},
38+
{
39+
name: "./websocket",
40+
path: "./websocket/mod.ts",
41+
},
42+
{
43+
name: "./unstable-api",
44+
path: "./api.ts",
45+
},
46+
{
47+
name: "./unstable-api/pages",
48+
path: "./api/pages.ts",
49+
},
50+
{
51+
name: "./unstable-api/pages/project",
52+
path: "./api/pages/project.ts",
53+
},
54+
{
55+
name: "./unstable-api/pages/project/replace",
56+
path: "./api/pages/project/replace.ts",
57+
},
58+
{
59+
name: "./unstable-api/pages/project/replace/links",
60+
path: "./api/pages/project/replace/links.ts",
61+
},
62+
{
63+
name: "./unstable-api/pages/project/search",
64+
path: "./api/pages/project/search.ts",
65+
},
66+
{
67+
name: "./unstable-api/pages/project/search/query",
68+
path: "./api/pages/project/search/query.ts",
69+
},
70+
{
71+
name: "./unstable-api/pages/project/search/titles",
72+
path: "./api/pages/project/search/titles.ts",
73+
},
74+
{
75+
name: "./unstable-api/pages/project/title",
76+
path: "./api/pages/project/title.ts",
77+
},
78+
{
79+
name: "./unstable-api/pages/projects",
80+
path: "./api/projects.ts",
81+
},
82+
{
83+
name: "./unstable-api/pages/projects/project",
84+
path: "./api/projects/project.ts",
85+
},
86+
{
87+
name: "./unstable-api/pages/project/title/text",
88+
path: "./api/pages/project/title/text.ts",
89+
},
90+
{
91+
name: "./unstable-api/pages/project/title/icon",
92+
path: "./api/pages/project/title/icon.ts",
93+
},
94+
{
95+
name: "./unstable-api/users",
96+
path: "./api/users.ts",
97+
},
98+
{
99+
name: "./unstable-api/users/me",
100+
path: "./api/users/me.ts",
101+
},
102+
],
103+
outDir: "./npm",
104+
shims: {
105+
// see JS docs for overview and more options
106+
deno: true,
107+
},
108+
package: {
109+
// package.json properties
110+
name: "@cosense/std",
111+
version: Deno.args[0] ?? "0.0.0",
112+
description: "UNOFFICIAL standard module for Scrapbox UserScript",
113+
author: "takker99",
114+
license: "MIT",
115+
repository: {
116+
type: "git",
117+
url: "git+https://github.com/takker99/scrapbox-userscript-std.git",
118+
},
119+
homepage: "https://github.com/takker99/scrapbox-userscript-std#readme",
120+
bugs: {
121+
url: "https://github.com/takker99/scrapbox-userscript-std/issues",
122+
},
123+
keywords: [
124+
"scrapbox",
125+
"userscript",
126+
"typescript",
127+
"deno"
128+
],
129+
engines: {
130+
node: ">=16.0.0",
131+
},
132+
},
133+
// Don't use import map for npm build to avoid JSR dependency conflicts
134+
// importMap: "./deno.jsonc",
135+
136+
// Disable tests for npm build as they're Deno-specific
137+
test: false,
138+
// Don't run type checking during build to avoid JSR dependency issues
139+
typeCheck: false,
140+
declaration: "inline",
141+
scriptModule: "cjs",
142+
compilerOptions: {
143+
lib: ["esnext", "dom", "dom.iterable"],
144+
target: "ES2020",
145+
},
146+
});
147+
148+
// Copy additional files
149+
await Deno.copyFile("LICENSE", "npm/LICENSE");
150+
await Deno.copyFile("README.md", "npm/README.md");
151+
152+
console.log("npm package built successfully!");

scripts/test_dnt.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env -S deno run -A
2+
3+
// Simple test to check if dnt is accessible
4+
try {
5+
console.log("Testing dnt import...");
6+
const { build } = await import("https://deno.land/x/[email protected]/mod.ts");
7+
console.log("✅ dnt imported successfully");
8+
console.log("Build function type:", typeof build);
9+
} catch (error) {
10+
console.error("❌ Failed to import dnt:", error.message);
11+
}

0 commit comments

Comments
 (0)