npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mishguru/selfupdate

v4.8.0

Published

Selfupdate your global NPM package

Downloads

33

Readme

@mishguru/selfupdate

A handful of functions to implement self updating for globally installed NPM packages.

Installation

Add @mishguru/selfupdate to your project:

$ npm install --save @mishguru/selfupdate

Example

The selfupdate function will automatically check the NPM registry for a new version, and if it exists install it globally and live restart your app to use the new version.

It's important that you run selfupdate(pkg) as soon as your app starts. You must also await the promise it returns before doing anything else. Otherwise both the old and new versions will run simultaneously.

import { selfupdate } from '@mishguru/selfupdate'

import pkg from './package.json'

selfupdate(pkg).then(startMyApp)

Advanced Example

If you want a full control over how the upgrade works, then you can use the helper functions provided.

import {
  fetchLatestPackageVersion,
  installPackageVersion,
  respawnProcess
} from '@mishguru/selfupdate'

import pkg from './package.json'

const latestVersion = await fetchLatestPackageVersion(pkg.name)

if (pkg.version !== latestVersion) {
  await installPackageVersion(pkg.name, latestVersion)

  console.log(`Upgraded from ${pkg.version} to ${latestVersion}. Restarting...`)

  respawnProcess()
}

Documentation

selfupdate(package: object, options?: object): Promise<void>

Automatically check for updates, and install and restart if they are available.

The options available are:

  • checkInterval: The number of milliseconds to wait before querying the registry. Set to 0 to query every time. Default is 1 hour.

Example:

import { selfupdate  } from '@mishguru/selfupdate'

import pkg from './package.json'

const start = async () => {
  await selfupdate(pkg, {
    checkInterval: 1 * 60 * 60 * 1000 // optional
  })

  console.log(`Running version ${pkg.version}`)
}

start().catch(console.error)

fetchLatestPackageVersion(packageName: string): Promise<string>

Find the latest version of a particular package.

Example:

import { fetchLatestPackageVersion } from '@mishguru/selfupdate'

import pkg from './package.json'

const latestVersion = await fetchLatestPackageVersion(pkg.name)

console.log(`Current version: ${pkg.version}`)
console.log(`Latest version: ${latestVersion}`)
console.log(`Up to date?: ${latestVersion === pkg.version}`)

installPackageVersion(packageName: string, packageVersion: string): Promise<void>

Install a specific version of a package.

You can also pass a tag instaed of a version. For example, you can pass 'latest' to install the latest version on NPM.

Example:

import { installPackageVersion } from '@mishguru/selfupdate'

import pkg from './package.json'

await installPackageVersion(pkg.name, '2.0.0')

respawnProcess()

Restart the current process, based on process.argv.

This function is useful when you self update your application and need to re run with the latest version.

Notice that the current process exits only when the child process does, so you must make sure the current process does not run any further code.

Example:

import { respawnProcess } from '@mishguru/selfupdate'

respawnProcess()