Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 npm 的语义版本控制 #29

Open
Popxie opened this issue Aug 23, 2021 · 0 comments
Open

使用 npm 的语义版本控制 #29

Popxie opened this issue Aug 23, 2021 · 0 comments
Assignees

Comments

@Popxie
Copy link
Owner

Popxie commented Aug 23, 2021

使用 npm 的语义版本控制

语义版本控制的概念很简单:所有的版本都有 3 个数字:x.y.z。

  • 第一个数字是主版本。
  • 第二个数字是次版本。
  • 第三个数字是补丁版本。

当发布新的版本时,不仅仅是随心所欲地增加数字,还要遵循以下规则:

  • 当进行不兼容的 API 更改时,则升级主版本。
  • 当以向后兼容的方式添加功能时,则升级次版本。
  • 当进行向后兼容的缺陷修复时,则升级补丁版本。

该约定在所有编程语言中均被采用,每个npm软件包都必须遵守该约定,这一点非常重要,因为整个系统都依赖于此。

为什么这么重要?

因为 npm 设置了一些规则,可用于在 package.json 文件中选择要将软件包更新到的版本(当运行 npm update 时)。

规则使用了这些符号:

  • ^ 插入号
  • ~ 波浪号
  • >
  • >=
  • <
  • <=
  • =
  • -
  • ||

这些规则的详情如下:

  • ^: 如果写入的是 ^0.13.0,则当运行 npm update 时,会更新到补丁版本和次版本:即 0.13.1、0.14.0、依此类推。
  • ~: 如果写入的是 〜0.13.0,则当运行 npm update 时,会更新到补丁版本:即 0.13.1 可以,但 0.14.0 不可以。
  • >: 接受高于指定版本的任何版本。
  • >=: 接受等于或高于指定版本的任何版本。
  • <=: 接受等于或低于指定版本的任何版本。
  • <: 接受低于指定版本的任何版本。
  • =: 接受确切的版本。
  • -: 接受一定范围的版本。例如:2.1.0 - 2.6.2。
  • ||: 组合集合。例如 < 2.1 || > 2.6。

可以合并其中的一些符号,例如 1.0.0 || >=1.1.0 <1.2.0,即使用 1.0.0 或从 1.1.0 开始但低于 1.2.0 的版本。

还有其他的规则:

  • 无符号: 仅接受指定的特定版本(例如 1.2.1)。
  • latest: 使用可用的最新版本。

使用 npm 的语义版本控制·node.js

npm 执行顺序

如果 npm 脚本里面需要执行多个任务,那么需要明确它们的执行顺序。

如果是并行执行(即同时的平行执行),可以使用&符号。

npm run script1.js & npm run script2.js

如果是继发执行(即只有前一个任务成功,才执行下一个任务),可以使用&&符号。

npm run script1.js && npm run script2.js

这两个符号是 Bash 的功能。

npm scripts 使用指南·阮一峰

@Popxie Popxie self-assigned this Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant