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.

Constructors

  • Parameters

    • params: {
          log: Logger;
          packageName: string;
          packageVersion: string;
          envVariableName?: string;
          timestampsFolderPath?: string;
          checkUpdatesIntervalSeconds?: number;
      }

      The generic object used to pass parameters to the constructor.

      • log: Logger

        A reference to a Logger instance.

      • packageName: string

        A string with the full package name, as from the name property in the package.json file.

      • packageVersion: string

        A string with the package version, in semver, as from the version property in the package.json file.

      • OptionalenvVariableName?: string

        A string with the name of an environment variable which, when present disables the update checks.

        If missing, NO_<packageName>_UPDATE_NOTIFIER is used.

      • OptionaltimestampsFolderPath?: string

        A 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.

      • OptionalcheckUpdatesIntervalSeconds?: number

        A number with the minimum interval between checks, in milliseconds,

        If missing, by default, checks are performed no sooner than 24 hours.

    Returns UpdateChecker

    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.

Properties

latestVersion: undefined | string = undefined

This variable holds the result of calling the latest-version module.

The content is a string in semver format.

envVariableName: string

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.

timestampFilePath: string

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.

returnedError: any = undefined

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.

log: Logger

A reference to a Logger instance.

packageName: string

A string with the full package name, as from the package.json.

packageVersion: string

A string with the package version, in semver, as from the package.json.

timestampsFolderPath: string

A string with the path to the folder where the stamps are created.

checkUpdatesIntervalMilliseconds: number

A number with the minimum interval to check for the updates, in milliseconds

latestVersionPromise: undefined | Promise<string> = undefined

A promise which resolves to a string with the latest version.

processEnv: any

A map with environment variables.

isCI: boolean = false

A boolean True when running in a CI environment.

isTTY: boolean = false

A boolean True when running in a terminal.

isRunningAsRoot: boolean = false

A boolean True when running with administrative credentials.

isInstalledGlobally: boolean = false

A boolean True when the package is installed globally.

isInstalledAsRoot: boolean = false

A boolean True when the package is installed with administrative rights.

testEnvironment: undefined | UpdateCheckerTestEnvironment

This property is normally undefined, but it can be set to a mock environment, during tests.

Methods

  • Returns Promise<void>

    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.

  • Returns Promise<void>

    Nothing.

    Await for the actual version to be retrieved, compare it with the current version, possibly notify and create a new timestamp.

  • Returns void

    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.

  • Parameters

    • ageMilliseconds: number

      Age in milliseconds.

    Returns Promise<boolean>

    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.

  • Returns Promise<null | Stats>

    A promise that resolves to a fs.Stats object or null.

  • Returns Promise<void>

    Nothing.

    The main reason for this to be a separate function is testability.

  • Returns Promise<void>

    Nothing.

    Create an empty file. The timestamp is the creation date.