Skip to content

Commit b7f09ba

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

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

+10618
-9
lines changed

docs/content/commands/npm-diff.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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 --diff=pkg@1.0.0 --diff=pkg@^2.0.0`. It's also possible to
25+
compare across forks of any package,
26+
e.g: `npm diff --diff=pkg@1.0.0 --diff=pkg-fork@1.0.0`.
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+
* `npm diff` (in a package directory, no arguments):
33+
34+
If the package is published to the registry, `npm diff` will fetch the
35+
tarball version tagged as `latest` (this value can be configured using the
36+
`tag` option) and proceed to compare the contents of files present in that
37+
tarball, with the current files in your local file system.
38+
39+
This workflow provides a handy way for package authors to see what
40+
package-tracked files have been changed in comparison with the latest
41+
published version of that package.
42+
43+
* `npm diff --diff=<pkg-name>` (in a package directory):
44+
45+
When using a single package name (with no version or tag specifier) as an
46+
argument, `npm diff` will work in a similar way to
47+
[`npm-outdated`](npm-outdated) and reach for the registry to figure out
48+
what current published version of the package named <pkg-name> will satisfy
49+
its dependent declared semver-range. Once that specific version is known
50+
`npm diff` will print diff patches comparing the current version of
51+
<pkg-name> found in the local file system with that specific version
52+
returned by the registry.
53+
54+
* `npm diff --diff=<spec-a>` (in a package directory):
55+
56+
Similar to using only a single package name, it's also possible to declare
57+
a full registry specifier version if you wish to compare the local version
58+
of a installed package with the specific version/tag/semver-range provided
59+
in `<spec-a>`. e.g: (assuming pkg@1.0.0 is installed in the current
60+
`node_modules` folder) running `npm diff --diff=pkg@2.0.0` will effectively
61+
be an alias to `npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0`.
62+
63+
* `npm diff --diff=<semver-a> [--diff=<semver-b>]` (in a package directory):
64+
65+
Using `npm diff` along with semver-valid version numbers is a shorthand
66+
to compare different versions of the current package. It needs to be run
67+
from a package directory, such that for a package named `pkg` running
68+
`npm diff --diff=1.0.0 --diff=1.0.1` is the same as running
69+
`npm diff --diff=pkg@1.0.0 --diff=pkg@1.0.1`. If only a single argument
70+
`<version-a>` is provided, then the current local file system is going to
71+
be compared against that version.
72+
73+
Note that tag names are not valid `--diff` argument values, if you wish to
74+
compare against a published tag, you must use the `pkg@tagname` syntax.
75+
76+
#### Filtering files
77+
78+
It's possible to also specify positional arguments using file names or globs
79+
pattern matching in order to limit the result of diff patches to only a subset
80+
of files for a given package, e.g:
81+
82+
`npm diff --diff=pkg@2 ./lib/ CHANGELOG.md`
83+
84+
### Configuration
85+
86+
#### diff
87+
88+
* Type: String, Array, null
89+
* Default: null
90+
91+
Defines up to two npm valid package specifiers in which to compare.
92+
93+
#### diff-name-only
94+
95+
* Type: Boolean
96+
* Default: false
97+
98+
When set to `true` running `npm diff` only returns the names of the files that
99+
have any difference.
100+
101+
#### diff-unified
102+
103+
* Type: number
104+
* Default: `3`
105+
106+
The number of lines of context to print in the unified diff format output.
107+
108+
#### diff-ignore-all-space
109+
110+
* Type: Boolean
111+
* Default: false
112+
113+
Ignore whitespace when comparing lines. This ignores differences even if one
114+
line has whitespace where the other line has none.
115+
116+
#### diff-no-prefix
117+
118+
* Type: Boolean
119+
* Default: false
120+
121+
Do not show any source or destination prefix.
122+
123+
#### diff-src-prefix
124+
125+
* Type: String
126+
* Default: `"a/"`
127+
128+
Show the given source prefix in diff patches headers instead of using "a/".
129+
130+
#### diff-dst-prefix
131+
132+
* Type: String
133+
* Default: `"b/"`
134+
135+
Show the given source prefix in diff patches headers instead of using "b/".
136+
137+
#### diff-text
138+
139+
* Type: Boolean
140+
* Default: false
141+
142+
Treat all files as text.
143+
144+
#### global
145+
146+
* Default: false
147+
* Type: Boolean
148+
149+
Uses packages from the global space as source for comparison.
150+
151+
#### tag
152+
153+
* Type: String
154+
* Default: `"latest"`
155+
156+
The tag used to fetch the tarball that will be compared with local file system
157+
files when running npm diff with no arguments.
158+
159+
160+
## See Also
161+
162+
* [npm outdated](/commands/npm-outdated)
163+
* [npm install](/commands/npm-install)
164+
* [npm config](/commands/npm-config)
165+
* [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)