Skip to content

Commit

Permalink
npm packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 16, 2024
1 parent ec4fa3c commit 323cfdd
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 94 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage/
npm/
4 changes: 4 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
"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,
"tasks": {
"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"
Expand Down
47 changes: 47 additions & 0 deletions dnt.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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
70 changes: 0 additions & 70 deletions logtape/__snapshots__/sink.test.ts.snap

This file was deleted.

3 changes: 2 additions & 1 deletion logtape/config.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
10 changes: 4 additions & 6 deletions logtape/filter.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion logtape/formatter.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
14 changes: 6 additions & 8 deletions logtape/logger.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
81 changes: 73 additions & 8 deletions logtape/sink.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
},
Expand All @@ -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);
Expand All @@ -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 }),
Expand Down

0 comments on commit 323cfdd

Please sign in to comment.