all files / src/common/ util.js

96.55% Statements 28/29
88.89% Branches 16/18
100% Functions 6/6
96.55% Lines 28/29
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                                13× 13×                                     13×   12×   11×             14×   13×   12× 12×             14×   13×   12×      
import fs from 'fs';
import path from 'path';
 
export {
  executeShellCommand,
  getParsedJsonFromFile,
  getParsedPackageJsonFromPath,
  isArray,
  isFunction,
  isString
}
 
/**
 * Executes the command passed to it at the path requested
 * using the instance of shelljs passed in
 */
function executeShellCommand(sh, path, installCommand) {
  sh.cd(path);
  sh.exec(installCommand);
}
 
/**
 * Gets the parsed contents of a json file
 */
function getParsedJsonFromFile(filePath, fileName, encoding = 'utf8') {
  try {
    var packageJsonContents = fs.readFileSync(path.join(filePath, fileName), encoding);
    return JSON.parse(packageJsonContents);
  } catch (e) {
    console.error(e);
  }
}
 
/**
 * A helper method for getting the contents of package.json at a given path
 */
function getParsedPackageJsonFromPath(path) {
  return getParsedJsonFromFile(path, 'package.json');
}
 
/**
 * Test if the passed argument is an array
 */
function isArray(arr) {
    if(typeof arr === "undefined")
  {
    return false;
  } else if (arr === null) {
    return false;
  } else {
    return arr.constructor === Array;
  }
}
 
/**
 * Test if the passed argument is a function
 */
function isFunction(functionToCheck) {
  if(typeof functionToCheck === "undefined")
  {
    return false;
  } else if (functionToCheck === null) {
    return false;
  } else {
    var getType = {};
    return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  }
}
 
/**
 * Test if the passed argument is a string
 */
function isString(str) {
  if(typeof str === "undefined")
  {
    return false;
  } else if (str === null) {
    return false;
  } else {
    return Object.prototype.toString.call(str) == '[object String]';
  }
}