简体   繁体   English

节点全局npm软件包,保持最新状态

[英]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. 我已经通过npm发布了一个全局节点包,以为我公司的项目生成样板模板。

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). 除了强迫用户升级外,另一种选择是将模板(以zip格式)发布在远程静态服务器(例如S3)上。 In such case, you can often update the zip to the latest template without upgrading the template generator. 在这种情况下,您通常可以将zip更新为最新的模板,而无需升级模板生成器。

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.). 创建一个包含程序包信息(例如最新的稳定版本,弃用消息等)的JSON文件。
  2. Upload the JSON file to a remote static server. 将JSON文件上传到远程静态服务器。
  3. Whenever the user runs your program, download the JSON file and check against the current package.json . 每当用户运行您的程序时,请下载JSON文件并检查当前的package.json
  4. Show a deprecation warning if the user should upgrade. 如果用户应升级,则显示弃用警告
  5. process.exit() the application if the user must upgrade. 如果用户必须升级,则为process.exit()应用程序。
  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. 例如,如果用户有10个月前开始的一堆项目,则他可能想对较新的项目使用相同的模板。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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