Skip to content
Merged
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
21 changes: 21 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,27 @@ Deno.test("command .lines()", async () => {
assertEquals(result, ["1", "2"]);
});

Deno.test("command .lines() with no output", async () => {
const result = await $`true`.lines();
assertEquals(result, []);
});

Deno.test("command .lines() strips \\r\\n", async () => {
const result = await $`deno eval ${'await Deno.stdout.write(new TextEncoder().encode("1\\r\\n2\\r\\n"))'}`.lines();
assertEquals(result, ["1", "2"]);
});

Deno.test("command .lines() matches .linesIter()", async () => {
for (const text of ["", "1\n2\n", "1\n2", "1\n\n2\n", "\n", "1\r\n2\r\n", "1\r\n2", "\r\n"]) {
const cmd = $`deno eval ${"await Deno.stdout.write(new TextEncoder().encode(" + JSON.stringify(text) + "))"}`;
const iter: string[] = [];
for await (const line of cmd.linesIter()) {
iter.push(line);
}
assertEquals(await cmd.lines(), iter, `mismatch for ${JSON.stringify(text)}`);
}
});

Deno.test("command .lines('stderr')", async () => {
const result = await $`deno eval "console.error(1); console.error(2)"`.env("NO_COLOR", "1").lines("stderr");
assertEquals(result, ["1", "2"]);
Expand Down
36 changes: 31 additions & 5 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,14 +986,24 @@ export class CommandBuilder implements PromiseLike<CommandResult> {
* ```
*/
async text(kind: StreamKind = "stdout"): Promise<string> {
const command = kind === "combined" ? this.quiet(kind).captureCombined() : this.quiet(kind);
return (await command)[kind].replace(/\r?\n$/, "");
return (await this.#textRaw(kind)).replace(/\r?\n$/, "");
}

/** Gets the text as an array of lines. */
/**
* Gets the text as an array of lines.
*
* Lines are split the same way as {@link CommandBuilder.linesIter} (matches
* [Rust's `str::lines`](https://doc.rust-lang.org/std/primitive.str.html#method.lines)):
* line terminators are not included, a trailing blank line caused by a final
* line ending is excluded, and empty output yields no lines.
*/
async lines(kind: StreamKind = "stdout"): Promise<string[]> {
const text = await this.text(kind);
return text.split(/\r?\n/g);
return splitLines(await this.#textRaw(kind));
}

async #textRaw(kind: StreamKind): Promise<string> {
const command = kind === "combined" ? this.quiet(kind).captureCombined() : this.quiet(kind);
return (await command)[kind];
}

/**
Expand Down Expand Up @@ -2414,6 +2424,22 @@ function attachCallerStack(err: unknown, callerStack: string | undefined): void
err.stack = err.stack == null ? frames : `${err.stack}\n${frames}`;
}

function splitLines(text: string): string[] {
const lines: string[] = [];
let start = 0;
while (true) {
const nl = text.indexOf("\n", start);
if (nl === -1) break;
// strip \r when it immediately precedes \n
const end = nl > start && text.charCodeAt(nl - 1) === 13 ? nl - 1 : nl;
lines.push(text.substring(start, end));
start = nl + 1;
}
// a final line ending does not produce a trailing blank line
if (start < text.length) lines.push(text.substring(start));
return lines;
}

async function* iterateLines(
child: CommandChild,
kind: "stdout" | "stderr",
Expand Down
Loading