Skip to content

Commit

Permalink
fix: deno stderr, examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Feb 19, 2024
1 parent da57cfe commit 06feaf0
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 79 deletions.
1 change: 1 addition & 0 deletions examples/json/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
@jsr:registry=https://npm.jsr.io
7 changes: 7 additions & 0 deletions examples/json/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lock": false,
"imports": {
"@denosaurs/log": "jsr:@denosaurs/log",
"@std/json": "jsr:@std/json"
}
}
65 changes: 0 additions & 65 deletions examples/json/package-lock.json

This file was deleted.

42 changes: 42 additions & 0 deletions examples/json/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { assert } from "jsr:@std/assert";
import { assertIsLog, createJSONLineStream } from "../../utils/test.ts";

assert(
(await new Deno.Command("npm", {
args: ["install"],
cwd: import.meta.dirname,
}).spawn().status).success,
);

Deno.test("deno", async () => {
const { success, stdout } = await new Deno.Command("deno", {
args: ["run", "--quiet", "--no-npm", "main.js"],
cwd: import.meta.dirname,
}).output();
assert(success);
for await (const line of createJSONLineStream(stdout)) {
assertIsLog(line);
}
});

Deno.test("bun", async () => {
const { success, stdout } = await new Deno.Command("bun", {
args: ["main.js"],
cwd: import.meta.dirname,
}).output();
assert(success);
for await (const line of createJSONLineStream(stdout)) {
assertIsLog(line);
}
});

Deno.test("node", async () => {
const { success, stdout } = await new Deno.Command("node", {
args: ["main.js"],
cwd: import.meta.dirname,
}).output();
assert(success);
for await (const line of createJSONLineStream(stdout)) {
assertIsLog(line);
}
});
2 changes: 2 additions & 0 deletions examples/tee/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=false
@jsr:registry=https://npm.jsr.io
7 changes: 7 additions & 0 deletions examples/tee/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lock": false,
"imports": {
"@denosaurs/log": "jsr:@denosaurs/log",
"@std/json": "jsr:@std/json"
}
}
23 changes: 23 additions & 0 deletions examples/tee/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Ensure Bun compatibility
import "@denosaurs/log/transforms/text_encoder_stream";

import { ConsoleReadableStream } from "@denosaurs/log";
import { stderr } from "@denosaurs/log/writables/stderr";
import { stdout } from "@denosaurs/log/writables/stdout";

import { JsonStringifyStream } from "@std/json";

const stream = new ConsoleReadableStream();
const [a, b] = stream
.pipeThrough(new JsonStringifyStream())
.pipeThrough(new TextEncoderStream())
.tee();

a.pipeTo(stderr);
b.pipeTo(stdout);

console.log("Hello, world!");
console.group("Group 1");
console.debug("Debug message");
console.groupEnd();
console.info("Info message");
7 changes: 7 additions & 0 deletions examples/tee/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"dependencies": {
"@std/json": "npm:@jsr/std__json",
"@denosaurs/log": "npm:@jsr/denosaurs__log"
}
}
54 changes: 54 additions & 0 deletions examples/tee/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { assert } from "jsr:@std/assert";
import { assertIsLog, createJSONLineStream } from "../../utils/test.ts";

assert(
(await new Deno.Command("npm", {
args: ["install"],
cwd: import.meta.dirname,
}).spawn().status).success,
);

Deno.test("deno", async () => {
const { success, stdout, stderr } = await new Deno.Command("deno", {
args: ["run", "--quiet", "--no-npm", "main.js"],
cwd: import.meta.dirname,
}).output();
assert(success);
for await (const line of createJSONLineStream(stdout)) {
assertIsLog(line);
}

for await (const line of createJSONLineStream(stderr)) {
assertIsLog(line);
}
});

Deno.test("bun", async () => {
const { success, stdout, stderr } = await new Deno.Command("bun", {
args: ["main.js"],
cwd: import.meta.dirname,
}).output();
assert(success);
for await (const line of createJSONLineStream(stdout)) {
assertIsLog(line);
}

for await (const line of createJSONLineStream(stderr)) {
assertIsLog(line);
}
});

Deno.test("node", async () => {
const { success, stdout, stderr } = await new Deno.Command("node", {
args: ["main.js"],
cwd: import.meta.dirname,
}).output();
assert(success);
for await (const line of createJSONLineStream(stdout)) {
assertIsLog(line);
}

for await (const line of createJSONLineStream(stderr)) {
assertIsLog(line);
}
});
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions utils/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// deno-fmt-ignore-file

export const isDeno = typeof Deno !== "undefined";

// @ts-expect-error: The type checking environment is deno, the bun types are not available
export const isBun = typeof process?.versions?.bun !== "undefined" &&
"isBun" in process && process?.isBun;
export const isBun = typeof process?.versions?.bun !== "undefined" && "isBun" in process && process?.isBun;

// @ts-expect-error: The type checking environment is deno, the node types are not available
export const isNode = typeof process !== "undefined" && !isDeno && !isBun;

// @ts-expect-error: The type checking environment is deno, the browser types are not available
export const isBrowser = typeof globalThis !== "undefined" &&
typeof globalThis.document?.createElement !== "undefined";
export const isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document?.createElement !== "undefined";

export const environment = isDeno
? "deno"
: isBun
? "bun"
: isNode
? "node"
: isBrowser
? "browser"
: "unknown";
export const environment =
isDeno ? "deno"
: isBun ? "bun"
: isNode ? "node"
: isBrowser ? "browser"
: "unknown";
21 changes: 21 additions & 0 deletions utils/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { assert, assertStrictEquals } from "jsr:@std/assert";
import { TextLineStream } from "jsr:@std/streams";
import { JsonParseStream } from "jsr:@std/json";
import { Log, LOG_LEVELS } from "../mod.ts";

// deno-lint-ignore no-explicit-any
export function assertIsLog(log: any): asserts log is Log {
assertStrictEquals(typeof log, "object");
assertStrictEquals(typeof log.timestamp, "number");
assertStrictEquals(typeof log.level, "string");
assert(Object.keys(LOG_LEVELS).includes(log.level));
assert(Array.isArray(log.groups));
assert(Array.isArray(log.data));
}

export function createJSONLineStream(...buffers: Uint8Array[]) {
return ReadableStream.from(buffers)
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(new JsonParseStream());
}
2 changes: 1 addition & 1 deletion writables/stderr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let writable: WritableStream<Uint8Array>;

switch (environment) {
case "deno":
writable = globalThis.Deno.stdout.writable;
writable = globalThis.Deno.stderr.writable;
break;
case "bun": {
// Once https://github.com/oven-sh/bun/issues/3927 is completed we can use the node code for bun.
Expand Down

0 comments on commit 06feaf0

Please sign in to comment.