/
util.js
48 lines (43 loc) · 1.12 KB
/
util.js
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
import fs from 'fs';
import path from 'path';
export {
getParsedJsonFromFile,
getParsedPackageJsonFromPath,
isFunction,
isInTest
}
/**
* 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 a function
*/
function isFunction (functionToCheck) {
if (typeof functionToCheck === "undefined")
{
return false;
} else if (functionToCheck === null) {
return false;
} else {
var getType = {};
var functionType = getType.toString.call(functionToCheck);
return functionToCheck && (functionType === '[object Function]' || functionType === '[object AsyncFunction]');
}
}
function isInTest () {
return typeof global.it === 'function';
}