diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index dcabdea..5c0fe9a 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -68,6 +68,19 @@ jobs: run: | set -ex [[ "$(jq -r .version deno.json)" = "$GITHUB_REF_NAME" ]] + - run: 'deno task dnt "$(jq -r .version deno.json)"' + - if: github.event_name == 'push' + run: | + set -ex + npm config set //registry.npmjs.org/:_authToken "$NPM_AUTH_TOKEN" + if [[ "$GITHUB_REF_TYPE" = "tag" ]]; then + npm publish --provenance --access public + else + npm publish --provenance --access public --tag + fi + env: + NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} + working-directory: ${{ github.workspace }}/npm/ - if: github.event_name == 'pull_request' run: deno publish --dry-run --allow-dirty - if: github.event_name == 'push' diff --git a/.gitignore b/.gitignore index 7da804e..5a27765 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ coverage/ +npm/ diff --git a/deno.json b/deno.json index f7cc407..46d297b 100644 --- a/deno.json +++ b/deno.json @@ -3,14 +3,17 @@ "version": "0.1.0", "exports": "./logtape/mod.ts", "imports": { + "@deno/dnt": "jsr:@deno/dnt@^0.41.1", "@std/assert": "jsr:@std/assert@^0.222.1", "@std/async": "jsr:@std/async@^0.222.1", + "@std/fs": "jsr:@std/fs@^0.223.0", "@std/testing": "jsr:@std/testing@^0.222.1", "consolemock": "npm:consolemock@^1.1.0" }, "exclude": [ "*-venv/", "coverage/", + "npm/", "README.md" ], "lock": false, @@ -18,6 +21,7 @@ "check": "deno check **/*.ts && deno lint && deno fmt --check", "test": "deno test --allow-read=logtape/__snapshots__ --allow-write=logtape/__snapshots__", "coverage": "rm -rf coverage && deno task test --coverage && deno coverage --html coverage", + "dnt": "deno run -A dnt.ts", "hooks:install": "deno run --allow-read=deno.json,.git/hooks/ --allow-write=.git/hooks/ jsr:@hongminhee/deno-task-hooks", "hooks:pre-commit": "deno task check", "hooks:pre-push": "deno task test" diff --git a/dnt.ts b/dnt.ts new file mode 100644 index 0000000..91eec62 --- /dev/null +++ b/dnt.ts @@ -0,0 +1,47 @@ +import { build, emptyDir } from "@deno/dnt"; +import metadata from "./deno.json" with { type: "json" }; + +await emptyDir("./npm"); + +await build({ + package: { + name: "@logtape/logtape", + version: Deno.args[0] ?? metadata.version, + description: "Simple logging library for Deno/Node.js/Bun/browsers", + license: "MIT", + author: { + name: "Hong Minhee", + email: "hong@minhee.org", + url: "https://hongminhee.org/", + }, + homepage: "https://github.com/dahlia/logtape", + repository: { + type: "git", + url: "git+https://github.com/dahlia/logtape.git", + }, + bugs: { + url: "https://github.com/dahlia/logtape/issues", + }, + }, + outDir: "./npm", + entryPoints: ["./logtape/mod.ts"], + importMap: "./deno.json", + shims: { + deno: "dev", + custom: [ + { + module: "node:stream/web", + globalNames: ["WritableStream"], + }, + ], + }, + typeCheck: "both", + declaration: "separate", + declarationMap: true, + async postBuild() { + await Deno.copyFile("LICENSE", "npm/LICENSE"); + await Deno.copyFile("README.md", "npm/README.md"); + }, +}); + +// cSpell: ignore Minhee diff --git a/logtape/__snapshots__/sink.test.ts.snap b/logtape/__snapshots__/sink.test.ts.snap deleted file mode 100644 index 1ad058b..0000000 --- a/logtape/__snapshots__/sink.test.ts.snap +++ /dev/null @@ -1,70 +0,0 @@ -export const snapshot = {}; - -snapshot[`getStreamSink() 1`] = ` -"2023-11-14 22:13:20.000 +00:00 [DBG] Hello, 123 & 456! -2023-11-14 22:13:20.000 +00:00 [INF] Hello, 123 & 456! -2023-11-14 22:13:20.000 +00:00 [WRN] Hello, 123 & 456! -2023-11-14 22:13:20.000 +00:00 [ERR] Hello, 123 & 456! -2023-11-14 22:13:20.000 +00:00 [FTL] Hello, 123 & 456! -" -`; - -snapshot[`getConsoleSink() 1`] = ` -[ - { - DEBUG: [ - "%cDEBUG%c %cmy-app·junk %cHello, %o & %o!", - "background-color: gray; color: white;", - "background-color: default;", - "color: gray;", - "color: default;", - 123, - 456, - ], - }, - { - INFO: [ - "%cINFO%c %cmy-app·junk %cHello, %o & %o!", - "background-color: white; color: black;", - "background-color: default;", - "color: gray;", - "color: default;", - 123, - 456, - ], - }, - { - WARN: [ - "%cWARNING%c %cmy-app·junk %cHello, %o & %o!", - "background-color: orange;", - "background-color: default;", - "color: gray;", - "color: default;", - 123, - 456, - ], - }, - { - ERROR: [ - "%cERROR%c %cmy-app·junk %cHello, %o & %o!", - "background-color: red;", - "background-color: default;", - "color: gray;", - "color: default;", - 123, - 456, - ], - }, - { - ERROR: [ - "%cFATAL%c %cmy-app·junk %cHello, %o & %o!", - "background-color: maroon;", - "background-color: default;", - "color: gray;", - "color: default;", - 123, - 456, - ], - }, -] -`; diff --git a/logtape/config.test.ts b/logtape/config.test.ts index 04a66d9..9f67c29 100644 --- a/logtape/config.test.ts +++ b/logtape/config.test.ts @@ -1,4 +1,5 @@ -import { assertEquals, assertThrows } from "@std/assert"; +import { assertEquals } from "@std/assert/assert-equals"; +import { assertThrows } from "@std/assert/assert-throws"; import { ConfigError, configure, reset } from "./config.ts"; import type { Filter } from "./filter.ts"; import { LoggerImpl } from "./logger.ts"; diff --git a/logtape/filter.test.ts b/logtape/filter.test.ts index e3e1207..71c1b00 100644 --- a/logtape/filter.test.ts +++ b/logtape/filter.test.ts @@ -1,9 +1,7 @@ -import { - assert, - assertFalse, - assertStrictEquals, - assertThrows, -} from "@std/assert"; +import { assert } from "@std/assert/assert"; +import { assertFalse } from "@std/assert/assert-false"; +import { assertStrictEquals } from "@std/assert/assert-strict-equals"; +import { assertThrows } from "@std/assert/assert-throws"; import { type Filter, getLevelFilter, toFilter } from "./filter.ts"; import { debug, error, fatal, info, warning } from "./fixtures.ts"; import type { LogLevel } from "./record.ts"; diff --git a/logtape/formatter.test.ts b/logtape/formatter.test.ts index 997a3d0..8b394e5 100644 --- a/logtape/formatter.test.ts +++ b/logtape/formatter.test.ts @@ -1,4 +1,4 @@ -import { assertEquals } from "@std/assert"; +import { assertEquals } from "@std/assert/assert-equals"; import { fatal, info } from "./fixtures.ts"; import { defaultConsoleFormatter, defaultTextFormatter } from "./formatter.ts"; diff --git a/logtape/logger.test.ts b/logtape/logger.test.ts index 3e523bc..7c2d60b 100644 --- a/logtape/logger.test.ts +++ b/logtape/logger.test.ts @@ -1,11 +1,9 @@ -import { - assert, - assertEquals, - assertFalse, - assertGreaterOrEqual, - assertLessOrEqual, - assertStrictEquals, -} from "@std/assert"; +import { assert } from "@std/assert/assert"; +import { assertEquals } from "@std/assert/assert-equals"; +import { assertFalse } from "@std/assert/assert-false"; +import { assertGreaterOrEqual } from "@std/assert/assert-greater-or-equal"; +import { assertLessOrEqual } from "@std/assert/assert-less-or-equal"; +import { assertStrictEquals } from "@std/assert/assert-strict-equals"; import { toFilter } from "./filter.ts"; import { debug, error, info, warning } from "./fixtures.ts"; import { diff --git a/logtape/sink.test.ts b/logtape/sink.test.ts index c732c12..5bfd8d2 100644 --- a/logtape/sink.test.ts +++ b/logtape/sink.test.ts @@ -1,6 +1,6 @@ -import { assertThrows } from "@std/assert"; -import { delay } from "@std/async"; -import { assertSnapshot } from "@std/testing/snapshot"; +import { assertEquals } from "@std/assert/assert-equals"; +import { assertThrows } from "@std/assert/assert-throws"; +import { delay } from "@std/async/delay"; import makeConsoleMock from "consolemock"; import { debug, error, fatal, info, warning } from "./fixtures.ts"; import { defaultConsoleFormatter } from "./formatter.ts"; @@ -11,12 +11,12 @@ interface ConsoleMock extends Console { history(): unknown[]; } -Deno.test("getStreamSink()", async (t) => { +Deno.test("getStreamSink()", async () => { let buffer: string = ""; const decoder = new TextDecoder(); const sink = getStreamSink( new WritableStream({ - write(chunk) { + write(chunk: Uint8Array) { buffer += decoder.decode(chunk); return Promise.resolve(); }, @@ -28,10 +28,19 @@ Deno.test("getStreamSink()", async (t) => { sink(error); sink(fatal); await delay(100); - await assertSnapshot(t, buffer); + assertEquals( + buffer, + `\ +2023-11-14 22:13:20.000 +00:00 [DBG] Hello, 123 & 456! +2023-11-14 22:13:20.000 +00:00 [INF] Hello, 123 & 456! +2023-11-14 22:13:20.000 +00:00 [WRN] Hello, 123 & 456! +2023-11-14 22:13:20.000 +00:00 [ERR] Hello, 123 & 456! +2023-11-14 22:13:20.000 +00:00 [FTL] Hello, 123 & 456! +`, + ); }); -Deno.test("getConsoleSink()", async (t) => { +Deno.test("getConsoleSink()", () => { // @ts-ignore: consolemock is not typed const mock: ConsoleMock = makeConsoleMock(); const sink = getConsoleSink(defaultConsoleFormatter, mock); @@ -40,7 +49,63 @@ Deno.test("getConsoleSink()", async (t) => { sink(warning); sink(error); sink(fatal); - await assertSnapshot(t, mock.history()); + assertEquals(mock.history(), [ + { + DEBUG: [ + "%cDEBUG%c %cmy-app·junk %cHello, %o & %o!", + "background-color: gray; color: white;", + "background-color: default;", + "color: gray;", + "color: default;", + 123, + 456, + ], + }, + { + INFO: [ + "%cINFO%c %cmy-app·junk %cHello, %o & %o!", + "background-color: white; color: black;", + "background-color: default;", + "color: gray;", + "color: default;", + 123, + 456, + ], + }, + { + WARN: [ + "%cWARNING%c %cmy-app·junk %cHello, %o & %o!", + "background-color: orange;", + "background-color: default;", + "color: gray;", + "color: default;", + 123, + 456, + ], + }, + { + ERROR: [ + "%cERROR%c %cmy-app·junk %cHello, %o & %o!", + "background-color: red;", + "background-color: default;", + "color: gray;", + "color: default;", + 123, + 456, + ], + }, + { + ERROR: [ + "%cFATAL%c %cmy-app·junk %cHello, %o & %o!", + "background-color: maroon;", + "background-color: default;", + "color: gray;", + "color: default;", + 123, + 456, + ], + }, + ]); assertThrows( () => sink({ ...info, level: "invalid" as LogLevel }),