diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4b13c0..e6e9b73 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,6 +4,7 @@ on: [push, pull_request] env: DENO_VERSION: 2.x + DENO_TLS_CA_STORE: system jobs: test: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index db1f23c..8afbb82 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -3,6 +3,8 @@ name: publish env: DENO_VERSION: 2.x + NODE_VERSION: 20.x + DENO_TLS_CA_STORE: system on: push: @@ -14,12 +16,34 @@ 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 }} + cache: true + - uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + registry-url: "https://registry.npmjs.org" + - name: Build npm package + run: deno task npm:build + env: + VERSION: ${{ github.ref_name }} + - name: Publish to npm + run: npm publish --provenance --access=public + working-directory: ./npm + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore index 9440aad..3839305 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ docs/ -coverage/ \ No newline at end of file +coverage/ +npm/ \ No newline at end of file diff --git a/README.md b/README.md index 23d5ecf..6045a4f 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,36 @@ ## @cosense/types [![JSR](https://jsr.io/badges/@cosense/types)](https://jsr.io/@cosense/types) +[![npm](https://img.shields.io/npm/v/@cosense/types)](https://www.npmjs.com/package/@cosense/types) [![test](https://github.com/scrapbox-jp/types/workflows/ci/badge.svg)](https://github.com/scrapbox-jp/types/actions?query=workflow%3Aci) +> **Node.js & npm Notice (since 0.11.1)** +> +> Now also published on **[npm](https://www.npmjs.com/package/@cosense/types)**. +> +> Node.js support is **experimental**: I mainly target Deno and browsers, so I +> don't actively maintain Node.js compatibility. Some tests run, but there may +> still be runtime or type gaps remaining. Please use it with that +> understanding. +> +> That said, **issues / PRs to improve Node support are very welcome!** +> +> If you don't need a public npm package, you can consume the JSR version +> directly—`npm` via a custom registry in `.npmrc`; `yarn` or `pnpm` need no +> extra config. See the +> [JSR docs](https://jsr.io/docs/using-packages#adding-a-package). + Type definitions for [cosense](https://cosen.se) ## Getting Started +This library provides TypeScript type definitions for Scrapbox/Cosense and +supports both JSR (JavaScript Registry) and npm installation methods. + +### Installation + +#### Option 1: JSR (Recommended for Deno projects) + If you want to use type definitions for REST API, run ```bash @@ -18,3 +42,51 @@ If you want to use type definitions for UserScript, run ```bash deno add jsr:@cosense/types/userscript ``` + +#### Option 2: npm (For Node.js projects) + +Install via npm: + +```bash +npm install @cosense/types +``` + +Import the library: + +```typescript ignore +// ESM syntax +import type { PageList } from "@cosense/types/rest"; +import type { Scrapbox } from "@cosense/types/userscript"; +``` + +### Usage Examples + +```typescript ignore +// Example for JSR (Deno) +import type { PageList } from "jsr:@cosense/types/rest"; + +const pages: PageList = { + projectName: "example-project", + skip: 0, + limit: 100, + count: 42, + pages: [], +}; +``` + +For npm users: + +```typescript ignore +// Example for npm (Node.js) +import type { PageList } from "@cosense/types/rest"; +import type { Scrapbox } from "@cosense/types/userscript"; + +// Use the types in your Node.js application +const pageData: PageList = { + projectName: "my-project", + skip: 0, + limit: 50, + count: 10, + pages: [], +}; +``` diff --git a/deno.jsonc b/deno.jsonc index 86071f1..95efee8 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -5,10 +5,13 @@ "fix": "deno fmt && deno lint --fix && deno test --allow-read --doc --parallel --shuffle && deno publish --dry-run --allow-dirty", "check": "deno fmt --check && deno lint && deno test --allow-read --doc --parallel --shuffle && deno publish --dry-run", "coverage": "deno test --allow-read=./ --parallel --shuffle --coverage && deno coverage --html", - "doc": "deno doc --html rest.ts userscript.ts websocket.ts" + "doc": "deno doc --html rest.ts userscript.ts websocket.ts", + "npm:build": "deno run -A scripts/build_npm.ts", + "npm:check": "cd npm && npm publish --provenance --access=public --dry-run" }, "imports": { - "@std/testing/types": "jsr:@std/testing@0/types" + "@std/testing/types": "jsr:@std/testing@0/types", + "@deno/dnt": "jsr:@deno/dnt@^0.42.3" }, "exports": { "./rest": "./rest.ts", @@ -24,6 +27,7 @@ }, "exclude": [ "coverage/", - "docs/" + "docs/", + "npm/" ] } diff --git a/deno.lock b/deno.lock index 693553c..67e421e 100644 --- a/deno.lock +++ b/deno.lock @@ -1,16 +1,71 @@ { - "version": "4", + "version": "5", "specifiers": { + "jsr:@david/code-block-writer@^13.0.3": "13.0.3", + "jsr:@deno/dnt@~0.42.3": "0.42.3", + "jsr:@std/fmt@1": "1.0.8", + "jsr:@std/fs@1": "1.0.19", + "jsr:@std/internal@^1.0.9": "1.0.10", + "jsr:@std/path@1": "1.1.1", + "jsr:@std/path@^1.1.1": "1.1.1", "jsr:@std/testing@*": "0.225.3", - "jsr:@std/testing@0": "0.225.3" + "jsr:@std/testing@0": "0.225.3", + "jsr:@ts-morph/bootstrap@0.27": "0.27.0", + "jsr:@ts-morph/common@0.27": "0.27.0" }, "jsr": { + "@david/code-block-writer@13.0.3": { + "integrity": "f98c77d320f5957899a61bfb7a9bead7c6d83ad1515daee92dbacc861e13bb7f" + }, + "@deno/dnt@0.42.3": { + "integrity": "62a917a0492f3c8af002dce90605bb0d41f7d29debc06aca40dba72ab65d8ae3", + "dependencies": [ + "jsr:@david/code-block-writer", + "jsr:@std/fmt", + "jsr:@std/fs", + "jsr:@std/path@1", + "jsr:@ts-morph/bootstrap" + ] + }, + "@std/fmt@1.0.8": { + "integrity": "71e1fc498787e4434d213647a6e43e794af4fd393ef8f52062246e06f7e372b7" + }, + "@std/fs@1.0.19": { + "integrity": "051968c2b1eae4d2ea9f79a08a3845740ef6af10356aff43d3e2ef11ed09fb06", + "dependencies": [ + "jsr:@std/internal", + "jsr:@std/path@^1.1.1" + ] + }, + "@std/internal@1.0.10": { + "integrity": "e3be62ce42cab0e177c27698e5d9800122f67b766a0bea6ca4867886cbde8cf7" + }, + "@std/path@1.1.1": { + "integrity": "fe00026bd3a7e6a27f73709b83c607798be40e20c81dde655ce34052fd82ec76", + "dependencies": [ + "jsr:@std/internal" + ] + }, "@std/testing@0.225.3": { "integrity": "348c24d0479d44ab3dbb4f26170f242e19f24051b45935d4a9e7ca0ab7e37780" + }, + "@ts-morph/bootstrap@0.27.0": { + "integrity": "b8d7bc8f7942ce853dde4161b28f9aa96769cef3d8eebafb379a81800b9e2448", + "dependencies": [ + "jsr:@ts-morph/common" + ] + }, + "@ts-morph/common@0.27.0": { + "integrity": "c7b73592d78ce8479b356fd4f3d6ec3c460d77753a8680ff196effea7a939052", + "dependencies": [ + "jsr:@std/fs", + "jsr:@std/path@1" + ] } }, "workspace": { "dependencies": [ + "jsr:@deno/dnt@~0.42.3", "jsr:@std/testing@0" ] } diff --git a/scripts/build_npm.ts b/scripts/build_npm.ts new file mode 100644 index 0000000..0316dbb --- /dev/null +++ b/scripts/build_npm.ts @@ -0,0 +1,60 @@ +import { build, emptyDir } from "@deno/dnt"; + +await emptyDir("./npm"); + +await build({ + entryPoints: [ + { + name: "./rest", + path: "./rest.ts", + }, + { + name: "./userscript", + path: "./userscript.ts", + }, + { + name: "./websocket", + path: "./websocket.ts", + }, + ], + outDir: "./npm", + shims: { deno: "dev" }, + package: { + // package.json properties + name: "@cosense/types", + version: Deno.env.get("VERSION") ?? "0.0.0", + description: "Type definitions for cosense", + author: "scrapbox-jp", + license: "MIT", + repository: { + type: "git", + url: "git+https://github.com/scrapbox-jp/types.git", + }, + homepage: "https://github.com/scrapbox-jp/types#readme", + bugs: { + url: "https://github.com/scrapbox-jp/types/issues", + }, + keywords: [ + "scrapbox", + "cosense", + "types", + "typescript", + "deno", + ], + engines: { + node: ">=16.0.0", + }, + }, + // Don't run type checking during build to avoid Node.js compatibility issues + typeCheck: false, + declaration: "separate", + scriptModule: false, + compilerOptions: { + lib: ["ESNext", "DOM", "DOM.Iterable"], + target: "ES2023", + }, + postBuild: async () => { + await Deno.copyFile("LICENSE", "npm/LICENSE"); + await Deno.copyFile("README.md", "npm/README.md"); + }, +});