Skip to content

Commit

Permalink
feat: support input redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 27, 2024
1 parent a7e0b96 commit 393b606
Show file tree
Hide file tree
Showing 6 changed files with 2,616 additions and 2,544 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ Deno.test("piping in command", async () => {
}
});

Deno.test("redirects", async () => {
Deno.test("output redirects", async () => {
await withTempDir(async (tempDir) => {
// absolute
const tempFile = tempDir.join("temp_file.txt");
Expand Down Expand Up @@ -1158,6 +1158,14 @@ Deno.test("redirects", async () => {
});
});

Deno.test("input redirects", async () => {
await withTempDir(async (tempDir) => {
tempDir.join("test.txt").writeTextSync("Hi!");
const text = await $`cat - < test.txt`.text();
assertEquals(text, "Hi!");
});
});

Deno.test("shebang support", async (t) => {
await withTempDir(async (dir) => {
const steps: Promise<boolean>[] = [];
Expand Down
30 changes: 15 additions & 15 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,32 +1199,32 @@ function getSignalAbortCode(signal: Deno.Signal) {
}

export function template(strings: TemplateStringsArray, exprs: any[]) {
let result = "";
for (let i = 0; i < Math.max(strings.length, exprs.length); i++) {
if (strings.length > i) {
result += strings[i];
}
if (exprs.length > i) {
result += templateLiteralExprToString(exprs[i], escapeArg);
}
}
return result;
return templateInner(strings, exprs, escapeArg);
}

export function templateRaw(strings: TemplateStringsArray, exprs: any[]) {
let result = "";
return templateInner(strings, exprs, undefined);
}

function templateInner(
strings: TemplateStringsArray,
exprs: any[],
escape: ((arg: string) => string) | undefined,
) {
let text = "";
for (let i = 0; i < Math.max(strings.length, exprs.length); i++) {
if (strings.length > i) {
result += strings[i];
text += strings[i];
}
if (exprs.length > i) {
result += templateLiteralExprToString(exprs[i]);
const expr = exprs[i];
text += templateLiteralExprToString(expr, escape);
}
}
return result;
return text;
}

function templateLiteralExprToString(expr: any, escape?: (arg: string) => string): string {
function templateLiteralExprToString(expr: any, escape: ((arg: string) => string) | undefined): string {
let result: string;
if (expr instanceof Array) {
return expr.map((e) => templateLiteralExprToString(e, escape)).join(" ");
Expand Down
Loading

0 comments on commit 393b606

Please sign in to comment.