<commit_before>import * as child_process from 'child_process';
import {randomBytes} from 'crypto';

type Options = {
  passphrase: string;
};

type Result = {
  error: Error | null;
  signal: string | null;
  status: number | null;
  stderr: string;
  stdout: string;
};

export default async function sudo(
  command: string,
  args: Array<string>,
  options: Options
): Promise<Result> {
  return new Promise((resolve, reject) => {
    const result = {
      error: null,
      signal: null,
      status: null,
      stderr: '',
      stdout: '',
    };

    const PROMPT_TEXT = `sudo[${randomBytes(16).toString('hex')}]:`;

    const child = child_process.spawn('sudo', [
      '-S',
      '-k',
      '-p',
      PROMPT_TEXT,
      '--',
      command,
      ...args,
    ]);

    // Sadly, we'll still see "Sorry, try again" if the wrong password is
    // supplied, because sudo is logging it directly to /dev/tty, not to stderr.
    //
    // See: https://github.com/sudo-project/sudo/blob/972670bf/plugins/sudoers/sudo_printf.c#L47
    child.stderr.on('data', (data) => {
      if (data.toString() === PROMPT_TEXT) {
        child.stdin.write(`${options.passphrase}\n`);

        // No point in retrying; by calling `end()` here we'll get one shot.<commit_msg>docs: improve accuracy of a comment<commit_after>import * as child_process from 'child_process';
import {randomBytes} from 'crypto';

type Options = {
  passphrase: string;
};

type Result = {
  error: Error | null;
  signal: string | null;
  status: number | null;
  stderr: string;
  stdout: string;
};

export default async function sudo(
  command: string,
  args: Array<string>,
  options: Options
): Promise<Result> {
  return new Promise((resolve, reject) => {
    const result = {
      error: null,
      signal: null,
      status: null,
      stderr: '',
      stdout: '',
    };

    const PROMPT_TEXT = `sudo[${randomBytes(16).toString('hex')}]:`;

    const child = child_process.spawn('sudo', [
      '-S',
      '-k',
      '-p',
      PROMPT_TEXT,
      '--',
      command,
      ...args,
    ]);

    // Sadly, we'll may see "Sorry, try again" if the wrong password is
    // supplied, because sudo may be configured to log it directly to
    // /dev/tty, not to stderr (true on macOS, not on Amazon Linux).
    //
    // See: https://github.com/sudo-project/sudo/blob/972670bf/plugins/sudoers/sudo_printf.c#L47
    child.stderr.on('data', (data) => {
      if (data.toString() === PROMPT_TEXT) {
        child.stdin.write(`${options.passphrase}\n`);

        // No point in retrying; by calling `end()` here we'll get one shot.
{'fraction_non_alphanumeric': 0.1529051987767584, 'fraction_numerical': 0.00764525993883792, 'mean_word_length': 2.6601398601398603, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1139733', 'n_tokens_mistral': 880, 'n_tokens_neox': 829, 'n_words': 234}