npm-diff

注册表差异命令

选择命令行版本:

概要

¥Synopsis

npm diff [...<paths>]

描述

¥Description

与其对应的 git diff 类似,此命令将打印发布到 npm 注册表的包的文件差异补丁。

¥Similar to its git diff counterpart, this command will print diff patches of files for packages published to the npm registry.

  • npm diff --diff=<spec-a> --diff=<spec-b>

    使用注册表说明符比较两个包版本,例如:npm diff --diff=pkg@1.0.0 --diff=pkg@^2.0.0。也可以比较任何包的复刻,例如:npm diff --diff=pkg@1.0.0 --diff=pkg-fork@1.0.0

    ¥Compares two package versions using their registry specifiers, e.g: npm diff --diff=pkg@1.0.0 --diff=pkg@^2.0.0. It's also possible to compare across forks of any package, e.g: npm diff --diff=pkg@1.0.0 --diff=pkg-fork@1.0.0.

    可以使用任何有效的规范,因此也可以比较目录或 git 存储库,例如:npm diff --diff=pkg@latest --diff=./packages/pkg

    ¥Any valid spec can be used, so that it's also possible to compare directories or git repositories, e.g: npm diff --diff=pkg@latest --diff=./packages/pkg

    这是一个比较注册表中名为 abbrev 的包的两个不同版本的示例:

    ¥Here's an example comparing two different versions of a package named abbrev from the registry:

    npm diff --diff=abbrev@1.1.0 --diff=abbrev@1.1.1

    成功后,输出如下所示:

    ¥On success, output looks like:

    diff --git a/package.json b/package.json
    index v1.1.0..v1.1.1 100644
    --- a/package.json
    +++ b/package.json
    @@ -1,6 +1,6 @@
    {
    "name": "abbrev",
    - "version": "1.1.0",
    + "version": "1.1.1",
    "description": "Like ruby's abbrev module, but in js",
    "author": "Isaac Z. Schlueter <i@izs.me>",
    "main": "abbrev.js",

    鉴于 npm 规范的灵活性,你还可以像使用 npm install 时一样定位本地目录或 git repos:

    ¥Given the flexible nature of npm specs, you can also target local directories or git repos just like when using npm install:

    npm diff --diff=https://github.com/npm/libnpmdiff --diff=./local-path

    在上面的示例中,我们可以比较从 github.com/npm/libnpmdiff 的 git repo 安装的包的内容与包含有效包的 ./local-path 的内容,例如原始包的修改副本。

    ¥In the example above we can compare the contents from the package installed from the git repo at github.com/npm/libnpmdiff with the contents of the ./local-path that contains a valid package, such as a modified copy of the original.

  • npm diff(在包目录中,无参数):

    ¥npm diff (in a package directory, no arguments):

    如果包发布到注册表,npm diff 将获取标记为 latest 的 tarball 版本(可以使用 tag 选项配置此值)并继续比较该 tarball 中存在的文件内容与本地文件中的当前文件系统。

    ¥If the package is published to the registry, npm diff will fetch the tarball version tagged as latest (this value can be configured using the tag option) and proceed to compare the contents of files present in that tarball, with the current files in your local file system.

    此工作流程为包作者提供了一种方便的方式,可以查看与该包的最新发布版本相比,哪些包跟踪文件已更改。

    ¥This workflow provides a handy way for package authors to see what package-tracked files have been changed in comparison with the latest published version of that package.

  • npm diff --diff=<pkg-name>(在包目录中):

    ¥npm diff --diff=<pkg-name> (in a package directory):

    当使用单个包名(没有版本或标签说明符)作为参数时,npm diff 将以与 npm-outdated 类似的方式工作,并到达注册表以确定名为 <pkg-name> 的包的当前发布版本将满足其依赖声明的 semver -范围。一旦知道该特定版本,npm diff 将打印差异补丁,将在本地文件系统中找到的 <pkg-name> 的当前版本与注册表返回的特定版本进行比较。

    ¥When using a single package name (with no version or tag specifier) as an argument, npm diff will work in a similar way to npm-outdated and reach for the registry to figure out what current published version of the package named <pkg-name> will satisfy its dependent declared semver-range. Once that specific version is known npm diff will print diff patches comparing the current version of <pkg-name> found in the local file system with that specific version returned by the registry.

    给定当前安装的名为 abbrev 的包:

    ¥Given a package named abbrev that is currently installed:

    npm diff --diff=abbrev

    这将向注册表请求其最新版本,如果版本号不同,将打印一个比较当前安装的版本与新版本的差异输出。

    ¥That will request from the registry its most up to date version and will print a diff output comparing the currently installed version to this newer one if the version numbers are not the same.

  • npm diff --diff=<spec-a>(在包目录中):

    ¥npm diff --diff=<spec-a> (in a package directory):

    与仅使用单个包名称类似,如果你希望将已安装包的本地版本与 <spec-a> 中提供的特定版本/标签/semver-range 进行比较,也可以声明完整的注册表说明符版本。

    ¥Similar to using only a single package name, it's also possible to declare a full registry specifier version if you wish to compare the local version of an installed package with the specific version/tag/semver-range provided in <spec-a>.

    一个例子:假设 pkg@1.0.0 安装在当前的 node_modules 文件夹中,运行:

    ¥An example: assuming pkg@1.0.0 is installed in the current node_modules folder, running:

    npm diff --diff=pkg@2.0.0

    它实际上是 npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0 的别名。

    ¥It will effectively be an alias to npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0.

  • npm diff --diff=<semver-a> [--diff=<semver-b>](在包目录中):

    ¥npm diff --diff=<semver-a> [--diff=<semver-b>] (in a package directory):

    使用 npm diff 和 semver-valid 版本号是比较当前包的不同版本的简写。

    ¥Using npm diff along with semver-valid version numbers is a shorthand to compare different versions of the current package.

    它需要从一个包目录运行,这样对于一个名为 pkg 的包来说,运行 npm diff --diff=1.0.0 --diff=1.0.1 与运行 npm diff --diff=pkg@1.0.0 --diff=pkg@1.0.1 是一样的。

    ¥It needs to be run from a package directory, such that for a package named pkg running npm diff --diff=1.0.0 --diff=1.0.1 is the same as running npm diff --diff=pkg@1.0.0 --diff=pkg@1.0.1.

    如果只提供了一个参数 <version-a>,那么当前本地文件系统将与该版本进行比较。

    ¥If only a single argument <version-a> is provided, then the current local file system is going to be compared against that version.

    这是一个比较当前项目目录的两个特定版本(发布到配置的注册表)的示例:

    ¥Here's an example comparing two specific versions (published to the configured registry) of the current project directory:

    npm diff --diff=1.0.0 --diff=1.1.0

请注意,标签名称不是有效的 --diff 参数值,如果你希望与已发布的标签进行比较,则必须使用 pkg@tagname 语法。

¥Note that tag names are not valid --diff argument values, if you wish to compare to a published tag, you must use the pkg@tagname syntax.

过滤文件

¥Filtering files

也可以使用文件名或 glob 模式匹配指定位置参数,以便将差异补丁的结果限制为给定包的文件子集,例如:

¥It's possible to also specify positional arguments using file names or globs pattern matching in order to limit the result of diff patches to only a subset of files for a given package, e.g:

npm diff --diff=pkg@2 ./lib/ CHANGELOG.md

在上面的示例中,diff 输出只会打印位于文件夹 ./lib/ 中的文件的内容以及 CHANGELOG.md 文件中更改的代码行。

¥In the example above the diff output is only going to print contents of files located within the folder ./lib/ and changed lines of code within the CHANGELOG.md file.

配置

¥Configuration

diff

  • 默认值:

    ¥Default:

  • 类型:字符串(可以设置多次)

    ¥Type: String (can be set multiple times)

定义要在 npm diff 中比较的参数。

¥Define arguments to compare in npm diff.

diff-name-only

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

使用 npm diff 时仅打印文件名。

¥Prints only filenames when using npm diff.

diff-unified

  • 默认值:3

    ¥Default: 3

  • 类型:数字

    ¥Type: Number

要在 npm diff 中打印的上下文行数。

¥The number of lines of context to print in npm diff.

diff-ignore-all-space

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

比较 npm diff 中的行时忽略空格。

¥Ignore whitespace when comparing lines in npm diff.

diff-no-prefix

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

不要在 npm diff 输出中显示任何源或目标前缀。

¥Do not show any source or destination prefix in npm diff output.

注意:这会导致 npm diff 忽略 --diff-src-prefix--diff-dst-prefix 配置。

¥Note: this causes npm diff to ignore the --diff-src-prefix and --diff-dst-prefix configs.

diff-src-prefix

  • 默认值:"a/"

    ¥Default: "a/"

  • 类型:字符串

    ¥Type: String

npm diff 输出中使用的源前缀。

¥Source prefix to be used in npm diff output.

diff-dst-prefix

  • 默认值:"b/"

    ¥Default: "b/"

  • 类型:字符串

    ¥Type: String

npm diff 输出中使用的目标前缀。

¥Destination prefix to be used in npm diff output.

diff-text

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

将所有文件视为 npm diff 中的文本。

¥Treat all files as text in npm diff.

global

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

在 "global" 模式下运行,以便将包安装到 prefix 文件夹而不是当前工作目录。有关行为差异的更多信息,请参见 文件夹

¥Operates in "global" mode, so that packages are installed into the prefix folder instead of the current working directory. See folders for more on the differences in behavior.

  • 包安装到 {prefix}/lib/node_modules 文件夹,而不是当前工作目录。

    ¥packages are installed into the {prefix}/lib/node_modules folder, instead of the current working directory.

  • bin 文件链接到 {prefix}/bin

    ¥bin files are linked to {prefix}/bin

  • 手册页链接到 {prefix}/share/man

    ¥man pages are linked to {prefix}/share/man

tag

  • 默认值:"latest"

    ¥Default: "latest"

  • 类型:字符串

    ¥Type: String

如果你让 npm 安装一个包并且没有告诉它一个特定的版本,那么它会安装指定的标签。

¥If you ask npm to install a package and don't tell it a specific version, then it will install the specified tag.

如果没有给出明确的标签,它是添加到 npm dist-tag add 命令中指定的 package@version 的标签。

¥It is the tag added to the package@version specified in the npm dist-tag add command, if no explicit tag is given.

当被 npm diff 命令使用时,这是用于获取 tarball 的标签,默认情况下将与本地文件进行比较。

¥When used by the npm diff command, this is the tag used to fetch the tarball that will be compared with the local files by default.

如果在 npm publish 命令中使用,这是将添加到提交到注册表的包的标签。

¥If used in the npm publish command, this is the tag that will be added to the package submitted to the registry.

workspace

  • 默认值:

    ¥Default:

  • 类型:字符串(可以设置多次)

    ¥Type: String (can be set multiple times)

启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。

¥Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

workspace 配置的有效值为:

¥Valid values for the workspace config are either:

  • 工作区名称

    ¥Workspace names

  • 工作区目录的路径

    ¥Path to a workspace directory

  • 父工作区目录的路径(将导致选择该文件夹中的所有工作区)

    ¥Path to a parent workspace directory (will result in selecting all workspaces within that folder)

npm init 命令设置时,可以将其设置为尚不存在的工作区的文件夹,以创建文件夹并将其设置为项目中的全新工作区。

¥When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.

此值不会导出到子进程的环境中。

¥This value is not exported to the environment for child processes.

workspaces

  • 默认值:null

    ¥Default: null

  • 类型:空值或布尔值

    ¥Type: null or Boolean

设置为 true 可在所有已配置工作区的上下文中运行该命令。

¥Set to true to run the command in the context of all configured workspaces.

显式将此设置为 false 将导致像 install 这样的命令完全忽略工作区。未明确设置时:

¥Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

  • node_modules 树上运行的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。* 执行其他操作(测试、执行、发布等)的命令将在根项目上运行,除非在 workspace 配置中指定了一个或多个工作区。

    ¥Commands that operate on the node_modules tree (install, update, etc.) will link workspaces into the node_modules folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in the workspace config.

此值不会导出到子进程的环境中。

¥This value is not exported to the environment for child processes.

include-workspace-root

  • 默认值:false

    ¥Default: false

  • 类型:布尔值

    ¥Type: Boolean

为命令启用工作区时包括工作区根。

¥Include the workspace root when workspaces are enabled for a command.

当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上运行,而不是在根项目上运行。

¥When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.

此值不会导出到子进程的环境中。

¥This value is not exported to the environment for child processes.

也可以看看

¥See Also

npm 中文网 - 粤ICP备13048890号