Skip to content

Commit d011266

Browse files
ruyadornonlf
authored andcommitted
feat: add npm diff
- As proposed in RFC: npm/rfcs#144 PR-URL: #1319 Credit: @ruyadorno Close: #1319 Reviewed-by: @isaacs
1 parent bb7329d commit d011266

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+10756
-9
lines changed

docs/content/commands/npm-diff.md

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
---
2+
title: npm-diff
3+
section: 1
4+
description: The registry diff command
5+
---
6+
7+
### Synopsis
8+
9+
```bash
10+
npm diff [...<paths>]
11+
npm diff --diff=<pkg-name> [...<paths>]
12+
npm diff --diff=<version-a> [--diff=<version-b>] [...<paths>]
13+
npm diff --diff=<spec-a> [--diff=<spec-b>] [...<paths>]
14+
npm diff [--diff-ignore-all-space] [--diff-name-only] [...<paths>]
15+
```
16+
17+
### Description
18+
19+
Similar to its `git diff` counterpart, this command will print diff patches
20+
of files for packages published to the npm registry.
21+
22+
* `npm diff --diff=<spec-a> --diff=<spec-b>`
23+
24+
Compares two package versions using their registry specifiers, e.g:
25+
`npm diff [email protected] --diff=pkg@^2.0.0`. It's also possible to
26+
compare across forks of any package,
27+
28+
29+
Any valid spec can be used, so that it's also possible to compare
30+
directories or git repositories,
31+
e.g: `npm diff --diff=pkg@latest --diff=./packages/pkg`
32+
33+
Here's an example comparing two different versions of a package named
34+
`abbrev` from the registry:
35+
36+
```bash
37+
38+
```
39+
40+
On success, output looks like:
41+
42+
```bash
43+
diff --git a/package.json b/package.json
44+
index v1.1.0..v1.1.1 100644
45+
--- a/package.json
46+
+++ b/package.json
47+
@@ -1,6 +1,6 @@
48+
{
49+
"name": "abbrev",
50+
- "version": "1.1.0",
51+
+ "version": "1.1.1",
52+
"description": "Like ruby's abbrev module, but in js",
53+
"author": "Isaac Z. Schlueter <[email protected]>",
54+
"main": "abbrev.js",
55+
```
56+
57+
Given the flexible nature of npm specs, you can also target local
58+
directories or git repos just like when using `npm install`:
59+
60+
```bash
61+
npm diff --diff=https://github.com/npm/libnpmdiff --diff=./local-path
62+
```
63+
64+
In the example above we can compare the contents from the package installed
65+
from the git repo at `github.com/npm/libnpmdiff` with the contents of the
66+
`./local-path` that contains a valid package, such as a modified copy of
67+
the original.
68+
69+
* `npm diff` (in a package directory, no arguments):
70+
71+
If the package is published to the registry, `npm diff` will fetch the
72+
tarball version tagged as `latest` (this value can be configured using the
73+
`tag` option) and proceed to compare the contents of files present in that
74+
tarball, with the current files in your local file system.
75+
76+
This workflow provides a handy way for package authors to see what
77+
package-tracked files have been changed in comparison with the latest
78+
published version of that package.
79+
80+
* `npm diff --diff=<pkg-name>` (in a package directory):
81+
82+
When using a single package name (with no version or tag specifier) as an
83+
argument, `npm diff` will work in a similar way to
84+
[`npm-outdated`](npm-outdated) and reach for the registry to figure out
85+
what current published version of the package named <pkg-name> will satisfy
86+
its dependent declared semver-range. Once that specific version is known
87+
`npm diff` will print diff patches comparing the current version of
88+
<pkg-name> found in the local file system with that specific version
89+
returned by the registry.
90+
91+
Given a package named `abbrev` that is currently installed:
92+
93+
```bash
94+
npm diff --diff=abbrev
95+
```
96+
97+
That will request from the registry its most up to date version and
98+
will print a diff output comparing the currently installed version to this
99+
newer one if the version numbers are not the same.
100+
101+
* `npm diff --diff=<spec-a>` (in a package directory):
102+
103+
Similar to using only a single package name, it's also possible to declare
104+
a full registry specifier version if you wish to compare the local version
105+
of an installed package with the specific version/tag/semver-range provided
106+
in `<spec-a>`.
107+
108+
An example: assuming `[email protected]` is installed in the current `node_modules`
109+
folder, running:
110+
111+
```bash
112+
113+
```
114+
115+
It will effectively be an alias to
116+
117+
118+
* `npm diff --diff=<semver-a> [--diff=<semver-b>]` (in a package directory):
119+
120+
Using `npm diff` along with semver-valid version numbers is a shorthand
121+
to compare different versions of the current package.
122+
123+
It needs to be run from a package directory, such that for a package named
124+
`pkg` running `npm diff --diff=1.0.0 --diff=1.0.1` is the same as running
125+
126+
127+
If only a single argument `<version-a>` is provided, then the current local
128+
file system is going to be compared against that version.
129+
130+
Here's an example comparing two specific versions (published to the
131+
configured registry) of the current project directory:
132+
133+
```bash
134+
npm diff --diff=1.0.0 --diff=1.1.0
135+
```
136+
137+
Note that tag names are not valid `--diff` argument values, if you wish to
138+
compare to a published tag, you must use the `pkg@tagname` syntax.
139+
140+
#### Filtering files
141+
142+
It's possible to also specify positional arguments using file names or globs
143+
pattern matching in order to limit the result of diff patches to only a subset
144+
of files for a given package, e.g:
145+
146+
```bash
147+
npm diff --diff=pkg@2 ./lib/ CHANGELOG.md
148+
```
149+
150+
In the example above the diff output is only going to print contents of files
151+
located within the folder `./lib/` and changed lines of code within the
152+
`CHANGELOG.md` file.
153+
154+
### Configuration
155+
156+
#### diff
157+
158+
* Type: Array<String>
159+
* Default: null
160+
161+
Defines npm package specifiers to compare using the `npm diff` command.
162+
163+
This can be specified up to 2 times.
164+
165+
#### diff-name-only
166+
167+
* Type: Boolean
168+
* Default: false
169+
170+
When set to `true` running `npm diff` only returns the names of the files that
171+
have any difference.
172+
173+
#### diff-unified
174+
175+
* Type: Number
176+
* Default: `3`
177+
178+
The number of lines of context to print in the unified diff format output.
179+
180+
#### diff-ignore-all-space
181+
182+
* Type: Boolean
183+
* Default: false
184+
185+
Ignore whitespace when comparing lines. This ignores differences even if one
186+
line has whitespace where the other line has none.
187+
188+
#### diff-no-prefix
189+
190+
* Type: Boolean
191+
* Default: false
192+
193+
Do not show any source or destination prefix.
194+
195+
#### diff-src-prefix
196+
197+
* Type: String
198+
* Default: `"a/"`
199+
200+
Show the given source prefix in diff patches headers instead of using "a/".
201+
202+
#### diff-dst-prefix
203+
204+
* Type: String
205+
* Default: `"b/"`
206+
207+
Show the given source prefix in diff patches headers instead of using "b/".
208+
209+
#### diff-text
210+
211+
* Type: Boolean
212+
* Default: false
213+
214+
Treat all files as text.
215+
216+
#### global
217+
218+
* Default: false
219+
* Type: Boolean
220+
221+
Uses packages from the global space as a source for comparison.
222+
223+
#### tag
224+
225+
* Type: String
226+
* Default: `"latest"`
227+
228+
The tag used to fetch the tarball that will be compared with the local file
229+
system files when running npm diff with no arguments.
230+
231+
232+
## See Also
233+
234+
* [npm outdated](/commands/npm-outdated)
235+
* [npm install](/commands/npm-install)
236+
* [npm config](/commands/npm-config)
237+
* [npm registry](/using-npm/registry)

