Skip to content

Commit c5b59a2

Browse files
authored
feat: implement negation (#341)
1 parent 52e7650 commit c5b59a2

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

mod.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,15 @@ Deno.test("windows cmd file", { ignore: Deno.build.os !== "windows" }, async ()
23232323
});
23242324
});
23252325

2326+
Deno.test("negation chaining", async () => {
2327+
await assertEquals(await $`! false && echo 1`.text(), "1");
2328+
await assertRejects(
2329+
() => $`! echo hello && ! echo 1`.text(),
2330+
Error,
2331+
"Exited with code: 1",
2332+
);
2333+
});
2334+
23262335
function ensurePromiseNotResolved(promise: Promise<unknown>) {
23272336
return new Promise<void>((resolve, reject) => {
23282337
promise.then(() => reject(new Error("Promise was resolved")));

src/shell.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,16 @@ function executeSequence(sequence: Sequence, context: Context): Promise<ExecuteR
586586
}
587587

588588
function executePipeline(pipeline: Pipeline, context: Context): Promise<ExecuteResult> {
589+
const output = executePipelineInner(pipeline.inner, context);
589590
if (pipeline.negated) {
590-
throw new Error("Negated pipelines are not implemented.");
591+
return Promise.resolve(output).then((result) => {
592+
return {
593+
...result,
594+
code: result.code === 0 ? 1 : 0,
595+
};
596+
});
591597
}
592-
return executePipelineInner(pipeline.inner, context);
598+
return output;
593599
}
594600

595601
async function executeBooleanList(list: BooleanList, context: Context): Promise<ExecuteResult> {

0 commit comments

Comments
 (0)