Class: RuntimeEnvChecker

RuntimeEnvChecker()

Checker for Runtime Environment properties. Note that all methods here are static, so no need to instance this class; see it as an Utility/Companion class.

Constructor

new RuntimeEnvChecker()

Create a new instance of a RuntimeEnvChecker class.

Create a new instance of a RuntimeEnvChecker class. Note that instancing is not allowed for this class because all its methods are static.
Source:
Throws:
because instancing not allowed for this class
Type
Error

Methods

(static) checkBoolean(arg, name) → {boolean}

Ensure that the given argument is a true value.

Ensure that the given argument is a true value. See RuntimeEnvChecker.isBoolean.
Parameters:
Name Type Description
arg object the object to check
name string the name to use in generated error (if any), empty name as default
Source:
Throws:
if it's a false value or it's null or undefined
Type
Error
Returns:
true if it's a true value
Type
boolean

(static) checkEnvVarDefined(name) → {boolean}

Ensure that the given environment variable is defined and has a value.

Ensure that the given environment variable is defined and has a value. See RuntimeEnvChecker.isEnvVarDefined.
Parameters:
Name Type Description
name string the name of the variable to check
Source:
Throws:
if it's not defined or does not have a value
Type
Error
Returns:
true if it's defined and has a value
Type
boolean

(static) checkESModule(filename, foldername) → {boolean}

Ensure that current code is running with JavaScript as ES Module.

Ensure that current code is running with JavaScript as ES Module. See RuntimeEnvChecker.isESModule.
Parameters:
Name Type Description
filename string the name of the file to analyze
foldername string the name of the folder where to search the project definition file
Source:
Throws:
if it's a false value or it's null or undefined
Type
Error
Returns:
true if ES Module is enabled
Type
boolean

(static) checkNodeEnvProduction() → {boolean}

Ensure that the current Node.js environment is production.

Ensure that the current Node.js environment is production. See RuntimeEnvChecker.isEnvVarDefined.
Source:
Throws:
if it's not production
Type
Error
Returns:
true if it's a not empty string
Type
boolean

(static) checkStrictMode() → {boolean}

Ensure that current code is running with JavaScript in strict mode (as generally should be now).

Ensure that current code is running with JavaScript in strict mode (as generally should be now). See RuntimeEnvChecker.isStrictMode.
Source:
Throws:
if it's a false value or it's null or undefined
Type
Error
Returns:
true if strict mode is enabled
Type
boolean

(static) checkStringNotEmpty(arg, name) → {boolean}

Ensure that the given argument is a not empty string.

Ensure that the given argument is a not empty string. See RuntimeEnvChecker.isStringNotEmpty.
Parameters:
Name Type Description
arg object the object to check
name string the name to use in generated error (if any), empty name as default
Source:
Throws:
if it's an empty string or it's null or undefined
Type
Error
Returns:
true if it's a not empty string
Type
boolean

(static) checkVersion(versionnon-null, expectedVersionnon-null) → {boolean}

Utility method that check if the given version is compatible with the given expected version (using then semver syntax).

Utility method that check if the given version is compatible with the given expected version (using then semver syntax).
Parameters:
Name Type Description
version string the version to check (as a string)
expectedVersion string the expected version for the comparison (as a semver string)
Source:
See:
  • isVersionCompatible
Throws:
  • if at least an argument is wrong
    Type
    Error
  • if versions does not matches
    Type
    Error
Returns:
true if version matches
Type
boolean

(static) checkVersionOfNode(version, versionExpected) → {boolean}

Utility method that check if the given Node.js version is compatible with the given expected version (using then semver syntax), usually read from a specific field in 'package.json'.

Utility method that check if the given Node.js version is compatible with the given expected version (using then semver syntax), usually read from a specific field in 'package.json'.
Parameters:
Name Type Description
version string the version to check (as a string), by default current Node.js version
versionExpected string the expected version for the comparison (as a semver string), by default current value for 'node', under 'engines' in 'package.json' (if set)
Source:
See:
  • checkVersion
Throws:
  • if at least an argument is wrong
    Type
    Error
  • if versions are comparable but does not matches
    Type
    Error
Returns:
true if version matches, false if one of versions is null
Type
boolean

(static) checkVersionOfNpm(version, versionExpected) → {boolean}

Utility method that check if the given NPM version is compatible with the given expected version (using then semver syntax), usually read from a specific field in 'package.json'.

Utility method that check if the given NPM version is compatible with the given expected version (using then semver syntax), usually read from a specific field in 'package.json'.
Parameters:
Name Type Default Description
version string null the version to check (as a string), default null
versionExpected string the expected version for the comparison (as a semver string), by default current value for 'npm', under 'engines' in 'package.json' (if set)
Source:
See:
  • checkVersion
