All files / src process.ts

55.55% Statements 10/18
25% Branches 2/8
50% Functions 2/4
66.66% Lines 10/15

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 46 47 48 49 50 51                                                47x 48x 48x                   47x 48x   48x 48x         48x 48x     47x  
// Here we mock the global `process` variable in case we are not in Node's environment.
 
export interface IProcess {
  getuid?(): number;
 
  getgid?(): number;
 
  cwd(): string;
 
  platform: string;
  emitWarning: (message: string, type: string) => void;
  env: {};
}
 
/**
 * Looks to return a `process` object, if one is available.
 *
 * The global `process` is returned if defined;
 * otherwise `require('process')` is attempted.
 *
 * If that fails, `undefined` is returned.
 *
 * @return {IProcess | undefined}
 */
const maybeReturnProcess = (): IProcess | undefined => {
  if (typeof process !== 'undefined') {
    return process;
  }
 
  try {
    return require('process');
  } catch {
    return undefined;
  }
};
 
export function createProcess(): IProcess {
  const p: IProcess = maybeReturnProcess() || ({} as IProcess);
 
  Iif (!p.cwd) p.cwd = () => '/';
  Iif (!p.emitWarning)
    p.emitWarning = (message, type) => {
      // tslint:disable-next-line:no-console
      console.warn(`${type}${type ? ': ' : ''}${message}`);
    };
  Iif (!p.env) p.env = {};
  return p;
}
 
export default createProcess();