libnpmdiff 1.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.3
4
+
5
+ - fix name of options sent by the npm cli
6
+
7
+ ## 2.0.2
8
+
9
+ - fix matching basename file filter
10
+
11
+ ## 2.0.1
12
+
13
+ - fix for tarballs not listing folder names
14
+
15
+ ## 2.0.0
16
+
17
+ - API rewrite:
18
+ - normalized all options
19
+ - specs to compare are now an array
20
+ - fix context=0
21
+ - added support to filtering by folder names
22
+
3
23
  ## 1.0.1
4
24
 
5
25
  - fixed nameOnly option
package/README.md CHANGED
@@ -20,10 +20,10 @@ The registry diff lib.
20
20
  ```js
21
21
  const libdiff = require('libnpmdiff')
22
22
 
23
- const patch = await libdiff({
24
- a: 'abbrev@1.1.0',
25
- b: 'abbrev@1.1.1'
26
- })
23
+ const patch = await libdiff([
24
+ 'abbrev@1.1.0',
25
+ 'abbrev@1.1.1'
26
+ ])
27
27
  console.log(
28
28
  patch
29
29
  )
@@ -70,27 +70,27 @@ Happy hacking!
70
70
 
71
71
  ### API
72
72
 
73
- #### `> libnpmdif({ a, b }, [opts]) -> Promise<String>`
73
+ #### `> libnpmdif([ a, b ], [opts]) -> Promise<String>`
74
74
 
75
75
  Fetches the registry tarballs and compare files between a spec `a` and spec `b`. **npm** spec types are usually described in `<pkg-name>@<version>` form but multiple other types are alsos supported, for more info on valid specs take a look at [`npm-package-arg`](https://github.com/npm/npm-package-arg).
76
76
 
77
- If only spec `a` is provided, then it's going to try and compare that specified spec against the current project directory in the local file system (cwd may be set via `opts.prefix` option).
78
-
79
77
  **Options**:
80
78
 
81
79
  - `color <Boolean>`: Should add ANSI colors to string output? Defaults to `false`.
82
80
  - `tagVersionPrefix <Sring>`: What prefix should be used to define version numbers. Defaults to `v`
83
- - `prefix <String>`: The path to use as current working directory if trying to read from local file system when using only a single comparison parameter `a`. Defaults to `.`.
81
+ - `diffUnified <Number>`: How many lines of code to print before/after each diff. Defaults to `3`.
82
+ - `diffFiles <Array<String>>`: If set only prints patches for the files listed in this array (also accepts globs). Defaults to `undefined`.
83
+ - `diffIgnoreAllSpace <Boolean>`: Whether or not should ignore changes in whitespace (very useful to avoid indentation changes extra diff lines). Defaults to `false`.
84
+ - `diffNameOnly <Boolean>`: Prints only file names and no patch diffs. Defaults to `false`.
85
+ - `diffNoPrefix <Boolean>`: If true then skips printing any prefixes in filenames. Defaults to `false`.
86
+ - `diffSrcPrefix <String>`: Prefix to be used in the filenames from `a`. Defaults to `a/`.
87
+ - `diffDstPrefix <String>`: Prefix to be used in the filenames from `b`. Defaults to `b/`.
88
+ - `diffText <Boolean>`: Should treat all files as text and try to print diff for binary files. Defaults to `false`.
84
89
  - ...`cache`, `registry` and other common options accepted by [pacote](https://github.com/npm/pacote#options)
85
- - `diffOpts <Object>`: Object containing extra options on how to format the diff patch output:
86
- - `context <Number>`: How many lines of code to print before/after each diff. Defaults to `3`.
87
- - `files <Array<String>>`: If set only prints patches for the files listed in this array (also accepts globs). Defaults to `undefined`.
88
- - `ignoreWhitespace <Boolean>`: Whether or not should ignore changes in whitespace (very useful to avoid indentation changes extra diff lines). Defaults to `false`.
89
- - `nameOnly <Boolean>`: Prints only file names and no patch diffs. Defaults to `false`.
90
- - `noPrefix <Boolean>`: If true then skips printing any prefixes in filenames. Defaults to `false`.
91
- - `srcPrefix <String>`: Prefix to be used in the filenames from `a`. Defaults to `a/`.
92
- - `dstPrefix <String>`: Prefix to be used in the filenames from `b`. Defaults to `b/`.
93
- - `text <Boolean>`: Should treat all files as text and try to print diff for binary files. Defaults to `false`.
90
+
91
+ Returns a `Promise` that fullfils with a `String` containing the resulting patch diffs.
92
+
93
+ Throws an error if either `a` or `b` are missing or if trying to diff more than two specs.
94
94
 
95
95
  ## LICENSE
96
96
 
package/index.js CHANGED
@@ -3,17 +3,20 @@ const pacote = require('pacote')
3
3
  const formatDiff = require('./lib/format-diff.js')
4
4
  const untar = require('./lib/untar.js')
5
5
 
6
+ const argsError = () =>
7
+ Object.assign(
8
+ new TypeError('libnpmdiff needs two arguments to compare'),
9
+ { code: 'EDIFFARGS' }
10
+ )
6
11
  const diff = async (specs, opts = {}) => {
7
- const { prefix: path } = opts
8
-
9
- const aManifest = await pacote.manifest(specs.a, opts)
10
-
11
- // when using a single argument the spec to compare from is going to be
12
- // figured out from reading the current location package
13
- if (!specs.b)
14
- specs.b = `file:${path || '.'}`
15
-
16
- const bManifest = await pacote.manifest(specs.b, opts)
12
+ if (specs.length !== 2)
13
+ throw argsError()
14
+
15
+ const [
16
+ aManifest,
17
+ bManifest,
18
+ ] =
19
+ await Promise.all(specs.map(spec => pacote.manifest(spec, opts)))
17
20
 
18
21
  const versions = {
19
22
  a: aManifest.version,
@@ -7,9 +7,8 @@ const shouldPrintPatch = require('./should-print-patch.js')
7
7
 
8
8
  const formatDiff = ({ files, opts = {}, refs, versions }) => {
9
9
  let res = ''
10
- const diffOpts = opts.diffOpts || {}
11
- const srcPrefix = diffOpts.noPrefix ? '' : diffOpts.srcPrefix || 'a/'
12
- const dstPrefix = diffOpts.noPrefix ? '' : diffOpts.dstPrefix || 'b/'
10
+ const srcPrefix = opts.diffNoPrefix ? '' : opts.diffSrcPrefix || 'a/'
11
+ const dstPrefix = opts.diffNoPrefix ? '' : opts.diffDstPrefix || 'b/'
13
12
 
14
13
  for (const filename of files.values()) {
15
14
  const names = {
@@ -34,7 +33,7 @@ const formatDiff = ({ files, opts = {}, refs, versions }) => {
34
33
  if (contents.a === contents.b && modes.a === modes.b)
35
34
  continue
36
35
 
37
- if (diffOpts.nameOnly) {
36
+ if (opts.diffNameOnly) {
38
37
  res += `${filename}${EOL}`
39
38
  continue
40
39
  }
@@ -71,8 +70,8 @@ const formatDiff = ({ files, opts = {}, refs, versions }) => {
71
70
  '',
72
71
  '',
73
72
  {
74
- context: diffOpts.context || 3,
75
- ignoreWhitespace: diffOpts.ignoreWhitespace,
73
+ context: opts.diffUnified === 0 ? 0 : opts.diffUnified || 3,
74
+ ignoreWhitespace: opts.diffIgnoreAllSpace,
76
75
  }
77
76
  ).replace(
78
77
  '===================================================================\n',
@@ -5,8 +5,7 @@ const binaryExtensions = require('binary-extensions')
5
5
  // we should try to print patches as long as the
6
6
  // extension is not identified as binary files
7
7
  const shouldPrintPatch = (path, opts = {}) => {
8
- const { text } = opts.diffOpts || {}
9
- if (text)
8
+ if (opts.diffText)
10
9
  return true
11
10
 
12
11
  const filename = basename(path)
package/lib/untar.js CHANGED
@@ -10,35 +10,49 @@ const normalizeMatch = str => str
10
10
  const untar = ({ files, refs }, { filterFiles, item, prefix }) => {
11
11
  tar.list({
12
12
  filter: (path, entry) => {
13
+ const fileMatch = () =>
14
+ (!filterFiles.length ||
15
+ filterFiles.some(f => {
16
+ const pattern = normalizeMatch(f)
17
+ return minimatch(
18
+ normalizeMatch(path),
19
+ `{package/,}${pattern}`,
20
+ { matchBase: pattern.startsWith('*') }
21
+ )
22
+ }))
23
+
24
+ // expands usage of simple path filters, e.g: lib or src/
25
+ const folderMatch = () =>
26
+ filterFiles.some(f =>
27
+ normalizeMatch(path).startsWith(normalizeMatch(f)) ||
28
+ normalizeMatch(path).startsWith(`package/${normalizeMatch(f)}`))
29
+
13
30
  if (
14
- entry.type !== 'File' ||
15
- (filterFiles.length &&
16
- !filterFiles.some(f =>
17
- minimatch(normalizeMatch(path), `{package/,}${normalizeMatch(f)}`)))
18
- )
19
- return false
31
+ entry.type === 'File' &&
32
+ (fileMatch() || folderMatch())
33
+ ) {
34
+ const key = path.replace(/^[^/]+\/?/, '')
35
+ files.add(key)
20
36
 
21
- const key = path.replace(/^[^/]+\/?/, '')
22
- files.add(key)
37
+ // should skip reading file when using --name-only option
38
+ let content
39
+ try {
40
+ entry.setEncoding('utf8')
41
+ content = entry.concat()
42
+ } catch (e) {
43
+ /* istanbul ignore next */
44
+ throw Object.assign(
45
+ new Error('failed to read files'),
46
+ { code: 'EDIFFUNTAR' }
47
+ )
48
+ }
23
49
 
24
- // should skip reading file when using --name-only option
25
- let content
26
- try {
27
- entry.setEncoding('utf8')
28
- content = entry.concat()
29
- } catch (e) {
30
- /* istanbul ignore next */
31
- throw Object.assign(
32
- new Error('failed to read files'),
33
- { code: 'EDIFFUNTAR' }
34
- )
50
+ refs.set(`${prefix}${key}`, {
51
+ content,
52
+ mode: `100${entry.mode.toString(8)}`,
53
+ })
54
+ return true
35
55
  }
36
-
37
- refs.set(`${prefix}${key}`, {
38
- content,
39
- mode: `100${entry.mode.toString(8)}`,
40
- })
41
- return true
42
56
  },
43
57
  })
44
58
  .on('error', /* istanbul ignore next */ e => {
@@ -51,9 +65,8 @@ const readTarballs = async (tarballs, opts = {}) => {
51
65
  const files = new Set()
52
66
  const refs = new Map()
53
67
  const arr = [].concat(tarballs)
54
- const { files: _files } = opts.diffOpts || {}
55
68
 
56
- const filterFiles = _files || []
69
+ const filterFiles = opts.diffFiles || []
57
70
 
58
71
  for (const i of arr) {
59
72
  untar({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libnpmdiff",
3
- "version": "1.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "The registry diff",
5
5
  "repository": "https://github.com/npm/libnpmdiff",
6
6
  "files": [
@@ -58,7 +58,7 @@
58
58
  "binary-extensions": "^2.2.0",
59
59
  "diff": "^5.0.0",
60
60
  "minimatch": "^3.0.4",
61
- "pacote": "^11.1.14",
61
+ "pacote": "^11.2.3",
62
62
  "tar": "^6.1.0"
63
63
  }
64
64
  }