Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-worms-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"just-bash": patch
---

Add regression coverage for UTF-8 piped file input.
13 changes: 7 additions & 6 deletions examples/custom-command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ This demonstrates how custom commands can:
Use `defineCommand` from just-bash:

```typescript
import { defineCommand } from "just-bash";
import { decodeBytesToUtf8, defineCommand } from "just-bash";

const myCommand = defineCommand("mycommand", async (args, ctx) => {
// args: command arguments (string[])
// ctx: CommandContext with fs, cwd, env, stdin, exec

// ctx.stdin is a ByteString. Decode it before text operations.
const input = decodeBytesToUtf8(ctx.stdin);

return {
stdout: "output here\n",
stdout: input.toUpperCase(),
stderr: "",
exitCode: 0,
};
Expand All @@ -75,6 +76,6 @@ Your command receives a context object with:
- `fs` - Virtual filesystem interface
- `cwd` - Current working directory
- `env` - Environment variables
- `stdin` - Standard input (from pipes)
- `stdin` - Standard input from pipes as a `ByteString`; use
`decodeBytesToUtf8(ctx.stdin)` before text parsing or string operations
- `exec` - Function to run subcommands

14 changes: 14 additions & 0 deletions packages/just-bash/src/commands/jq/jq.utf8-stdin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ describe("jq reads UTF-8 from stdin", () => {
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe("한글 / café / 漢字\n");
});

it("preserves multibyte string values when file input is redirected", async () => {
const env = new Bash({
files: {
"/places.json": JSON.stringify({ name: "Florida — Miami" }),
},
});

const result = await env.exec("jq '.name' /places.json > /out.json");
expect(result.exitCode).toBe(0);

const out = await env.fs.readFile("/out.json", "utf8");
expect(out).toBe('"Florida — Miami"\n');
});
});
17 changes: 17 additions & 0 deletions packages/just-bash/src/custom-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ describe("custom-commands", () => {
expect(result.exitCode).toBe(0);
});

it("custom command decodes multibyte stdin from a file pipe", async () => {
const capture = defineCommand("capture", async (_args, ctx) => ({
stdout: decodeBytesToUtf8(ctx.stdin),
stderr: "",
exitCode: 0,
}));

const bash = new Bash({
customCommands: [capture],
files: { "/place.json": '{"name":"Florida — Miami"}' },
});
const result = await bash.exec("cat /place.json | capture");

expect(result.stdout).toBe('{"name":"Florida — Miami"}');
expect(result.exitCode).toBe(0);
});

it("custom command can read files via ctx.fs", async () => {
const reader = defineCommand("reader", async (args, ctx) => {
const content = await ctx.fs.readFile(args[0]);
Expand Down
Loading