Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/npm-build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: npm-build-test

env:
DENO_VERSION: 2.x
NODE_VERSION: 20.x

on:
pull_request:
paths:
- 'scripts/build_npm.ts'
- 'package.json'
- 'deno.jsonc'
- '.github/workflows/publish.yml'
- '.github/workflows/npm-build-test.yml'
workflow_dispatch:

jobs:
test-npm-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: ${{ env.DENO_VERSION }}
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'
- name: Build npm package
run: deno task build:npm "0.0.0-test"
- name: Verify npm package structure
run: |
cd npm
echo "Package structure:"
find . -type f -name "*.js" -o -name "*.d.ts" -o -name "package.json" | head -20
echo "Package.json content:"
cat package.json | head -30
echo "Testing package installation..."
npm pack --dry-run
28 changes: 26 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: publish

env:
DENO_VERSION: 2.x
NODE_VERSION: 20.x

on:
push:
Expand All @@ -14,12 +15,35 @@ permissions:
id-token: write

jobs:
publish:
publish-jsr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: ${{ env.DENO_VERSION }}
- name: Publish on tag
- name: Publish to JSR
run: deno run --allow-env --allow-run=deno --allow-read --allow-write=deno.jsonc jsr:@david/publish-on-tag@0.1.4

publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: ${{ env.DENO_VERSION }}
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'
- name: Extract version from tag
id: version
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: Build npm package
run: deno task build:npm ${{ steps.version.outputs.VERSION }}
- name: Publish to npm
run: |
cd npm
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local_test/
coverage/
docs/
npm/
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# scrapbox-userscript-std

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

UNOFFICIAL standard module for Scrapbox UserScript
Expand All @@ -14,6 +15,10 @@ common utilities.

### Installation

This library supports both JSR (JavaScript Registry) and npm installation methods.

#### Option 1: JSR (Recommended for Deno projects)

1. Bundler Configuration This library is distributed through JSR (JavaScript
Registry) and requires a bundler configuration. Follow these steps:

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

#### Option 2: npm (For Node.js projects)

1. Install via npm:

```bash
npm install @cosense/std
```

2. Import the library:

```typescript
// ESM syntax (recommended)
import { getPage } from "@cosense/std/rest";
import { parseAbsoluteLink } from "@cosense/std";

// CommonJS syntax
const { getPage } = require("@cosense/std/rest");
const { parseAbsoluteLink } = require("@cosense/std");
```

2. Module Organization The library is organized into the following main modules:

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

```typescript
// Get page content and metadata
// JSR import
import { getPage } from "jsr:@cosense/std/rest";
// npm import
// import { getPage } from "@cosense/std/rest";

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

```typescript
// Interact with the current page's content
// JSR import
import { getLines, press } from "jsr:@cosense/std/browser/dom";
// npm import
// import { getLines, press } from "@cosense/std/browser/dom";

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

```typescript
// Parse external links (YouTube, Spotify, etc.)
// JSR import
import { parseAbsoluteLink } from "jsr:@cosense/std";
import type { LinkNode } from "@progfay/scrapbox-parser";
// npm import
// import { parseAbsoluteLink } from "@cosense/std";
// import type { LinkNode } from "@progfay/scrapbox-parser";

// Create a link node with absolute path type
const link = {
Expand Down
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
},
"name": "@cosense/std",
"tasks": {
"build:npm": "deno run -A scripts/build_npm.ts",
"check": {
"command": "deno fmt --check && deno lint && deno publish --dry-run",
"dependencies": [
Expand Down
152 changes: 152 additions & 0 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/usr/bin/env -S deno run -A

import { build, emptyDir } from "https://deno.land/x/dnt@0.40.0/mod.ts";

await emptyDir("./npm");

await build({
entryPoints: [
"./mod.ts",
{
name: "./browser",
path: "./browser/mod.ts",
},
{
name: "./browser/dom",
path: "./browser/dom/mod.ts",
},
{
name: "./browser/websocket",
path: "./websocket/mod.ts",
},
{
name: "./parseAbsoluteLink",
path: "./parseAbsoluteLink.ts",
},
{
name: "./rest",
path: "./rest/mod.ts",
},
{
name: "./text",
path: "./text.ts",
},
{
name: "./title",
path: "./title.ts",
},
{
name: "./websocket",
path: "./websocket/mod.ts",
},
{
name: "./unstable-api",
path: "./api.ts",
},
{
name: "./unstable-api/pages",
path: "./api/pages.ts",
},
{
name: "./unstable-api/pages/project",
path: "./api/pages/project.ts",
},
{
name: "./unstable-api/pages/project/replace",
path: "./api/pages/project/replace.ts",
},
{
name: "./unstable-api/pages/project/replace/links",
path: "./api/pages/project/replace/links.ts",
},
{
name: "./unstable-api/pages/project/search",
path: "./api/pages/project/search.ts",
},
{
name: "./unstable-api/pages/project/search/query",
path: "./api/pages/project/search/query.ts",
},
{
name: "./unstable-api/pages/project/search/titles",
path: "./api/pages/project/search/titles.ts",
},
{
name: "./unstable-api/pages/project/title",
path: "./api/pages/project/title.ts",
},
{
name: "./unstable-api/pages/projects",
path: "./api/projects.ts",
},
{
name: "./unstable-api/pages/projects/project",
path: "./api/projects/project.ts",
},
{
name: "./unstable-api/pages/project/title/text",
path: "./api/pages/project/title/text.ts",
},
{
name: "./unstable-api/pages/project/title/icon",
path: "./api/pages/project/title/icon.ts",
},
{
name: "./unstable-api/users",
path: "./api/users.ts",
},
{
name: "./unstable-api/users/me",
path: "./api/users/me.ts",
},
],
outDir: "./npm",
shims: {
// see JS docs for overview and more options
deno: true,
},
package: {
// package.json properties
name: "@cosense/std",
version: Deno.args[0] ?? "0.0.0",
description: "UNOFFICIAL standard module for Scrapbox UserScript",
author: "takker99",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/takker99/scrapbox-userscript-std.git",
},
homepage: "https://github.com/takker99/scrapbox-userscript-std#readme",
bugs: {
url: "https://github.com/takker99/scrapbox-userscript-std/issues",
},
keywords: [
"scrapbox",
"userscript",
"typescript",
"deno"
],
engines: {
node: ">=16.0.0",
},
},
// Don't use import map for npm build to avoid JSR dependency conflicts
// importMap: "./deno.jsonc",

// Disable tests for npm build as they're Deno-specific
test: false,
// Don't run type checking during build to avoid JSR dependency issues
typeCheck: false,
declaration: "inline",
scriptModule: "cjs",
compilerOptions: {
lib: ["esnext", "dom", "dom.iterable"],
target: "ES2020",
},
});

// Copy additional files
await Deno.copyFile("LICENSE", "npm/LICENSE");
await Deno.copyFile("README.md", "npm/README.md");

console.log("npm package built successfully!");
11 changes: 11 additions & 0 deletions scripts/test_dnt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env -S deno run -A

// Simple test to check if dnt is accessible
try {
console.log("Testing dnt import...");
const { build } = await import("https://deno.land/x/dnt@0.40.0/mod.ts");
console.log("✅ dnt imported successfully");
console.log("Build function type:", typeof build);
} catch (error) {
console.error("❌ Failed to import dnt:", error.message);
}