All files promisify.ts

94.12% Statements 16/17
100% Branches 0/0
100% Functions 5/5
92.86% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x               20x             1x 14x 4x 12x       1x   1x         1x         1x               1x  
import { exec } from 'child_process';
import { exists } from 'fs';
import * as util from 'util';
 
/**
 * This file contains an implementation of util.promisify (available on node >= 8)
 * Note: Can be removed once we drop support for node 6 (and 7).
 */
function promisify(original: any) {
  if ('promisify' in util) {
    return util.promisify(original); // let nodejs do it's thing thing, if it is supported
  } else {
    return innerPromisify(original); // Do it ourselves
  }
}
 
// This function is exported so that it can be tested on node >= 8
export function innerPromisify(original: any) {
  return function fn(this: unknown, ...args: any[]) {
    return new Promise((resolve, reject) => {
      original.call(this, ...args, (err: Error, ...values: any[]) => {
        if (original === exists) {
          // the exists callback is NOT consistent with NodeJS callbacks: https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
          // First argument is the result instead of an error
          resolve(err);
        } else if (err) {
          reject(err);
        } else {
          // Make an exception for child_process.exec, this is also done by node 8+'s implementation
          // https://github.com/nodejs/node/blob/2ec57a71343cdb725d45801508fceb0a266a9324/lib/internal/util.js#L272
          if (original === exec) {
            resolve({
              stderr: values[1],
              stdout: values[0]
            });
          } else {
            resolve(values[0]);
          }
        }
      });
    });
  };
}
 
export default promisify as typeof util.promisify;