Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect between stderr and stdout in Deno.Command #2750

Open
nayeemrmn opened this issue Aug 8, 2019 · 3 comments
Open

Redirect between stderr and stdout in Deno.Command #2750

nayeemrmn opened this issue Aug 8, 2019 · 3 comments
Labels
feat new feature (which has been agreed to/accepted) public API related to "Deno" namespace in JS runtime Relates to code in the runtime crate

Comments

@nayeemrmn
Copy link
Collaborator

nayeemrmn commented Aug 8, 2019

Ref #1828 (comment)

Rather than adding Process::combinedOutput(), I propose modelling 2>&1 in RunOptions.

Ref https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module

export interface RunOptions {
  cmd: string[];
  cwd?: string;
  env?: {
    [key: string]: string;
  };
  stdout?: ProcessStdio | number;
  stderr?: ProcessStdio | number;
  stdin?: ProcessStdio | number;
}

->

export interface RunOptions {
  cmd: string[];
  cwd?: string;
  env?: {
    [key: string]: string;
  };
  stdout?: ProcessStdio | number;
  stderr?: ProcessStdio | "stdout" | number;
  stdin?: ProcessStdio | number;
}
@brandonkal
Copy link
Contributor

This would be very convenient when a program outputs on stdout when it should be on Deno's stderr. This would be especially nice if it streamed.

Process::combinedOutput() may be nice as well but not as useful if the Promise only resolved when the process exits.

@stale
Copy link

stale bot commented Jan 6, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 6, 2021
@lucacasonato lucacasonato added the suggestion suggestions for new features (yet to be agreed) label Jan 13, 2021
@stale stale bot removed the stale label Jan 13, 2021
@jeff-hykin
Copy link

jeff-hykin commented Feb 6, 2022

Thanks to some help from crowlKats in the discord, I created a Sprinter to make subprocesses more convenient. If people find it useful I'll upgrade it over time.

import { run, Out, Stdout, Stderr, Overwrite, AppendTo, Timeout } from "https://deno.land/x/[email protected]/index.js"

// yes &> my_log.txt
await run("yes", Out(Overwrite("my_log.txt")))

// yes &>> my_log.txt
await run("yes", Out(AppendTo("my_log.txt")))

// yes 1>> my_log.txt
await run("yes", Stdout(AppendTo("my_log.txt")))

// yes 1>my_log.txt 2>>my_error_log.txt
await run("yes", Stdout(Overwrite("my_log.txt")), Stderr(AppendTo("my_error_log.txt")))

// using an already-open file
const file = await Deno.open("./my_log.txt", { read: false, write: true, create: true, })
await run("yes", Out(AppendTo(file)))

// with a timeout
await run("yes", Out(AppendTo(file)), Timeout({ gentlyBy: 100, waitBeforeUsingForce: 500}))

There's more, like piping multiple files as input and multiple files as output, but those above should cover the basic use-cases.

@lucacasonato lucacasonato changed the title Redirect between stderr and stdout in RunOptions Redirect between stderr and stdout in Deno.Command Sep 4, 2024
@lucacasonato lucacasonato added feat new feature (which has been agreed to/accepted) public API related to "Deno" namespace in JS runtime Relates to code in the runtime crate and removed suggestion suggestions for new features (yet to be agreed) labels Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat new feature (which has been agreed to/accepted) public API related to "Deno" namespace in JS runtime Relates to code in the runtime crate
Projects
None yet
Development

No branches or pull requests

4 participants