diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5337fc..54adcd1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,7 @@ name: ci env: DENO_VERSION: 2.x + DENO_TLS_CA_STORE: system on: [push, pull_request] diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6d2f8f3..335a2d9 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,7 +16,7 @@ permissions: id-token: write jobs: - publish: + publish-jsr: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -22,5 +24,27 @@ jobs: with: deno-version: ${{ env.DENO_VERSION }} cache: true - - 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/.github/workflows/udd.yml b/.github/workflows/udd.yml index ef4cbde..454a7c6 100644 --- a/.github/workflows/udd.yml +++ b/.github/workflows/udd.yml @@ -2,6 +2,7 @@ name: update env: DENO_VERSION: 2.x + DENO_TLS_CA_STORE: system on: schedule: diff --git a/.gitignore b/.gitignore index ca902fd..6d39250 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ local_test/ coverage/ docs/ +npm/ diff --git a/README.md b/README.md index 7163e23..1ddeb82 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,27 @@ # 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 +> **Node.js & npm Notice (since vX.Y.Z)** +> +> Now also published on **[npm](https://www.npmjs.com/package/@cosense/std)**. +> +> 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). + ## Getting Started This library serves as an unofficial standard library for developing Scrapbox @@ -14,6 +31,11 @@ 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: @@ -35,6 +57,24 @@ 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: + +**Only ESM syntax is supported.** + +```typescript +// ESM syntax +import { getPage } from "@cosense/std/rest"; +import { parseAbsoluteLink } from "@cosense/std"; +``` + 2. Module Organization The library is organized into the following main modules: - `rest/`: API operations for Scrapbox REST endpoints @@ -58,7 +98,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) { @@ -73,7 +116,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(); @@ -88,8 +134,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 = { diff --git a/api/pages/project.ts b/api/pages/project.ts index e93402b..0f9d3ad 100644 --- a/api/pages/project.ts +++ b/api/pages/project.ts @@ -1,9 +1,9 @@ import type { - BasePage, NotFoundError, NotLoggedInError, NotMemberError, PageList, + PageSummary, } from "@cosense/types/rest"; import { type BaseOptions, setDefaults } from "../../util.ts"; import { cookie } from "../../rest/auth.ts"; @@ -150,7 +150,7 @@ export interface ListPagesStreamOption export async function* listPagesStream( project: string, options?: ListPagesStreamOption, -): AsyncGenerator { +): AsyncGenerator { const props = { ...(options ?? {}), skip: options?.skip ?? 0, diff --git a/deno.jsonc b/deno.jsonc index 8b69b7b..135a05f 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -9,7 +9,8 @@ }, "exclude": [ "coverage/", - "docs/" + "docs/", + "npm/" ], "exports": { ".": "./mod.ts", @@ -20,7 +21,6 @@ "./rest": "./rest/mod.ts", "./text": "./text.ts", "./title": "./title.ts", - "./websocket": "./websocket/mod.ts", "./unstable-api": "./api.ts", "./unstable-api/pages": "./api/pages.ts", "./unstable-api/pages/project": "./api/pages/project.ts", @@ -30,15 +30,16 @@ "./unstable-api/pages/project/search/query": "./api/pages/project/search/query.ts", "./unstable-api/pages/project/search/titles": "./api/pages/project/search/titles.ts", "./unstable-api/pages/project/title": "./api/pages/project/title.ts", + "./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts", + "./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts", "./unstable-api/pages/projects": "./api/projects.ts", "./unstable-api/pages/projects/project": "./api/projects/project.ts", - "./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts", - "./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts", "./unstable-api/pages/smart-context": "./api/smart-context.ts", "./unstable-api/pages/smart-context/export-1hop-links": "./api/smart-context/export-1hop-links/project.ts", "./unstable-api/pages/smart-context/export-2hop-links": "./api/smart-context/export-2hop-links/project.ts", "./unstable-api/users": "./api/users.ts", - "./unstable-api/users/me": "./api/users/me.ts" + "./unstable-api/users/me": "./api/users/me.ts", + "./websocket": "./websocket/mod.ts" }, "imports": { "@core/iterutil": "jsr:@core/iterutil@^0.9.0", @@ -46,20 +47,18 @@ "@cosense/std/browser/websocket": "./websocket/mod.ts", "@cosense/std/rest": "./rest/mod.ts", "@cosense/std/websocket": "./websocket/mod.ts", - "@cosense/types": "jsr:@cosense/types@^0.10.7", - "@cosense/types/rest": "jsr:@cosense/types@0.10/rest", - "@cosense/types/userscript": "jsr:@cosense/types@0.10/userscript", - "@progfay/scrapbox-parser": "jsr:@progfay/scrapbox-parser@9", + "@cosense/types": "jsr:@cosense/types@^0.11.0", + "@deno/dnt": "jsr:@deno/dnt@^0.42.3", + "@progfay/scrapbox-parser": "jsr:@progfay/scrapbox-parser@^10.0.1", "@std/assert": "jsr:@std/assert@1", - "@std/async": "jsr:@std/async@^1.0.11", + "@std/async": "jsr:@std/async@^1.0.14", "@std/encoding": "jsr:@std/encoding@1", "@std/http": "jsr:@std/http@^1.0.13", "@std/json": "jsr:@std/json@^1.0.0", "@std/testing": "jsr:@std/testing@^1.0.9", - "@std/testing/snapshot": "jsr:@std/testing@1/snapshot", "@takker/md5": "jsr:@takker/md5@0.1", "@takker/onp": "./vendor/raw.githubusercontent.com/takker99/onp/0.0.1/mod.ts", - "option-t": "npm:option-t@^51.0.0", + "option-t": "npm:option-t@53", "socket.io-client": "npm:socket.io-client@^4.7.5" }, "lint": { @@ -76,7 +75,7 @@ "test" ] }, - "coverage": "deno test --allow-read=./ --parallel --shuffle --coverage --no-check && deno coverage --html", + "coverage": "deno test --allow-read=./ --doc --parallel --shuffle --coverage --no-check && deno coverage --html", "doc": "deno doc --html mod.ts", "fix": { "command": "deno fmt && deno lint --fix && deno publish --dry-run --allow-dirty", @@ -85,6 +84,13 @@ "test" ] }, + "npm:build": "deno run -A scripts/build_npm.ts", + "npm:check": { + "command": "cd npm && npm publish --provenance --access=public --dry-run", + "dependencies": [ + "npm:build" + ] + }, "test": "deno test --allow-read=./ --doc --parallel --shuffle --no-check", "type-check": "deno check --remote **/*.ts", // from https://github.com/jsr-core/unknownutil/blob/v4.2.2/deno.jsonc#L84-L85 diff --git a/deno.lock b/deno.lock index 5c662df..f7e95b5 100644 --- a/deno.lock +++ b/deno.lock @@ -1,33 +1,32 @@ { - "version": "4", + "version": "5", "specifiers": { "jsr:@core/iterutil@0.9": "0.9.0", "jsr:@core/unknownutil@4": "4.3.0", - "jsr:@cosense/types@0.10": "0.10.7", - "jsr:@cosense/types@~0.10.7": "0.10.7", - "jsr:@progfay/scrapbox-parser@9": "9.2.0", - "jsr:@std/assert@1": "1.0.11", - "jsr:@std/assert@^1.0.10": "1.0.11", - "jsr:@std/async@^1.0.11": "1.0.11", - "jsr:@std/cli@^1.0.12": "1.0.13", - "jsr:@std/data-structures@^1.0.6": "1.0.6", - "jsr:@std/encoding@1": "1.0.7", - "jsr:@std/encoding@^1.0.7": "1.0.7", - "jsr:@std/fmt@^1.0.5": "1.0.5", - "jsr:@std/fs@^1.0.9": "1.0.13", - "jsr:@std/html@^1.0.3": "1.0.3", - "jsr:@std/http@^1.0.13": "1.0.13", - "jsr:@std/internal@^1.0.5": "1.0.5", - "jsr:@std/json@1": "1.0.1", - "jsr:@std/media-types@^1.1.0": "1.1.0", - "jsr:@std/net@^1.0.4": "1.0.4", - "jsr:@std/path@^1.0.8": "1.0.8", - "jsr:@std/streams@^1.0.7": "1.0.9", - "jsr:@std/streams@^1.0.9": "1.0.9", - "jsr:@std/testing@1": "1.0.9", - "jsr:@std/testing@^1.0.9": "1.0.9", + "jsr:@cosense/types@0.11": "0.11.0", + "jsr:@david/code-block-writer@^13.0.3": "13.0.3", + "jsr:@deno/dnt@~0.42.3": "0.42.3", + "jsr:@progfay/scrapbox-parser@^10.0.1": "10.0.1", + "jsr:@std/assert@1": "1.0.13", + "jsr:@std/assert@^1.0.13": "1.0.13", + "jsr:@std/async@^1.0.14": "1.0.14", + "jsr:@std/encoding@1": "1.0.10", + "jsr:@std/fmt@1": "1.0.8", + "jsr:@std/fs@1": "1.0.19", + "jsr:@std/fs@^1.0.19": "1.0.19", + "jsr:@std/http@^1.0.13": "1.0.20", + "jsr:@std/internal@^1.0.10": "1.0.10", + "jsr:@std/internal@^1.0.6": "1.0.10", + "jsr:@std/internal@^1.0.9": "1.0.10", + "jsr:@std/json@1": "1.0.2", + "jsr:@std/path@1": "1.1.1", + "jsr:@std/path@^1.1.1": "1.1.1", + "jsr:@std/streams@^1.0.9": "1.0.10", + "jsr:@std/testing@^1.0.9": "1.0.15", "jsr:@takker/md5@0.1": "0.1.0", - "npm:option-t@51": "51.0.1", + "jsr:@ts-morph/bootstrap@0.27": "0.27.0", + "jsr:@ts-morph/common@0.27": "0.27.0", + "npm:option-t@53": "53.0.0", "npm:socket.io-client@^4.7.5": "4.8.1" }, "jsr": { @@ -37,97 +36,92 @@ "@core/unknownutil@4.3.0": { "integrity": "538a3687ffa81028e91d148818047df219663d0da671d906cecd165581ae55cc" }, - "@cosense/types@0.10.7": { - "integrity": "ce62b4cc7dc599f0f55abc5f4b88d6fe3f5aa7fbacdc46cc1ff32a62fea60176" + "@cosense/types@0.11.0": { + "integrity": "6e1d8768b1be162e635ec0c611092b49c528a623ef3e73c7cd95acab04ba2c10" }, - "@progfay/scrapbox-parser@9.2.0": { - "integrity": "82ebb95e72dd0ea44547fd48e2bcb479fd2275fc26c4515598f742e5aaf6e0e5" + "@david/code-block-writer@13.0.3": { + "integrity": "f98c77d320f5957899a61bfb7a9bead7c6d83ad1515daee92dbacc861e13bb7f" }, - "@std/assert@1.0.11": { - "integrity": "2461ef3c368fe88bc60e186e7744a93112f16fd110022e113a0849e94d1c83c1", + "@deno/dnt@0.42.3": { + "integrity": "62a917a0492f3c8af002dce90605bb0d41f7d29debc06aca40dba72ab65d8ae3", "dependencies": [ - "jsr:@std/internal" + "jsr:@david/code-block-writer", + "jsr:@std/fmt", + "jsr:@std/fs@1", + "jsr:@std/path@1", + "jsr:@ts-morph/bootstrap" ] }, - "@std/async@1.0.11": { - "integrity": "eee0d3405275506638a9c8efaa849cf0d35873120c69b7caa1309c9a9e5b6f85" + "@progfay/scrapbox-parser@10.0.1": { + "integrity": "5b3983ef2cebe5c6b3f0bfb9f5a8a0d1e992f3d2de44e560bfe0628a8344eef1" }, - "@std/cli@1.0.13": { - "integrity": "5db2d95ab2dca3bca9fb6ad3c19908c314e93d6391c8b026725e4892d4615a69" + "@std/assert@1.0.13": { + "integrity": "ae0d31e41919b12c656c742b22522c32fb26ed0cba32975cb0de2a273cb68b29", + "dependencies": [ + "jsr:@std/internal@^1.0.6" + ] }, - "@std/data-structures@1.0.6": { - "integrity": "76a7fd8080c66604c0496220a791860492ab21a04a63a969c0b9a0609bbbb760" + "@std/async@1.0.14": { + "integrity": "62e954a418652c704d37563a3e54a37d4cf0268a9dcaeac1660cc652880b5326" }, - "@std/encoding@1.0.7": { - "integrity": "f631247c1698fef289f2de9e2a33d571e46133b38d042905e3eac3715030a82d" + "@std/encoding@1.0.10": { + "integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1" }, - "@std/fmt@1.0.5": { - "integrity": "0cfab43364bc36650d83c425cd6d99910fc20c4576631149f0f987eddede1a4d" + "@std/fmt@1.0.8": { + "integrity": "71e1fc498787e4434d213647a6e43e794af4fd393ef8f52062246e06f7e372b7" }, - "@std/fs@1.0.9": { - "integrity": "3eef7e3ed3d317b29432c7dcb3b20122820dbc574263f721cb0248ad91bad890", + "@std/fs@1.0.19": { + "integrity": "051968c2b1eae4d2ea9f79a08a3845740ef6af10356aff43d3e2ef11ed09fb06", "dependencies": [ - "jsr:@std/path" + "jsr:@std/internal@^1.0.9", + "jsr:@std/path@^1.1.1" ] }, - "@std/fs@1.0.13": { - "integrity": "756d3ff0ade91c9e72b228e8012b6ff00c3d4a4ac9c642c4dac083536bf6c605", - "dependencies": [ - "jsr:@std/path" - ] + "@std/http@1.0.20": { + "integrity": "b5cc33fc001bccce65ed4c51815668c9891c69ccd908295997e983d8f56070a1" }, - "@std/html@1.0.3": { - "integrity": "7a0ac35e050431fb49d44e61c8b8aac1ebd55937e0dc9ec6409aa4bab39a7988" + "@std/internal@1.0.10": { + "integrity": "e3be62ce42cab0e177c27698e5d9800122f67b766a0bea6ca4867886cbde8cf7" }, - "@std/http@1.0.13": { - "integrity": "d29618b982f7ae44380111f7e5b43da59b15db64101198bb5f77100d44eb1e1e", + "@std/json@1.0.2": { + "integrity": "d9e5497801c15fb679f55a2c01c7794ad7a5dfda4dd1bebab5e409cb5e0d34d4", "dependencies": [ - "jsr:@std/cli", - "jsr:@std/encoding@^1.0.7", - "jsr:@std/fmt", - "jsr:@std/html", - "jsr:@std/media-types", - "jsr:@std/net", - "jsr:@std/path", - "jsr:@std/streams@^1.0.9" + "jsr:@std/streams" ] }, - "@std/internal@1.0.5": { - "integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba" - }, - "@std/json@1.0.1": { - "integrity": "1f0f70737e8827f9acca086282e903677bc1bb0c8ffcd1f21bca60039563049f", + "@std/path@1.1.1": { + "integrity": "fe00026bd3a7e6a27f73709b83c607798be40e20c81dde655ce34052fd82ec76", "dependencies": [ - "jsr:@std/streams@^1.0.7" + "jsr:@std/internal@^1.0.9" ] }, - "@std/media-types@1.1.0": { - "integrity": "c9d093f0c05c3512932b330e3cc1fe1d627b301db33a4c2c2185c02471d6eaa4" - }, - "@std/net@1.0.4": { - "integrity": "2f403b455ebbccf83d8a027d29c5a9e3a2452fea39bb2da7f2c04af09c8bc852" - }, - "@std/path@1.0.8": { - "integrity": "548fa456bb6a04d3c1a1e7477986b6cffbce95102d0bb447c67c4ee70e0364be" + "@std/streams@1.0.10": { + "integrity": "75c0b1431873cd0d8b3d679015220204d36d3c7420d93b60acfc379eb0dc30af" }, - "@std/streams@1.0.8": { - "integrity": "b41332d93d2cf6a82fe4ac2153b930adf1a859392931e2a19d9fabfb6f154fb3" - }, - "@std/streams@1.0.9": { - "integrity": "a9d26b1988cdd7aa7b1f4b51e1c36c1557f3f252880fa6cc5b9f37078b1a5035" - }, - "@std/testing@1.0.9": { - "integrity": "9bdd4ac07cb13e7594ac30e90f6ceef7254ac83a9aeaa089be0008f33aab5cd4", + "@std/testing@1.0.15": { + "integrity": "a490169f5ccb0f3ae9c94fbc69d2cd43603f2cffb41713a85f99bbb0e3087cbc", "dependencies": [ - "jsr:@std/assert@^1.0.10", - "jsr:@std/data-structures", - "jsr:@std/fs", - "jsr:@std/internal", - "jsr:@std/path" + "jsr:@std/assert@^1.0.13", + "jsr:@std/fs@^1.0.19", + "jsr:@std/internal@^1.0.10", + "jsr:@std/path@^1.1.1" ] }, "@takker/md5@0.1.0": { "integrity": "4c423d8247aadf7bcb1eb83c727bf28c05c21906e916517395d00aa157b6eae0" + }, + "@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@1", + "jsr:@std/path@1" + ] } }, "npm": { @@ -156,8 +150,8 @@ "ms@2.1.3": { "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" }, - "option-t@51.0.1": { - "integrity": "sha512-/M5KCGp6SQS4luBoWE1r3G3BZbRPuPfedHpVwJa7UGj98nNqQ2HBuy0E4fGFa4GKzTP+hnxFIs+HKtAq6DNA+w==" + "option-t@53.0.0": { + "integrity": "sha512-qsyo1cFXfUN1MVnxTrpcy+V4tJYEBvEQmeiDkC6m5W/V2JiNfs4V/qXmDGZg4k9XRR1EWkv3jT8TXSZnhGPBLA==" }, "socket.io-client@4.8.1": { "integrity": "sha512-hJVXfu3E28NmzGk8o1sHhN3om52tRvwYeidbj7xKy2eIIse5IoKX3USlS6Tqt3BHAtflLIkCQBkzVrEEfWUyYQ==", @@ -186,18 +180,17 @@ "dependencies": [ "jsr:@core/iterutil@0.9", "jsr:@core/unknownutil@4", - "jsr:@cosense/types@0.10", - "jsr:@cosense/types@~0.10.7", - "jsr:@progfay/scrapbox-parser@9", + "jsr:@cosense/types@0.11", + "jsr:@deno/dnt@~0.42.3", + "jsr:@progfay/scrapbox-parser@^10.0.1", "jsr:@std/assert@1", - "jsr:@std/async@^1.0.11", + "jsr:@std/async@^1.0.14", "jsr:@std/encoding@1", "jsr:@std/http@^1.0.13", "jsr:@std/json@1", - "jsr:@std/testing@1", "jsr:@std/testing@^1.0.9", "jsr:@takker/md5@0.1", - "npm:option-t@51", + "npm:option-t@53", "npm:socket.io-client@^4.7.5" ] } diff --git a/parser/__snapshots__/vimeo.test.ts.snap b/parser/__snapshots__/vimeo.test.ts.snap deleted file mode 100644 index 5778eff..0000000 --- a/parser/__snapshots__/vimeo.test.ts.snap +++ /dev/null @@ -1,11 +0,0 @@ -export const snapshot = {}; - -snapshot[`vimeo links > is 1`] = `"121284607"`; - -snapshot[`vimeo links > is not 1`] = `undefined`; - -snapshot[`vimeo links > is not 2`] = `undefined`; - -snapshot[`vimeo links > is not 3`] = `undefined`; - -snapshot[`vimeo links > is not 4`] = `undefined`; diff --git a/scripts/build_npm.ts b/scripts/build_npm.ts new file mode 100644 index 0000000..507fcd0 --- /dev/null +++ b/scripts/build_npm.ts @@ -0,0 +1,170 @@ +import { build, emptyDir } from "@deno/dnt"; + +await emptyDir("./npm"); + +// Replace jsr packages with corresponding npm packages +await Deno.copyFile("./deno.jsonc", "./deno_node.jsonc"); +await using stack = new AsyncDisposableStack(); +stack.defer(() => Deno.remove("./deno_node.jsonc").catch(() => {})); + +await Deno.writeTextFile( + "./deno_node.jsonc", + (await Deno.readTextFile("./deno.jsonc")).replace( + "jsr:@progfay/scrapbox-parser", + "npm:@progfay/scrapbox-parser", + ), +); + +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: { deno: "dev" }, + package: { + // package.json properties + name: "@cosense/std", + version: Deno.env.get("VERSION") ?? "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", + }, + }, + configFile: new URL("../deno_node.jsonc", import.meta.url).href, + // 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"); + + // ignore snapshot testing & related test files on Node distribution + const emptyTestFiles = [ + "npm/esm/browser/dom/extractCodeFiles.test.js", + "npm/esm/parser/anchor-fm.test.js", + "npm/esm/parser/spotify.test.js", + "npm/esm/parser/youtube.test.js", + "npm/esm/rest/getCodeBlocks.test.js", + "npm/esm/rest/pages.test.js", + "npm/esm/rest/project.test.js", + "npm/esm/websocket/_codeBlock.test.js", + "npm/esm/websocket/diffToChanges.test.js", + ]; + await Promise.all( + emptyTestFiles.map((filePath) => Deno.writeTextFile(filePath, "")), + ); + }, +});