Skip to content

Commit 41cfc44

Browse files
committed
feat: add npm diff
- As proposed in RFC: npm/rfcs#144
1 parent eb4f069 commit 41cfc44

60 files changed

Lines changed: 10687 additions & 9 deletions

Some content is hidden

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

docs/content/commands/npm-diff.md

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