Throws:
  • if at least an argument is wrong
    Type
    Error
  • if versions are comparable but does not matches
    Type
    Error
Returns:
true if version matches, false if one of versions is null
Type
boolean

(static) getAvailableCpu() → {int}

Utility method that returns the number of total CPU available.

Utility method that returns the number of total CPU available.
Source:
Returns:
the number of available CPU
Type
int

(static) getNodeEnv() → {string}

Utility method that gets the value of Node.js environment variable.

Utility method that gets the value of Node.js environment variable.
Source:
Returns:
Node.js env var NODE_ENV value (as a string)
Type
string

(static) getVersionOfNpm() → {string}

Utility method that gets current NPM version.

Utility method that gets current NPM version. Note that NPM is executed in a child process (but only to get its version), in a synchronous way.
Source:
Returns:
npm version (as a string) if found, otherwise null
Type
string

(static) isBoolean(arg) → {boolean}

Tell if the given argument is defined and not null, and is a boolean.

Tell if the given argument is defined and not null, and is a boolean. See RuntimeEnvChecker.isDefinedAndNotNull.
Parameters:
Name Type Description
arg object the object to check
Source:
Returns:
true if it's a boolean, false otherwise
Type
boolean

(static) isDefinedAndNotNull(arg) → {boolean}

Tell if the given argument is defined and not null.

Tell if the given argument is defined and not null.
Parameters:
Name Type Description
arg object the object to check
Source:
Returns:
true if defined and not null, false otherwise
Type
boolean

(static) isEnvVarDefined(namenon-null) → {boolean}

Tell if the environment variable with the given name is defined and has a value.

Tell if the environment variable with the given name is defined and has a value. See RuntimeEnvChecker.isStringNotEmpty.
Parameters:
Name Type Description
name string the name to check
Source:
Returns:
true if it's defined and has a value, false otherwise
Type
boolean

(static) isESModule(filename, foldername) → {boolean}

Utility method that returns if the given source file is running with JavaScript in ES Module.

Utility method that returns if the given source file is running with JavaScript in ES Module. Note that the logic here is approximated and uses: - first the given filename (pass `__filename` to analyze current file where this function is called) - the then given folder name to try to read 'package.json' from there (pass `__dirname` for example) - other simple logic. but in some cases (for example if given both) I don't check here the correctness of both values (for example the given filename could not belong to the given foldername nor its parent, so different rules could be applied by the JavaScript engine, and results here could not be precise).
Parameters:
Name Type Description
filename string the name of the file to analyze
foldername string the name of the folder where to search the project definition file
Source:
Throws:
if both arguments are undefined, null or empty
Type
Error
Returns:
true if it seems an ESModule
Type
boolean

(static) isNodeEnvProduction() → {boolean}

Tell if the current Node.js environment is production.

Tell if the current Node.js environment is production. See RuntimeEnvChecker.isEnvVarDefined.
Source:
Returns:
true if it's production, false otherwise
Type
boolean

(static) isStrictMode() → {boolean}

Utility method that returns if current code is running with JavaScript in strict mode (as generally should be now).

Utility method that returns if current code is running with JavaScript in strict mode (as generally should be now).
Source:
Returns:
true if strict mode is enabled
Type
boolean

(static) isStringNotEmpty(arg) → {boolean}

Tell if the given argument is defined and not null, is a string and is not empty.

Tell if the given argument is defined and not null, is a string and is not empty. See RuntimeEnvChecker.isDefinedAndNotNull.
Parameters:
Name Type Description
arg object the object to check
Source:
Returns:
true if it's a not empty string, false otherwise
Type
boolean

(static) isVersionCompatible(versionnon-null, expectedVersionnon-null) → {boolean}

Utility method that tell if the given version is compatible with the expected version (using then semver syntax).

Utility method that tell if the given version is compatible with the expected version (using then semver syntax).
Parameters:
Name Type Description
version string the version to check (as a string)
expectedVersion string the expected version for the comparison (as a semver string)
Source:
Throws:
if at least an argument is wrong
Type
Error
Returns:
true if version is compatible with the given constraint, otherwise false
Type
boolean

(static) memoryInfo() → {object}

Utility method that get some info about memory (total, available) and wraps them into an object.

Utility method that get some info about memory (total, available) and wraps them into an object.
Source:
Returns:
the object representation of some memory-related info
Type
object

(static) processInfo() → {object}

Utility method that get some process-related info and wraps them into an object.

Utility method that get some process-related info and wraps them into an object.
Source:
Returns:
the object representation of process-related info data
Type
object