简体   繁体   中英

Node global npm package, keeping up to date

I have published a global node package via npm to generate boilerplate templates for projects at my company.

I would like to compare the current version with the latest published in order to exit the process if it's not the latest.

  1. What node libraries would you recommend to check for the latest version.

  2. Is there a way to auto update the global package if a new version is detected.

Remember this is an internal tool for my company so It's critical they are creating projects with the latest templates and I'd like them to be able to update as automatically or easily as possible

Personal Suggestion

Instead of forcing the user to upgrade, another option is to publish your templates (as zip) on remote static server (eg S3). In such case, you can often update the zip to the latest template without upgrading the template generator.

generate-template angularjs-template:latest
generate-template angularjs-template:4.3

Answering Your Questions

  1. What node libraries would you recommend to check for the latest version.

I am not sure if there is a library for this. However, you can build one very easily.

  1. Create a JSON file which contains the package information (eg latest stable version, deprecation message, etc.).
  2. Upload the JSON file to a remote static server.
  3. Whenever the user runs your program, download the JSON file and check against the current package.json .
  4. Show a deprecation warning if the user should upgrade.
  5. process.exit() the application if the user must upgrade.
  1. Is there a way to auto update the global package if a new version is detected.

I think it is better to leave the control to the user, because there could be some reasons why he doesn't prefer upgrade. For example, if the user has a bunch of projects started 10 months ago, he might want to use the same template for newer projects. But if you really want to automate it, you might use the following code (not tested) .

const { execSync } = require('child_process');
const pkg = require('./package.json')
execSync(`npm update -g ${pkg.name}`)

process.exit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM