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.

Hierarchy

  • UpdateChecker

Constructors

  • Summary

    Create an UpdateChecker instance.

    Description

    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.

    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.

      • Optional envVariableName?: 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.

      • Optional timestampsFolderPath?: 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.

      • Optional checkUpdatesIntervalSeconds?: 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

Properties

latestVersion: undefined | string = undefined

Summary

The latest version of the package.

Description

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

The content is a string in semver format.

envVariableName: string

Summary

A string with the name of an environment variable.

Description

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

Summary

The absolute path to the timestamp file.

Description

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

Summary

The possible error returned while retrieving the version.

Description

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

Summary

Mock environment to be used during tests.

Description

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

Methods

  • Summary

    Initiate the asynchronous procedure to retrieve the version.

    Returns

    Nothing.

    Description

    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>

  • Summary

    Compare the versions and notify if an update is available.

    Returns

    Nothing.

    Description

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

    Returns Promise<void>

  • Summary

    Send the notification message.

    Returns

    Nothing.

    Description

    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.

    Returns void

  • Summary

    Check if a subsequent test is too soon.

    Returns

    True if there is no timestamp or if it is older than the given age.

    Description

    Compute the timestamp age (as the time difference between the current time and the timestamp) and compare to the given age.

    Parameters

    • ageMilliseconds: number

      Age in milliseconds.

    Returns Promise<boolean>

  • Summary

    Read the stats of the timestamp file.

    Returns

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

    Returns Promise<null | Stats>

  • Summary

    Remove the file used as timestamp.

    Returns

    Nothing.

    Description

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

    Returns Promise<void>

  • Summary

    Create the file used as timestamp.

    Returns

    Nothing.

    Description

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

    Returns Promise<void>

Generated using TypeDoc