docs/content/using-npm/config.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,63 @@ commands that modify your local installation, eg, `install`, `update`,
379379
`dedupe`, `uninstall`. This is NOT currently honored by some network related
380380
commands, eg `dist-tags`, `owner`, etc.
381381

382+
#### diff
383+
384+
* Default: null
385+
* Type: String, Array, null
386+
387+
Define arguments to compare in `npm diff`.
388+
389+
#### diff-name-only
390+
391+
* Default: false
392+
* Type: Boolean
393+
394+
Prints only filenames when using `npm diff`.
395+
396+
#### diff-unified
397+
398+
* Type: number
399+
* Default: `3`
400+
401+
The number of lines of context to print in `npm diff`.
402+
403+
#### diff-ignore-all-space
404+
405+
* Type: Boolean
406+
* Default: false
407+
408+
Ignore whitespace when comparing lines in `npm diff.
409+
410+
#### diff-no-prefix
411+
412+
* Type: Boolean
413+
* Default: false
414+
415+
Do not show any source or destination prefix in `npm diff` output.
416+
417+
#### diff-src-prefix
418+
419+
* Type: String
420+
* Default: `"a/"`
421+
422+
Source prefix to be used in `npm diff` output.
423+
424+
#### diff-dst-prefix
425+
426+
* Type: String
427+
* Default: `"b/"`
428+
429+
Destination prefix to be used in `npm diff` output.
430+
431+
#### diff-text
432+
433+
* Alias: `-a`
434+
* Type: Boolean
435+
* Default: false
436+
437+
Treat all files as text in `npm diff`.
438+
382439
#### editor
383440

384441
* Default: `EDITOR` environment variable if set, or `"vi"` on Posix,

0 commit comments

Comments
 (0)