Create an UpdateChecker instance.
This function initialises the internal variables with lots of conditions tested later to determine if the update must be performed.
To improve testability, a mock environment can be passed via a a static property.
The generic object used to pass parameters to the constructor.
A reference to a Logger instance.
A string with the full package name, as from the name
property in the package.json
file.
A string with the package version, in semver, as from the
version
property in the package.json
file.
Optional
envA string with the name of an environment variable which, when present disables the update checks.
If missing, NO_<packageName>_UPDATE_NOTIFIER
is used.
Optional
timestampsA string with the path to the folder where the timestamps are located.
If missing, by default, timestamps are created in
the $HOME/.config/timestamps
folder.
Optional
checkA number with the minimum interval between checks, in milliseconds,
If missing, by default, checks are performed no sooner than 24 hours.
The latest version of the package.
This variable holds the result of calling the
latest-version
module.
The content is a string in semver format.
A string with the name of an environment variable.
This variable provides an external method to control whether the checks are performed or not.
If the variable is present in the environment, the update checks are disabled.
The absolute path to the timestamp file.
To avoid repeating the check too often, the timestamp of the
latest check is remembered as the creation file of a file, in
the $HOME/.config/timestamps
folder.
The content is a string with the absolute path of the file created after checking the version.
The possible error returned while retrieving the version.
In case an error occurs while retrieving the version, the error
message is logged, but the Error
object is also stored in this
variable, accessible to the caller.
Protected
logA reference to a Logger instance.
Protected
packageA string with the full package name, as from the package.json
.
Protected
packageA string with the package version, in semver, as from the
package.json
.
Protected
timestampsA string with the path to the folder where the stamps are created.
Protected
checkA number with the minimum interval to check for the updates, in milliseconds
Protected
latestA promise which resolves to a string with the latest version.
Protected
processA map with environment variables.
Protected
isCIA boolean True when running in a CI environment.
Protected
isTTYA boolean True when running in a terminal.
Protected
isA boolean True when running with administrative credentials.
Protected
isA boolean True when the package is installed globally.
Protected
isA boolean True when the package is installed with administrative rights.
Static
testMock environment to be used during tests.
This property is normally undefined, but it can be set to a mock environment, during tests.
Initiate the asynchronous procedure to retrieve the version.
Nothing.
The operation is performed by the third party package
latest-version
,
which accesses the location where the package is stored
(like the npmjs.com
server, or GitHub).
Since this is a potentially long operation, it is expected to be started at the beginning of a CLI application and completed at the end, giving it a good chance to run in parallel.
The operation is not started if it is considered
not useful, like when running in a CI environment, when not
running from a console, or an environment variable like
NO_<packageName>_UPDATE_NOTIFIER
is defined.
Send the notification message.
Nothing.
The notification message is composed using the values stored in the instance properties.
The message is sent out by writing to the log, using the info
level.
This can be silenced by lowering the log level.
The notification message looks like this:
>>> New version 0.14.9 -> 0.15.0 available. <<<
>>> Run 'npm install --global xpm@0.15.0' to update. <<<
To customise the notification message, this function can be redefined in a class derived from this one.
Protected
didCheck if a subsequent test is too soon.
True if there is no timestamp or if it is older than the given age.
Compute the timestamp age (as the time difference between the current time and the timestamp) and compare to the given age.
Age in milliseconds.
Protected
readProtected
clearProtected
createGenerated using TypeDoc
Summary
The UpdateChecker class can be used to check and notify if there is a newer version of a npm package.
Description
Checking the version requires an access to the packages registry, which might take some time to complete.
Given that the result is needed later, before exiting, the operation can be started asynchronously and awaited at the end.